X Tutup
Skip to content

Commit cedebda

Browse files
committed
Add realm wiki example code
1 parent 3f79bb5 commit cedebda

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

MPChartExample/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<activity android:name="BarChartActivitySinus"></activity>
6161
<activity android:name="ScrollViewActivity"></activity>
6262
<activity android:name="StackedBarActivityNegative"></activity>
63+
<activity android:name=".realm.RealmWikiExample"></activity>
6364
</application>
6465

6566
</manifest>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="@android:color/white"
6+
android:orientation="vertical">
7+
8+
<com.github.mikephil.charting.charts.LineChart
9+
android:id="@+id/lineChart"
10+
android:layout_width="match_parent"
11+
android:layout_height="0dp"
12+
android:layout_weight="1" />
13+
14+
<com.github.mikephil.charting.charts.BarChart
15+
android:id="@+id/barChart"
16+
android:layout_width="match_parent"
17+
android:layout_height="0dp"
18+
android:layout_weight="1" />
19+
20+
</LinearLayout>

MPChartExample/src/com/xxmassdeveloper/mpchartexample/realm/RealmMainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
4242
objects.add(new ContentItem("Bubble Chart", "Creating a BubbleChart with Realm.io database"));
4343
objects.add(new ContentItem("Pie Chart", "Creating a PieChart with Realm.io database"));
4444
objects.add(new ContentItem("Radar Chart", "Creating a RadarChart with Realm.io database"));
45+
objects.add(new ContentItem("Realm Wiki", "This is the code related to the wiki entry about realm.io on the MPAndroidChart github page."));
4546

4647
MyAdapter adapter = new MyAdapter(this, objects);
4748

@@ -89,6 +90,10 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
8990
i = new Intent(this, RealmDatabaseActivityRadar.class);
9091
startActivity(i);
9192
break;
93+
case 8:
94+
i = new Intent(this, RealmWikiExample.class);
95+
startActivity(i);
96+
break;
9297
}
9398
}
9499

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.charts.LineChart;
9+
import com.github.mikephil.charting.data.BarData;
10+
import com.github.mikephil.charting.data.LineData;
11+
import com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet;
12+
import com.github.mikephil.charting.data.realm.implementation.RealmLineDataSet;
13+
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
14+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
15+
import com.github.mikephil.charting.utils.ColorTemplate;
16+
import com.xxmassdeveloper.mpchartexample.R;
17+
18+
import java.util.ArrayList;
19+
20+
import io.realm.RealmResults;
21+
22+
/**
23+
* Created by Philipp Jahoda on 18/12/15.
24+
*/
25+
public class RealmWikiExample extends RealmBaseActivity {
26+
27+
private LineChart lineChart;
28+
private BarChart barChart;
29+
30+
@Override
31+
protected void onCreate(Bundle savedInstanceState) {
32+
super.onCreate(savedInstanceState);
33+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
34+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
35+
setContentView(R.layout.activity_realm_wiki);
36+
37+
lineChart = (LineChart) findViewById(R.id.lineChart);
38+
barChart = (BarChart) findViewById(R.id.barChart);
39+
setup(lineChart);
40+
setup(barChart);
41+
42+
lineChart.getAxisLeft().setDrawGridLines(false);
43+
lineChart.getXAxis().setDrawGridLines(false);
44+
barChart.getAxisLeft().setDrawGridLines(false);
45+
barChart.getXAxis().setDrawGridLines(false);
46+
}
47+
48+
@Override
49+
protected void onResume() {
50+
super.onResume(); // setup realm
51+
52+
mRealm.beginTransaction();
53+
54+
// write some demo-data into the realm.io database
55+
Score score1 = new Score(100f, 0, "Peter");
56+
mRealm.copyToRealm(score1);
57+
Score score2 = new Score(110f, 1, "Lisa");
58+
mRealm.copyToRealm(score2);
59+
Score score3 = new Score(130f, 2, "Dennis");
60+
mRealm.copyToRealm(score3);
61+
Score score4 = new Score(70f, 3, "Luke");
62+
mRealm.copyToRealm(score4);
63+
Score score5 = new Score(80f, 4, "Sarah");
64+
mRealm.copyToRealm(score5);
65+
66+
mRealm.commitTransaction();
67+
68+
// add data to the chart
69+
setData();
70+
}
71+
72+
private void setData() {
73+
74+
// LINE-CHART
75+
RealmResults<Score> results = mRealm.allObjects(Score.class);
76+
77+
RealmLineDataSet<Score> lineDataSet = new RealmLineDataSet<Score>(results, "totalScore", "scoreNr");
78+
lineDataSet.setDrawCubic(false);
79+
lineDataSet.setLabel("Realm LineDataSet");
80+
lineDataSet.setDrawCircleHole(false);
81+
lineDataSet.setColor(ColorTemplate.rgb("#FF5722"));
82+
lineDataSet.setCircleColor(ColorTemplate.rgb("#FF5722"));
83+
lineDataSet.setLineWidth(1.8f);
84+
lineDataSet.setCircleSize(3.6f);
85+
86+
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
87+
dataSets.add(lineDataSet);
88+
89+
LineData lineData = new LineData(results, "playerName", dataSets);
90+
styleData(lineData);
91+
92+
// set data
93+
lineChart.setData(lineData);
94+
lineChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
95+
96+
97+
// BAR-CHART
98+
RealmBarDataSet<Score> barDataSet = new RealmBarDataSet<Score>(results, "totalScore", "scoreNr");
99+
barDataSet.setColors(new int[]{ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4")});
100+
barDataSet.setLabel("Realm BarDataSet");
101+
102+
ArrayList<IBarDataSet> barDataSets = new ArrayList<IBarDataSet>();
103+
barDataSets.add(barDataSet);
104+
105+
BarData barData = new BarData(results, "playerName", barDataSets);
106+
styleData(barData);
107+
108+
barChart.setData(barData);
109+
barChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
110+
}
111+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.xxmassdeveloper.mpchartexample.realm;
2+
3+
4+
import io.realm.RealmObject;
5+
6+
/**
7+
* our data object
8+
*/
9+
public class Score extends RealmObject {
10+
11+
private float totalScore;
12+
13+
private int scoreNr;
14+
15+
private String playerName;
16+
17+
public Score() {
18+
}
19+
20+
public Score(float totalScore, int scoreNr, String playerName) {
21+
this.scoreNr = scoreNr;
22+
this.playerName = playerName;
23+
this.totalScore = totalScore;
24+
}
25+
26+
// all getters and setters...
27+
28+
public float getTotalScore() {
29+
return totalScore;
30+
}
31+
32+
public void setTotalScore(float totalScore) {
33+
this.totalScore = totalScore;
34+
}
35+
36+
public int getScoreNr() {
37+
return scoreNr;
38+
}
39+
40+
public void setScoreNr(int scoreNr) {
41+
this.scoreNr = scoreNr;
42+
}
43+
44+
public String getPlayerName() {
45+
return playerName;
46+
}
47+
48+
public void setPlayerName(String playerName) {
49+
this.playerName = playerName;
50+
}
51+
}

screenshots/realm_wiki.png

28.9 KB
Loading

0 commit comments

Comments
 (0)
X Tutup