| package com.xxmassdeveloper.mpchartexample; |
| |
| import android.graphics.Typeface; |
| import android.os.Bundle; |
| import android.view.WindowManager; |
| |
| import com.github.mikephil.charting.animation.Easing; |
| import com.github.mikephil.charting.charts.LineChart; |
| import com.github.mikephil.charting.components.YAxis; |
| import com.github.mikephil.charting.data.LineData; |
| import com.github.mikephil.charting.data.realm.RealmLineDataSet; |
| import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; |
| import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| |
| import io.realm.Realm; |
| import io.realm.RealmConfiguration; |
| import io.realm.RealmResults; |
| |
| /** |
| * Created by Philipp Jahoda on 21/10/15. |
| */ |
| public class RealmDatabaseActivity extends DemoBase { |
| |
| private LineChart mChart; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| setContentView(R.layout.activity_linechart_noseekbar); |
| |
| mChart = (LineChart) findViewById(R.id.chart1); |
| mChart.setDrawGridBackground(false); |
| |
| // no description text |
| mChart.setDescription(""); |
| mChart.setNoDataTextDescription("You need to provide data for the chart."); |
| |
| // enable touch gestures |
| mChart.setTouchEnabled(true); |
| |
| // enable scaling and dragging |
| mChart.setDragEnabled(true); |
| mChart.setScaleEnabled(true); |
| |
| // if disabled, scaling can be done on x- and y-axis separately |
| mChart.setPinchZoom(false); |
| |
| Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| |
| YAxis leftAxis = mChart.getAxisLeft(); |
| leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines |
| leftAxis.setAxisMaxValue(220f); |
| leftAxis.setAxisMinValue(-50f); |
| leftAxis.setStartAtZero(false); |
| leftAxis.setTypeface(tf); |
| |
| mChart.getXAxis().setTypeface(tf); |
| |
| mChart.getAxisRight().setEnabled(false); |
| |
| // write some demo-data into the realm.io database |
| writeToDB(400); |
| |
| // add data |
| setData(); |
| |
| mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart); |
| } |
| |
| private void writeToDB(int objectCount) { |
| |
| RealmConfiguration config = new RealmConfiguration.Builder(this) |
| .name("myrealm.realm") |
| .build(); |
| |
| Realm.deleteRealm(config); |
| |
| Realm.setDefaultConfiguration(config); |
| |
| Realm realm = Realm.getInstance(config); |
| |
| realm.beginTransaction(); |
| |
| realm.clear(RealmDemoData.class); |
| |
| for(int i = 0; i < objectCount; i++) { |
| |
| RealmDemoData d = new RealmDemoData(30f + (float) (Math.random() * 100.0), i); |
| realm.copyToRealm(d); |
| } |
| |
| realm.commitTransaction(); |
| realm.close(); |
| } |
| |
| private void setData() { |
| |
| RealmConfiguration config = new RealmConfiguration.Builder(this) |
| .name("myrealm.realm") |
| .build(); |
| |
| Realm realm = Realm.getInstance(config); |
| RealmResults<RealmDemoData> result = realm.allObjects(RealmDemoData.class); |
| |
| ArrayList<String> xVals = new ArrayList<String>(); |
| for (int i = 0; i < result.size(); i++) { |
| xVals.add((i) + ""); |
| } |
| |
| RealmLineDataSet<RealmDemoData> set = new RealmLineDataSet<RealmDemoData>(result, "value", "xIndex"); |
| set.setValueTextSize(9f); |
| |
| ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>(); |
| dataSets.add(set); // add the dataset |
| |
| // create a data object with the dataset list |
| LineData data = new LineData(xVals, dataSets); |
| |
| // set data |
| mChart.setData(data); |
| |
| realm.close(); |
| } |
| } |