|
| 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 | +} |
0 commit comments