Skip to content

Commit a6e6fed

Browse files
committed
Work on new example with multicolor barchart
1 parent f19b8e8 commit a6e6fed

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

MPChartExample/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<activity android:name="ScrollViewActivity"></activity>
6262
<activity android:name="StackedBarActivityNegative"></activity>
6363
<activity android:name=".realm.RealmWikiExample"></activity>
64+
<activity android:name=".BarChartPositiveNegative"></activity>
6465
</application>
6566

6667
</manifest>
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
2+
package com.xxmassdeveloper.mpchartexample;
3+
4+
import android.graphics.Color;
5+
import android.graphics.Typeface;
6+
import android.os.Bundle;
7+
import android.view.WindowManager;
8+
9+
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;
13+
import com.github.mikephil.charting.components.XAxis;
14+
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
15+
import com.github.mikephil.charting.components.YAxis;
16+
import com.github.mikephil.charting.data.BarData;
17+
import com.github.mikephil.charting.data.BarDataSet;
18+
import com.github.mikephil.charting.data.BarEntry;
19+
import com.github.mikephil.charting.data.Entry;
20+
import com.github.mikephil.charting.formatter.ValueFormatter;
21+
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
22+
import com.github.mikephil.charting.utils.ViewPortHandler;
23+
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
24+
25+
import java.text.DecimalFormat;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
29+
public class BarChartPositiveNegative extends DemoBase {
30+
31+
protected BarChart mChart;
32+
private Typeface mTf;
33+
34+
@Override
35+
protected void onCreate(Bundle savedInstanceState) {
36+
super.onCreate(savedInstanceState);
37+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
38+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
39+
setContentView(R.layout.activity_barchart_noseekbar);
40+
41+
mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
42+
mChart = (BarChart) findViewById(R.id.chart1);
43+
mChart.setExtraTopOffset(-30f);
44+
mChart.setExtraBottomOffset(10f);
45+
46+
mChart.setDrawBarShadow(false);
47+
mChart.setDrawValueAboveBar(true);
48+
49+
mChart.setDescription("");
50+
51+
// scaling can now only be done on x- and y-axis separately
52+
mChart.setPinchZoom(false);
53+
54+
mChart.setDrawGridBackground(false);
55+
56+
XAxis xAxis = mChart.getXAxis();
57+
xAxis.setPosition(XAxisPosition.BOTTOM);
58+
xAxis.setTypeface(mTf);
59+
xAxis.setDrawGridLines(false);
60+
xAxis.setDrawAxisLine(false);
61+
xAxis.setSpaceBetweenLabels(2);
62+
xAxis.setTextColor(Color.LTGRAY);
63+
xAxis.setTextSize(12f);
64+
65+
YAxis left = mChart.getAxisLeft();
66+
left.setDrawLabels(false);
67+
left.setStartAtZero(false);
68+
left.setSpaceTop(25f);
69+
left.setSpaceBottom(25f);
70+
left.setDrawAxisLine(false);
71+
left.setDrawGridLines(false);
72+
mChart.getAxisRight().setEnabled(false);
73+
mChart.getLegend().setEnabled(false);
74+
75+
// THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
76+
List<Data> data = new ArrayList<>();
77+
data.add(new Data(0, -224.1f, "12-29"));
78+
data.add(new Data(1, 238.5f, "12-30"));
79+
data.add(new Data(2, 1280.1f, "12-31"));
80+
data.add(new Data(3, -442.3f, "01-01"));
81+
data.add(new Data(4, -2280.1f, "01-02"));
82+
83+
setData(data);
84+
}
85+
86+
private void setData(List<Data> dataList) {
87+
88+
ArrayList<BarEntry> positiveValues = new ArrayList<BarEntry>();
89+
ArrayList<BarEntry> negativeValues = new ArrayList<BarEntry>();
90+
String[] dates = new String[dataList.size()];
91+
92+
for (int i = 0; i < dataList.size(); i++) {
93+
94+
Data d = dataList.get(i);
95+
BarEntry entry = new BarEntry(d.yValue, d.xIndex);
96+
97+
if (d.yValue >= 0)
98+
positiveValues.add(entry);
99+
else
100+
negativeValues.add(entry);
101+
102+
dates[i] = dataList.get(i).xAxisValue;
103+
}
104+
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);
121+
122+
BarData data = new BarData(dates, dataSets);
123+
data.setValueTextSize(12f);
124+
data.setValueTypeface(mTf);
125+
data.setValueFormatter(new ValueFormatter());
126+
127+
mChart.setData(data);
128+
mChart.invalidate();
129+
}
130+
131+
/**
132+
* Demo class representing data.
133+
*/
134+
private class Data {
135+
136+
public String xAxisValue;
137+
public float yValue;
138+
public int xIndex;
139+
140+
public Data(int xIndex, float yValue, String xAxisValue) {
141+
this.xAxisValue = xAxisValue;
142+
this.yValue = yValue;
143+
this.xIndex = xIndex;
144+
}
145+
}
146+
147+
private class ValueFormatter implements com.github.mikephil.charting.formatter.ValueFormatter {
148+
149+
private DecimalFormat mFormat;
150+
151+
public ValueFormatter() {
152+
mFormat = new DecimalFormat("######.0");
153+
}
154+
155+
@Override
156+
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
157+
return mFormat.format(value);
158+
}
159+
}
160+
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.xxmassdeveloper.mpchartexample.BarChartActivity;
1919
import com.xxmassdeveloper.mpchartexample.BarChartActivityMultiDataset;
2020
import com.xxmassdeveloper.mpchartexample.BarChartActivitySinus;
21+
import com.xxmassdeveloper.mpchartexample.BarChartPositiveNegative;
2122
import com.xxmassdeveloper.mpchartexample.BubbleChartActivity;
2223
import com.xxmassdeveloper.mpchartexample.CandleStickChartActivity;
2324
import com.xxmassdeveloper.mpchartexample.CombinedChartActivity;
@@ -123,6 +124,9 @@ protected void onCreate(Bundle savedInstanceState) {
123124
objects.add(new ContentItem(
124125
"Chart in ScrollView",
125126
"This demonstrates how to use a chart inside a ScrollView."));
127+
objects.add(new ContentItem(
128+
"BarChart positive / negative",
129+
"This demonstrates how to create a BarChart with positive and negative values in different colors."));
126130

127131
ContentItem realm = new ContentItem(
128132
"Realm.io Database",
@@ -249,6 +253,10 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
249253
startActivity(i);
250254
break;
251255
case 26:
256+
i = new Intent(this, BarChartPositiveNegative.class);
257+
startActivity(i);
258+
break;
259+
case 27:
252260
i = new Intent(this, RealmMainActivity.class);
253261
startActivity(i);
254262
break;

0 commit comments

Comments
 (0)