Skip to content

Commit 9615cc2

Browse files
committed
Improve example
1 parent a6e6fed commit 9615cc2

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77
import android.view.WindowManager;
88

99
import com.github.mikephil.charting.charts.BarChart;
10-
import com.github.mikephil.charting.components.Legend;
11-
import com.github.mikephil.charting.components.Legend.LegendForm;
12-
import com.github.mikephil.charting.components.Legend.LegendPosition;
1310
import com.github.mikephil.charting.components.XAxis;
1411
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
1512
import com.github.mikephil.charting.components.YAxis;
1613
import com.github.mikephil.charting.data.BarData;
1714
import com.github.mikephil.charting.data.BarDataSet;
1815
import com.github.mikephil.charting.data.BarEntry;
1916
import com.github.mikephil.charting.data.Entry;
20-
import com.github.mikephil.charting.formatter.ValueFormatter;
21-
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
2217
import com.github.mikephil.charting.utils.ViewPortHandler;
2318
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2419

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

4643
mChart.setDrawBarShadow(false);
4744
mChart.setDrawValueAboveBar(true);
@@ -60,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
6057
xAxis.setDrawAxisLine(false);
6158
xAxis.setSpaceBetweenLabels(2);
6259
xAxis.setTextColor(Color.LTGRAY);
63-
xAxis.setTextSize(12f);
60+
xAxis.setTextSize(13f);
6461

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

8683
private void setData(List<Data> dataList) {
8784

88-
ArrayList<BarEntry> positiveValues = new ArrayList<BarEntry>();
89-
ArrayList<BarEntry> negativeValues = new ArrayList<BarEntry>();
85+
ArrayList<BarEntry> values = new ArrayList<BarEntry>();
9086
String[] dates = new String[dataList.size()];
87+
List<Integer> colors = new ArrayList<Integer>();
88+
89+
int green = Color.rgb(110, 190, 102);
90+
int red = Color.rgb(211, 74, 88);
9191

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

9494
Data d = dataList.get(i);
9595
BarEntry entry = new BarEntry(d.yValue, d.xIndex);
96+
values.add(entry);
9697

98+
dates[i] = dataList.get(i).xAxisValue;
99+
100+
// specific colors
97101
if (d.yValue >= 0)
98-
positiveValues.add(entry);
102+
colors.add(red);
99103
else
100-
negativeValues.add(entry);
101-
102-
dates[i] = dataList.get(i).xAxisValue;
104+
colors.add(green);
103105
}
104106

105-
int green = Color.rgb(110, 190, 102);
106-
int red = Color.rgb(211, 74, 88);
107-
108-
BarDataSet positive = new BarDataSet(positiveValues, "Positive");
109-
positive.setBarSpacePercent(35f);
110-
positive.setColor(red);
111-
positive.setValueTextColor(red);
112-
113-
BarDataSet negative = new BarDataSet(negativeValues, "Negative");
114-
negative.setBarSpacePercent(35f);
115-
negative.setColor(green);
116-
negative.setValueTextColor(green);
117-
118-
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
119-
dataSets.add(positive);
120-
dataSets.add(negative);
107+
BarDataSet set = new BarDataSet(values, "Values");
108+
set.setBarSpacePercent(40f);
109+
set.setColors(colors);
110+
set.setValueTextColor(red);
121111

122-
BarData data = new BarData(dates, dataSets);
123-
data.setValueTextSize(12f);
112+
BarData data = new BarData(dates, set);
113+
data.setValueTextSize(13f);
124114
data.setValueTypeface(mTf);
125115
data.setValueFormatter(new ValueFormatter());
126116

0 commit comments

Comments
 (0)