Skip to content

Commit d8a898f

Browse files
committed
Work on implementing realm stacked-bars support
1 parent 00574b7 commit d8a898f

9 files changed

Lines changed: 250 additions & 36 deletions

File tree

‎MPChartExample/AndroidManifest.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
<activity android:name="RadarChartActivitry"></activity>
4646
<activity android:name="LineChartActivityColored"></activity>
4747
<activity android:name="DynamicalAddingActivity"></activity>
48-
<activity android:name="RealmDatabaseActivity"></activity>
48+
<activity android:name=".RealmDatabaseActivityLine"></activity>
49+
<activity android:name=".RealmDatabaseActivityBar"></activity>
4950
<activity android:name="RealtimeLineChartActivity"></activity>
5051
<activity android:name="CombinedChartActivity"></activity>
5152
<activity android:name="PerformanceLineChart"></activity>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent" >
5+
6+
<com.github.mikephil.charting.charts.BarChart
7+
android:id="@+id/chart1"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent" />
10+
11+
</RelativeLayout>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.xxmassdeveloper.mpchartexample;
2+
3+
import android.graphics.Typeface;
4+
import android.os.Bundle;
5+
import android.view.WindowManager;
6+
7+
import com.github.mikephil.charting.animation.Easing;
8+
import com.github.mikephil.charting.charts.BarChart;
9+
import com.github.mikephil.charting.charts.LineChart;
10+
import com.github.mikephil.charting.components.YAxis;
11+
import com.github.mikephil.charting.data.BarData;
12+
import com.github.mikephil.charting.data.LineData;
13+
import com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet;
14+
import com.github.mikephil.charting.data.realm.implementation.RealmLineDataSet;
15+
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
16+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
17+
import com.github.mikephil.charting.utils.ColorTemplate;
18+
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;
19+
import com.xxmassdeveloper.mpchartexample.notimportant.RealmBaseActivity;
20+
21+
import java.util.ArrayList;
22+
23+
import io.realm.RealmResults;
24+
25+
/**
26+
* Created by Philipp Jahoda on 21/10/15.
27+
*/
28+
public class RealmDatabaseActivityBar extends RealmBaseActivity {
29+
30+
private BarChart mChart;
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
36+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
37+
setContentView(R.layout.activity_barchart_noseekbar);
38+
39+
mChart = (BarChart) findViewById(R.id.chart1);
40+
setup(mChart);
41+
}
42+
43+
@Override
44+
protected void onResume() {
45+
super.onResume(); // setup realm
46+
47+
// write some demo-data into the realm.io database
48+
writeToDBStack(200);
49+
50+
// add data to the chart
51+
setData();
52+
}
53+
54+
private void setData() {
55+
56+
RealmResults<RealmDemoData> result = mRealm.allObjects(RealmDemoData.class);
57+
58+
RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "stackValues", "xIndex");
59+
set.setValueTextSize(9f);
60+
set.setColors(getColors(set.getStackSize()));
61+
set.setLabel("Realm BarDataSet");
62+
set.setStackLabels(new String[]{"Births", "Divorces", "Marriages"});
63+
64+
System.out.println("Stacksize: " + set.getStackSize());
65+
66+
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
67+
dataSets.add(set); // add the dataset
68+
69+
// create a data object with the dataset list
70+
BarData data = new BarData(result, "xValue", dataSets);
71+
72+
// set data
73+
mChart.setData(data);
74+
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
75+
}
76+
77+
private int[] getColors(int stacksize) {
78+
79+
// have as many colors as stack-values per entry
80+
int[] colors = new int[stacksize];
81+
82+
for (int i = 0; i < stacksize; i++) {
83+
colors[i] = ColorTemplate.VORDIPLOM_COLORS[i];
84+
}
85+
86+
return colors;
87+
}
88+
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealmDatabaseActivity.java renamed to MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealmDatabaseActivityLine.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* Created by Philipp Jahoda on 21/10/15.
2626
*/
27-
public class RealmDatabaseActivity extends RealmBaseActivity {
27+
public class RealmDatabaseActivityLine extends RealmBaseActivity {
2828

2929
private LineChart mChart;
3030

@@ -36,34 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {
3636
setContentView(R.layout.activity_linechart_noseekbar);
3737

3838
mChart = (LineChart) findViewById(R.id.chart1);
39-
mChart.setDrawGridBackground(false);
40-
41-
// no description text
42-
mChart.setDescription("");
43-
mChart.setNoDataTextDescription("You need to provide data for the chart.");
44-
45-
// enable touch gestures
46-
mChart.setTouchEnabled(true);
47-
48-
// enable scaling and dragging
49-
mChart.setDragEnabled(true);
50-
mChart.setScaleEnabled(true);
51-
52-
// if disabled, scaling can be done on x- and y-axis separately
53-
mChart.setPinchZoom(false);
54-
55-
Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
56-
57-
YAxis leftAxis = mChart.getAxisLeft();
58-
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
59-
leftAxis.setAxisMaxValue(220f);
60-
leftAxis.setAxisMinValue(-50f);
61-
leftAxis.setStartAtZero(false);
62-
leftAxis.setTypeface(tf);
63-
64-
mChart.getXAxis().setTypeface(tf);
65-
66-
mChart.getAxisRight().setEnabled(false);
39+
setup(mChart);
6740
}
6841

6942
@Override

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/RealmDemoData.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.xxmassdeveloper.mpchartexample.custom;
22

33

4+
import io.realm.RealmList;
45
import io.realm.RealmObject;
56

67
public class RealmDemoData extends RealmObject {
78

89
private float value;
10+
private RealmList<RealmFloat> stackValues;
911
private int xIndex;
1012

1113
private String xValue;
@@ -24,6 +26,23 @@ public RealmDemoData(float value, int xIndex, String xValue) {
2426
this.xValue = xValue;
2527
}
2628

29+
/**
30+
* Constructor for stacked bars.
31+
*
32+
* @param stackValues
33+
* @param xIndex
34+
* @param xValue
35+
*/
36+
public RealmDemoData(float[] stackValues, int xIndex, String xValue) {
37+
this.xIndex = xIndex;
38+
this.xValue = xValue;
39+
this.stackValues = new RealmList<>();
40+
41+
for(float val : stackValues) {
42+
this.stackValues.add(new RealmFloat(val));
43+
}
44+
}
45+
2746
public float getValue() {
2847
return value;
2948
}
@@ -32,6 +51,14 @@ public void setValue(float value) {
3251
this.value = value;
3352
}
3453

54+
public RealmList<RealmFloat> getStackValues() {
55+
return stackValues;
56+
}
57+
58+
public void setStackValues(RealmList<RealmFloat> stackValues) {
59+
this.stackValues = stackValues;
60+
}
61+
3562
public int getxIndex() {
3663
return xIndex;
3764
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.xxmassdeveloper.mpchartexample.custom;
2+
3+
import io.realm.RealmObject;
4+
5+
/**
6+
* Created by Philipp Jahoda on 09/11/15.
7+
*/
8+
public class RealmFloat extends RealmObject {
9+
10+
private float value;
11+
12+
public RealmFloat() {
13+
14+
}
15+
16+
public RealmFloat(float value) {
17+
this.value = value;
18+
}
19+
20+
public float getValue() {
21+
return value;
22+
}
23+
24+
public void setValue(float value) {
25+
this.value = value;
26+
}
27+
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
import com.xxmassdeveloper.mpchartexample.PieChartActivity;
4141
import com.xxmassdeveloper.mpchartexample.R;
4242
import com.xxmassdeveloper.mpchartexample.RadarChartActivitry;
43-
import com.xxmassdeveloper.mpchartexample.RealmDatabaseActivity;
43+
import com.xxmassdeveloper.mpchartexample.RealmDatabaseActivityBar;
44+
import com.xxmassdeveloper.mpchartexample.RealmDatabaseActivityLine;
4445
import com.xxmassdeveloper.mpchartexample.RealtimeLineChartActivity;
4546
import com.xxmassdeveloper.mpchartexample.ScatterChartActivity;
4647
import com.xxmassdeveloper.mpchartexample.ScrollViewActivity;
@@ -126,7 +127,10 @@ protected void onCreate(Bundle savedInstanceState) {
126127
"Chart in ScrollView",
127128
"This demonstrates how to use a chart inside a ScrollView."));
128129
objects.add(new ContentItem(
129-
"Realm.io Database",
130+
"Realm.io Database LineChart",
131+
"This demonstrates how to use this library with Realm.io mobile database."));
132+
objects.add(new ContentItem(
133+
"Realm.io Database BarChart",
130134
"This demonstrates how to use this library with Realm.io mobile database."));
131135

132136
MyAdapter adapter = new MyAdapter(this, objects);
@@ -248,7 +252,11 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
248252
startActivity(i);
249253
break;
250254
case 26:
251-
i = new Intent(this, RealmDatabaseActivity.class);
255+
i = new Intent(this, RealmDatabaseActivityLine.class);
256+
startActivity(i);
257+
break;
258+
case 27:
259+
i = new Intent(this, RealmDatabaseActivityBar.class);
252260
startActivity(i);
253261
break;
254262
}

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/RealmBaseActivity.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.xxmassdeveloper.mpchartexample.notimportant;
22

3+
import android.graphics.Typeface;
4+
5+
import com.github.mikephil.charting.charts.BarLineChartBase;
6+
import com.github.mikephil.charting.charts.Chart;
7+
import com.github.mikephil.charting.components.YAxis;
38
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;
49

510
import io.realm.Realm;
@@ -12,6 +17,38 @@ public abstract class RealmBaseActivity extends DemoBase {
1217

1318
protected Realm mRealm;
1419

20+
protected void setup(BarLineChartBase mChart) {
21+
22+
mChart.setDrawGridBackground(false);
23+
24+
// no description text
25+
mChart.setDescription("");
26+
mChart.setNoDataTextDescription("You need to provide data for the chart.");
27+
28+
// enable touch gestures
29+
mChart.setTouchEnabled(true);
30+
31+
// enable scaling and dragging
32+
mChart.setDragEnabled(true);
33+
mChart.setScaleEnabled(true);
34+
35+
// if disabled, scaling can be done on x- and y-axis separately
36+
mChart.setPinchZoom(false);
37+
38+
Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
39+
40+
YAxis leftAxis = mChart.getAxisLeft();
41+
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
42+
leftAxis.setAxisMaxValue(220f);
43+
leftAxis.setAxisMinValue(-50f);
44+
leftAxis.setStartAtZero(false);
45+
leftAxis.setTypeface(tf);
46+
47+
mChart.getXAxis().setTypeface(tf);
48+
49+
mChart.getAxisRight().setEnabled(false);
50+
}
51+
1552
@Override
1653
protected void onResume() {
1754
super.onResume();
@@ -47,4 +84,24 @@ protected void writeToDB(int objectCount) {
4784

4885
mRealm.commitTransaction();
4986
}
87+
88+
protected void writeToDBStack(int objectCount) {
89+
90+
mRealm.beginTransaction();
91+
92+
mRealm.clear(RealmDemoData.class);
93+
94+
for(int i = 0; i < objectCount; i++) {
95+
96+
float val1 = 20f + (float) (Math.random() * 50.0);
97+
float val2 = 20f + (float) (Math.random() * 50.0);
98+
float val3 = 20f + (float) (Math.random() * 50.0);
99+
float [] stack = new float[] {val1, val2, val3};
100+
101+
RealmDemoData d = new RealmDemoData(stack, i, "" + i);
102+
mRealm.copyToRealm(d);
103+
}
104+
105+
mRealm.commitTransaction();
106+
}
50107
}

‎MPChartLib/src/com/github/mikephil/charting/data/realm/implementation/RealmBarDataSet.java‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import com.github.mikephil.charting.data.realm.base.RealmBarLineScatterCandleBubbleDataSet;
77
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
88

9+
import io.realm.RealmList;
910
import io.realm.RealmObject;
1011
import io.realm.RealmResults;
12+
import io.realm.dynamic.DynamicRealmList;
1113
import io.realm.dynamic.DynamicRealmObject;
1214

1315
/**
@@ -50,10 +52,30 @@ public RealmBarDataSet(RealmResults<T> results, String yValuesField, String xInd
5052
@Override
5153
public void build(RealmResults<T> results) {
5254

53-
for (T object : results) {
55+
for (T realmObject : results) {
5456

55-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
56-
mValues.add(new BarEntry(dynamicObject.getFloat(mValuesField), dynamicObject.getInt(mIndexField)));
57+
DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);
58+
59+
try {
60+
61+
float value = dynamicObject.getFloat(mValuesField);
62+
mValues.add(new BarEntry(value, dynamicObject.getInt(mIndexField)));
63+
64+
} catch(IllegalArgumentException e) {
65+
66+
DynamicRealmList list = dynamicObject.getList(mValuesField);
67+
float [] values = new float[list.size()];
68+
69+
int i = 0;
70+
for(DynamicRealmObject o : list) {
71+
values[i] = o.getFloat("value");
72+
System.out.println("Value: " + values[i]);
73+
i++;
74+
}
75+
mValues.add(new BarEntry(values, dynamicObject.getInt(mIndexField)));
76+
77+
mStackSize = Math.max(mStackSize, values.length);
78+
}
5779
}
5880
}
5981

0 commit comments

Comments
 (0)