Skip to content

Commit

Permalink
Improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Feb 6, 2016
1 parent a6e6fed commit 9615cc2
Showing 1 changed file with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
import android.view.WindowManager;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

Expand All @@ -42,6 +37,8 @@ protected void onCreate(Bundle savedInstanceState) {
mChart = (BarChart) findViewById(R.id.chart1);
mChart.setExtraTopOffset(-30f);
mChart.setExtraBottomOffset(10f);
mChart.setExtraLeftOffset(70f);
mChart.setExtraRightOffset(70f);

mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
Expand All @@ -60,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setDrawAxisLine(false);
xAxis.setSpaceBetweenLabels(2);
xAxis.setTextColor(Color.LTGRAY);
xAxis.setTextSize(12f);
xAxis.setTextSize(13f);

YAxis left = mChart.getAxisLeft();
left.setDrawLabels(false);
Expand All @@ -85,42 +82,35 @@ protected void onCreate(Bundle savedInstanceState) {

private void setData(List<Data> dataList) {

ArrayList<BarEntry> positiveValues = new ArrayList<BarEntry>();
ArrayList<BarEntry> negativeValues = new ArrayList<BarEntry>();
ArrayList<BarEntry> values = new ArrayList<BarEntry>();
String[] dates = new String[dataList.size()];
List<Integer> colors = new ArrayList<Integer>();

int green = Color.rgb(110, 190, 102);
int red = Color.rgb(211, 74, 88);

for (int i = 0; i < dataList.size(); i++) {

Data d = dataList.get(i);
BarEntry entry = new BarEntry(d.yValue, d.xIndex);
values.add(entry);

dates[i] = dataList.get(i).xAxisValue;

// specific colors
if (d.yValue >= 0)
positiveValues.add(entry);
colors.add(red);
else
negativeValues.add(entry);

dates[i] = dataList.get(i).xAxisValue;
colors.add(green);
}

int green = Color.rgb(110, 190, 102);
int red = Color.rgb(211, 74, 88);

BarDataSet positive = new BarDataSet(positiveValues, "Positive");
positive.setBarSpacePercent(35f);
positive.setColor(red);
positive.setValueTextColor(red);

BarDataSet negative = new BarDataSet(negativeValues, "Negative");
negative.setBarSpacePercent(35f);
negative.setColor(green);
negative.setValueTextColor(green);

ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(positive);
dataSets.add(negative);
BarDataSet set = new BarDataSet(values, "Values");
set.setBarSpacePercent(40f);
set.setColors(colors);
set.setValueTextColor(red);

BarData data = new BarData(dates, dataSets);
data.setValueTextSize(12f);
BarData data = new BarData(dates, set);
data.setValueTextSize(13f);
data.setValueTypeface(mTf);
data.setValueFormatter(new ValueFormatter());

Expand Down

0 comments on commit 9615cc2

Please sign in to comment.