Add realm wiki example code
This commit is contained in:
parent
3f79bb538b
commit
cedebdae6a
6 changed files with 188 additions and 0 deletions
|
@ -60,6 +60,7 @@
|
|||
<activity android:name="BarChartActivitySinus"></activity>
|
||||
<activity android:name="ScrollViewActivity"></activity>
|
||||
<activity android:name="StackedBarActivityNegative"></activity>
|
||||
<activity android:name=".realm.RealmWikiExample"></activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
20
MPChartExample/res/layout/activity_realm_wiki.xml
Normal file
20
MPChartExample/res/layout/activity_realm_wiki.xml
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/white"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.github.mikephil.charting.charts.LineChart
|
||||
android:id="@+id/lineChart"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1" />
|
||||
|
||||
<com.github.mikephil.charting.charts.BarChart
|
||||
android:id="@+id/barChart"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1" />
|
||||
|
||||
</LinearLayout>
|
|
@ -42,6 +42,7 @@ public class RealmMainActivity extends DemoBase implements AdapterView.OnItemCli
|
|||
objects.add(new ContentItem("Bubble Chart", "Creating a BubbleChart with Realm.io database"));
|
||||
objects.add(new ContentItem("Pie Chart", "Creating a PieChart with Realm.io database"));
|
||||
objects.add(new ContentItem("Radar Chart", "Creating a RadarChart with Realm.io database"));
|
||||
objects.add(new ContentItem("Realm Wiki", "This is the code related to the wiki entry about realm.io on the MPAndroidChart github page."));
|
||||
|
||||
MyAdapter adapter = new MyAdapter(this, objects);
|
||||
|
||||
|
@ -89,6 +90,10 @@ public class RealmMainActivity extends DemoBase implements AdapterView.OnItemCli
|
|||
i = new Intent(this, RealmDatabaseActivityRadar.class);
|
||||
startActivity(i);
|
||||
break;
|
||||
case 8:
|
||||
i = new Intent(this, RealmWikiExample.class);
|
||||
startActivity(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
package com.xxmassdeveloper.mpchartexample.realm;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.github.mikephil.charting.animation.Easing;
|
||||
import com.github.mikephil.charting.charts.BarChart;
|
||||
import com.github.mikephil.charting.charts.LineChart;
|
||||
import com.github.mikephil.charting.data.BarData;
|
||||
import com.github.mikephil.charting.data.LineData;
|
||||
import com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet;
|
||||
import com.github.mikephil.charting.data.realm.implementation.RealmLineDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
|
||||
import com.github.mikephil.charting.utils.ColorTemplate;
|
||||
import com.xxmassdeveloper.mpchartexample.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import io.realm.RealmResults;
|
||||
|
||||
/**
|
||||
* Created by Philipp Jahoda on 18/12/15.
|
||||
*/
|
||||
public class RealmWikiExample extends RealmBaseActivity {
|
||||
|
||||
private LineChart lineChart;
|
||||
private BarChart barChart;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
setContentView(R.layout.activity_realm_wiki);
|
||||
|
||||
lineChart = (LineChart) findViewById(R.id.lineChart);
|
||||
barChart = (BarChart) findViewById(R.id.barChart);
|
||||
setup(lineChart);
|
||||
setup(barChart);
|
||||
|
||||
lineChart.getAxisLeft().setDrawGridLines(false);
|
||||
lineChart.getXAxis().setDrawGridLines(false);
|
||||
barChart.getAxisLeft().setDrawGridLines(false);
|
||||
barChart.getXAxis().setDrawGridLines(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume(); // setup realm
|
||||
|
||||
mRealm.beginTransaction();
|
||||
|
||||
// write some demo-data into the realm.io database
|
||||
Score score1 = new Score(100f, 0, "Peter");
|
||||
mRealm.copyToRealm(score1);
|
||||
Score score2 = new Score(110f, 1, "Lisa");
|
||||
mRealm.copyToRealm(score2);
|
||||
Score score3 = new Score(130f, 2, "Dennis");
|
||||
mRealm.copyToRealm(score3);
|
||||
Score score4 = new Score(70f, 3, "Luke");
|
||||
mRealm.copyToRealm(score4);
|
||||
Score score5 = new Score(80f, 4, "Sarah");
|
||||
mRealm.copyToRealm(score5);
|
||||
|
||||
mRealm.commitTransaction();
|
||||
|
||||
// add data to the chart
|
||||
setData();
|
||||
}
|
||||
|
||||
private void setData() {
|
||||
|
||||
// LINE-CHART
|
||||
RealmResults<Score> results = mRealm.allObjects(Score.class);
|
||||
|
||||
RealmLineDataSet<Score> lineDataSet = new RealmLineDataSet<Score>(results, "totalScore", "scoreNr");
|
||||
lineDataSet.setDrawCubic(false);
|
||||
lineDataSet.setLabel("Realm LineDataSet");
|
||||
lineDataSet.setDrawCircleHole(false);
|
||||
lineDataSet.setColor(ColorTemplate.rgb("#FF5722"));
|
||||
lineDataSet.setCircleColor(ColorTemplate.rgb("#FF5722"));
|
||||
lineDataSet.setLineWidth(1.8f);
|
||||
lineDataSet.setCircleSize(3.6f);
|
||||
|
||||
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
|
||||
dataSets.add(lineDataSet);
|
||||
|
||||
LineData lineData = new LineData(results, "playerName", dataSets);
|
||||
styleData(lineData);
|
||||
|
||||
// set data
|
||||
lineChart.setData(lineData);
|
||||
lineChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
|
||||
|
||||
|
||||
// BAR-CHART
|
||||
RealmBarDataSet<Score> barDataSet = new RealmBarDataSet<Score>(results, "totalScore", "scoreNr");
|
||||
barDataSet.setColors(new int[]{ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4")});
|
||||
barDataSet.setLabel("Realm BarDataSet");
|
||||
|
||||
ArrayList<IBarDataSet> barDataSets = new ArrayList<IBarDataSet>();
|
||||
barDataSets.add(barDataSet);
|
||||
|
||||
BarData barData = new BarData(results, "playerName", barDataSets);
|
||||
styleData(barData);
|
||||
|
||||
barChart.setData(barData);
|
||||
barChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.xxmassdeveloper.mpchartexample.realm;
|
||||
|
||||
|
||||
import io.realm.RealmObject;
|
||||
|
||||
/**
|
||||
* our data object
|
||||
*/
|
||||
public class Score extends RealmObject {
|
||||
|
||||
private float totalScore;
|
||||
|
||||
private int scoreNr;
|
||||
|
||||
private String playerName;
|
||||
|
||||
public Score() {
|
||||
}
|
||||
|
||||
public Score(float totalScore, int scoreNr, String playerName) {
|
||||
this.scoreNr = scoreNr;
|
||||
this.playerName = playerName;
|
||||
this.totalScore = totalScore;
|
||||
}
|
||||
|
||||
// all getters and setters...
|
||||
|
||||
public float getTotalScore() {
|
||||
return totalScore;
|
||||
}
|
||||
|
||||
public void setTotalScore(float totalScore) {
|
||||
this.totalScore = totalScore;
|
||||
}
|
||||
|
||||
public int getScoreNr() {
|
||||
return scoreNr;
|
||||
}
|
||||
|
||||
public void setScoreNr(int scoreNr) {
|
||||
this.scoreNr = scoreNr;
|
||||
}
|
||||
|
||||
public String getPlayerName() {
|
||||
return playerName;
|
||||
}
|
||||
|
||||
public void setPlayerName(String playerName) {
|
||||
this.playerName = playerName;
|
||||
}
|
||||
}
|
BIN
screenshots/realm_wiki.png
Normal file
BIN
screenshots/realm_wiki.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
Loading…
Add table
Reference in a new issue