Skip to content

Commit 0789628

Browse files
committed
Add realm related example code
1 parent 6a2e6e5 commit 0789628

15 files changed

+1129
-4
lines changed

MPChartExample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ repositories {
5656
dependencies {
5757
//compile fileTree(dir: 'libs', include: ['*.jar'])
5858
//compile project(':MPChartLib-Realm') // clone "https://github.com/PhilJay/MPAndroidChart-Realm" to get this or uncomment the gradle dependency below:
59-
//compile 'com.github.PhilJay:MPAndroidChart-Realm:v0.9.9'
59+
compile 'com.github.PhilJay:MPAndroidChart-Realm:v1.0.0@aar'
6060

6161
compile project(':MPChartLib')
6262
compile 'com.android.support:appcompat-v7:23.1.1'

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.xxmassdeveloper.mpchartexample.StackedBarActivity;
4747
import com.xxmassdeveloper.mpchartexample.StackedBarActivityNegative;
4848
import com.xxmassdeveloper.mpchartexample.fragments.SimpleChartDemo;
49+
import com.xxmassdeveloper.mpchartexample.realm.RealmMainActivity;
4950

5051
import java.util.ArrayList;
5152

@@ -274,8 +275,8 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
274275
startActivity(i);
275276
break;
276277
case 28:
277-
//i = new Intent(this, RealmMainActivity.class);
278-
//startActivity(i);
278+
i = new Intent(this, RealmMainActivity.class);
279+
startActivity(i);
279280
break;
280281
case 29:
281282
i = new Intent(this, LineChartTime.class);
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package com.xxmassdeveloper.mpchartexample.realm;
2+
3+
import android.graphics.Color;
4+
import android.graphics.Typeface;
5+
import android.os.Bundle;
6+
7+
import com.github.mikephil.charting.charts.BarLineChartBase;
8+
import com.github.mikephil.charting.charts.Chart;
9+
import com.github.mikephil.charting.components.XAxis;
10+
import com.github.mikephil.charting.components.YAxis;
11+
import com.github.mikephil.charting.data.ChartData;
12+
import com.github.mikephil.charting.formatter.PercentFormatter;
13+
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;
14+
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
15+
16+
import io.realm.Realm;
17+
import io.realm.RealmConfiguration;
18+
19+
/**
20+
* Created by Philipp Jahoda on 05/11/15.
21+
*/
22+
public abstract class RealmBaseActivity extends DemoBase {
23+
24+
protected Realm mRealm;
25+
26+
protected Typeface mTf;
27+
28+
@Override
29+
protected void onCreate(Bundle savedInstanceState) {
30+
super.onCreate(savedInstanceState);
31+
setTitle("Realm.io Examples");
32+
}
33+
34+
protected void setup(Chart<?> chart) {
35+
36+
mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
37+
38+
// no description text
39+
chart.setDescription("");
40+
chart.setNoDataTextDescription("You need to provide data for the chart.");
41+
42+
// enable touch gestures
43+
chart.setTouchEnabled(true);
44+
45+
if (chart instanceof BarLineChartBase) {
46+
47+
BarLineChartBase mChart = (BarLineChartBase) chart;
48+
49+
mChart.setDrawGridBackground(false);
50+
51+
// enable scaling and dragging
52+
mChart.setDragEnabled(true);
53+
mChart.setScaleEnabled(true);
54+
55+
// if disabled, scaling can be done on x- and y-axis separately
56+
mChart.setPinchZoom(false);
57+
58+
YAxis leftAxis = mChart.getAxisLeft();
59+
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
60+
leftAxis.setTypeface(mTf);
61+
leftAxis.setTextSize(8f);
62+
leftAxis.setTextColor(Color.DKGRAY);
63+
leftAxis.setValueFormatter(new PercentFormatter());
64+
65+
XAxis xAxis = mChart.getXAxis();
66+
xAxis.setTypeface(mTf);
67+
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
68+
xAxis.setTextSize(8f);
69+
xAxis.setTextColor(Color.DKGRAY);
70+
71+
mChart.getAxisRight().setEnabled(false);
72+
}
73+
}
74+
75+
protected void styleData(ChartData data) {
76+
data.setValueTypeface(mTf);
77+
data.setValueTextSize(8f);
78+
data.setValueTextColor(Color.DKGRAY);
79+
data.setValueFormatter(new PercentFormatter());
80+
}
81+
82+
@Override
83+
protected void onResume() {
84+
super.onResume();
85+
86+
// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
87+
RealmConfiguration realmConfig = new RealmConfiguration.Builder(getApplicationContext()).build();
88+
Realm.setDefaultConfiguration(realmConfig);
89+
90+
mRealm = Realm.getDefaultInstance();
91+
}
92+
93+
@Override
94+
protected void onPause() {
95+
super.onPause();
96+
mRealm.close();
97+
}
98+
99+
protected void writeToDB(int objectCount) {
100+
101+
mRealm.beginTransaction();
102+
103+
mRealm.delete(RealmDemoData.class);
104+
105+
for (int i = 0; i < objectCount; i++) {
106+
107+
float value = 40f + (float) (Math.random() * 60f);
108+
109+
RealmDemoData d = new RealmDemoData(i, value);
110+
mRealm.copyToRealm(d);
111+
}
112+
113+
mRealm.commitTransaction();
114+
}
115+
116+
protected void writeToDBStack(int objectCount) {
117+
118+
mRealm.beginTransaction();
119+
120+
mRealm.delete(RealmDemoData.class);
121+
122+
for (int i = 0; i < objectCount; i++) {
123+
124+
float val1 = 34f + (float) (Math.random() * 12.0f);
125+
float val2 = 34f + (float) (Math.random() * 12.0f);
126+
float[] stack = new float[]{val1, val2, 100 - val1 - val2};
127+
128+
RealmDemoData d = new RealmDemoData(i, stack);
129+
mRealm.copyToRealm(d);
130+
}
131+
132+
mRealm.commitTransaction();
133+
}
134+
135+
protected void writeToDBCandle(int objectCount) {
136+
137+
mRealm.beginTransaction();
138+
139+
mRealm.delete(RealmDemoData.class);
140+
141+
for (int i = 0; i < objectCount; i++) {
142+
143+
float mult = 50;
144+
float val = (float) (Math.random() * 40) + mult;
145+
146+
float high = (float) (Math.random() * 9) + 8f;
147+
float low = (float) (Math.random() * 9) + 8f;
148+
149+
float open = (float) (Math.random() * 6) + 1f;
150+
float close = (float) (Math.random() * 6) + 1f;
151+
152+
boolean even = i % 2 == 0;
153+
154+
RealmDemoData d = new RealmDemoData(i, val + high, val - low, even ? val + open : val - open,
155+
even ? val - close : val + close);
156+
157+
mRealm.copyToRealm(d);
158+
}
159+
160+
mRealm.commitTransaction();
161+
}
162+
163+
protected void writeToDBBubble(int objectCount) {
164+
165+
mRealm.beginTransaction();
166+
167+
mRealm.delete(RealmDemoData.class);
168+
169+
for (int i = 0; i < objectCount; i++) {
170+
171+
float value = 30f + (float) (Math.random() * 100.0);
172+
float size = 15f + (float) (Math.random() * 20.0);
173+
174+
RealmDemoData d = new RealmDemoData(i, value, size);
175+
mRealm.copyToRealm(d);
176+
}
177+
178+
mRealm.commitTransaction();
179+
}
180+
181+
protected void writeToDBPie() {
182+
183+
mRealm.beginTransaction();
184+
185+
mRealm.delete(RealmDemoData.class);
186+
187+
float value1 = 15f + (float) (Math.random() * 8f);
188+
float value2 = 15f + (float) (Math.random() * 8f);
189+
float value3 = 15f + (float) (Math.random() * 8f);
190+
float value4 = 15f + (float) (Math.random() * 8f);
191+
float value5 = 100f - value1 - value2 - value3 - value4;
192+
193+
float[] values = new float[] { value1, value2, value3, value4, value5 };
194+
String[] labels = new String[]{ "iOS", "Android", "WP 10", "BlackBerry", "Other"};
195+
196+
for (int i = 0; i < values.length; i++) {
197+
RealmDemoData d = new RealmDemoData(values[i], labels[i]);
198+
mRealm.copyToRealm(d);
199+
}
200+
201+
mRealm.commitTransaction();
202+
}
203+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.xxmassdeveloper.mpchartexample.realm;
2+
3+
import android.os.Bundle;
4+
import android.view.WindowManager;
5+
6+
import com.github.mikephil.charting.animation.Easing;
7+
import com.github.mikephil.charting.charts.BarChart;
8+
import com.github.mikephil.charting.data.BarData;
9+
import com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet;
10+
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
11+
import com.github.mikephil.charting.utils.ColorTemplate;
12+
import com.xxmassdeveloper.mpchartexample.R;
13+
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;
14+
15+
import java.util.ArrayList;
16+
17+
import io.realm.RealmResults;
18+
19+
/**
20+
* Created by Philipp Jahoda on 21/10/15.
21+
*/
22+
public class RealmDatabaseActivityBar extends RealmBaseActivity {
23+
24+
private BarChart mChart;
25+
26+
@Override
27+
protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
30+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
31+
setContentView(R.layout.activity_barchart_noseekbar);
32+
33+
mChart = (BarChart) findViewById(R.id.chart1);
34+
setup(mChart);
35+
}
36+
37+
@Override
38+
protected void onResume() {
39+
super.onResume(); // setup realm
40+
41+
// write some demo-data into the realm.io database
42+
writeToDB(20);
43+
44+
// add data to the chart
45+
setData();
46+
}
47+
48+
private void setData() {
49+
50+
RealmResults<RealmDemoData> result = mRealm.where(RealmDemoData.class).findAll();
51+
52+
//RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "stackValues", "xIndex"); // normal entries
53+
RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "xValue", "yValue"); // stacked entries
54+
set.setColors(new int[] {ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4")});
55+
set.setLabel("Realm BarDataSet");
56+
57+
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
58+
dataSets.add(set); // add the dataset
59+
60+
// create a data object with the dataset list
61+
BarData data = new BarData(dataSets);
62+
styleData(data);
63+
64+
// set data
65+
mChart.setData(data);
66+
mChart.setFitBars(true);
67+
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
68+
}
69+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.xxmassdeveloper.mpchartexample.realm;
2+
3+
import android.os.Bundle;
4+
import android.view.WindowManager;
5+
6+
import com.github.mikephil.charting.animation.Easing;
7+
import com.github.mikephil.charting.charts.BubbleChart;
8+
import com.github.mikephil.charting.data.BubbleData;
9+
import com.github.mikephil.charting.data.realm.implementation.RealmBubbleDataSet;
10+
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
11+
import com.github.mikephil.charting.utils.ColorTemplate;
12+
import com.xxmassdeveloper.mpchartexample.R;
13+
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;
14+
15+
import java.util.ArrayList;
16+
17+
import io.realm.RealmResults;
18+
19+
/**
20+
* Created by Philipp Jahoda on 21/10/15.
21+
*/
22+
public class RealmDatabaseActivityBubble extends RealmBaseActivity {
23+
24+
private BubbleChart mChart;
25+
26+
@Override
27+
protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
30+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
31+
setContentView(R.layout.activity_bubblechart_noseekbar);
32+
33+
mChart = (BubbleChart) findViewById(R.id.chart1);
34+
setup(mChart);
35+
36+
mChart.getXAxis().setDrawGridLines(false);
37+
mChart.getAxisLeft().setDrawGridLines(false);
38+
mChart.setPinchZoom(true);
39+
}
40+
41+
@Override
42+
protected void onResume() {
43+
super.onResume(); // setup realm
44+
45+
// write some demo-data into the realm.io database
46+
writeToDBBubble(10);
47+
48+
// add data to the chart
49+
setData();
50+
}
51+
52+
private void setData() {
53+
54+
RealmResults<RealmDemoData> result = mRealm.where(RealmDemoData.class).findAll();
55+
56+
RealmBubbleDataSet<RealmDemoData> set = new RealmBubbleDataSet<RealmDemoData>(result, "xValue", "yValue", "bubbleSize");
57+
set.setLabel("Realm BubbleDataSet");
58+
set.setColors(ColorTemplate.COLORFUL_COLORS, 110);
59+
60+
ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
61+
dataSets.add(set); // add the dataset
62+
63+
// create a data object with the dataset list
64+
BubbleData data = new BubbleData(dataSets);
65+
styleData(data);
66+
67+
// set data
68+
mChart.setData(data);
69+
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
70+
}
71+
}

0 commit comments

Comments
 (0)