Skip to content

Commit

Permalink
Add realm related example code
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Jul 1, 2016
1 parent 6a2e6e5 commit 0789628
Show file tree
Hide file tree
Showing 15 changed files with 1,129 additions and 4 deletions.
2 changes: 1 addition & 1 deletion MPChartExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ repositories {
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
//compile project(':MPChartLib-Realm') // clone "https://github.com/PhilJay/MPAndroidChart-Realm" to get this or uncomment the gradle dependency below:
//compile 'com.github.PhilJay:MPAndroidChart-Realm:v0.9.9'
compile 'com.github.PhilJay:MPAndroidChart-Realm:v1.0.0@aar'

compile project(':MPChartLib')
compile 'com.android.support:appcompat-v7:23.1.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.xxmassdeveloper.mpchartexample.StackedBarActivity;
import com.xxmassdeveloper.mpchartexample.StackedBarActivityNegative;
import com.xxmassdeveloper.mpchartexample.fragments.SimpleChartDemo;
import com.xxmassdeveloper.mpchartexample.realm.RealmMainActivity;

import java.util.ArrayList;

Expand Down Expand Up @@ -274,8 +275,8 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
startActivity(i);
break;
case 28:
//i = new Intent(this, RealmMainActivity.class);
//startActivity(i);
i = new Intent(this, RealmMainActivity.class);
startActivity(i);
break;
case 29:
i = new Intent(this, LineChartTime.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package com.xxmassdeveloper.mpchartexample.realm;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
* Created by Philipp Jahoda on 05/11/15.
*/
public abstract class RealmBaseActivity extends DemoBase {

protected Realm mRealm;

protected Typeface mTf;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Realm.io Examples");
}

protected void setup(Chart<?> chart) {

mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

// no description text
chart.setDescription("");
chart.setNoDataTextDescription("You need to provide data for the chart.");

// enable touch gestures
chart.setTouchEnabled(true);

if (chart instanceof BarLineChartBase) {

BarLineChartBase mChart = (BarLineChartBase) chart;

mChart.setDrawGridBackground(false);

// 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);

YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
leftAxis.setTypeface(mTf);
leftAxis.setTextSize(8f);
leftAxis.setTextColor(Color.DKGRAY);
leftAxis.setValueFormatter(new PercentFormatter());

XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTf);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(8f);
xAxis.setTextColor(Color.DKGRAY);

mChart.getAxisRight().setEnabled(false);
}
}

protected void styleData(ChartData data) {
data.setValueTypeface(mTf);
data.setValueTextSize(8f);
data.setValueTextColor(Color.DKGRAY);
data.setValueFormatter(new PercentFormatter());
}

@Override
protected void onResume() {
super.onResume();

// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
RealmConfiguration realmConfig = new RealmConfiguration.Builder(getApplicationContext()).build();
Realm.setDefaultConfiguration(realmConfig);

mRealm = Realm.getDefaultInstance();
}

@Override
protected void onPause() {
super.onPause();
mRealm.close();
}

protected void writeToDB(int objectCount) {

mRealm.beginTransaction();

mRealm.delete(RealmDemoData.class);

for (int i = 0; i < objectCount; i++) {

float value = 40f + (float) (Math.random() * 60f);

RealmDemoData d = new RealmDemoData(i, value);
mRealm.copyToRealm(d);
}

mRealm.commitTransaction();
}

protected void writeToDBStack(int objectCount) {

mRealm.beginTransaction();

mRealm.delete(RealmDemoData.class);

for (int i = 0; i < objectCount; i++) {

float val1 = 34f + (float) (Math.random() * 12.0f);
float val2 = 34f + (float) (Math.random() * 12.0f);
float[] stack = new float[]{val1, val2, 100 - val1 - val2};

RealmDemoData d = new RealmDemoData(i, stack);
mRealm.copyToRealm(d);
}

mRealm.commitTransaction();
}

protected void writeToDBCandle(int objectCount) {

mRealm.beginTransaction();

mRealm.delete(RealmDemoData.class);

for (int i = 0; i < objectCount; i++) {

float mult = 50;
float val = (float) (Math.random() * 40) + mult;

float high = (float) (Math.random() * 9) + 8f;
float low = (float) (Math.random() * 9) + 8f;

float open = (float) (Math.random() * 6) + 1f;
float close = (float) (Math.random() * 6) + 1f;

boolean even = i % 2 == 0;

RealmDemoData d = new RealmDemoData(i, val + high, val - low, even ? val + open : val - open,
even ? val - close : val + close);

mRealm.copyToRealm(d);
}

mRealm.commitTransaction();
}

protected void writeToDBBubble(int objectCount) {

mRealm.beginTransaction();

mRealm.delete(RealmDemoData.class);

for (int i = 0; i < objectCount; i++) {

float value = 30f + (float) (Math.random() * 100.0);
float size = 15f + (float) (Math.random() * 20.0);

RealmDemoData d = new RealmDemoData(i, value, size);
mRealm.copyToRealm(d);
}

mRealm.commitTransaction();
}

protected void writeToDBPie() {

mRealm.beginTransaction();

mRealm.delete(RealmDemoData.class);

float value1 = 15f + (float) (Math.random() * 8f);
float value2 = 15f + (float) (Math.random() * 8f);
float value3 = 15f + (float) (Math.random() * 8f);
float value4 = 15f + (float) (Math.random() * 8f);
float value5 = 100f - value1 - value2 - value3 - value4;

float[] values = new float[] { value1, value2, value3, value4, value5 };
String[] labels = new String[]{ "iOS", "Android", "WP 10", "BlackBerry", "Other"};

for (int i = 0; i < values.length; i++) {
RealmDemoData d = new RealmDemoData(values[i], labels[i]);
mRealm.copyToRealm(d);
}

mRealm.commitTransaction();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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.data.BarData;
import com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.R;
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;

import java.util.ArrayList;

import io.realm.RealmResults;

/**
* Created by Philipp Jahoda on 21/10/15.
*/
public class RealmDatabaseActivityBar extends RealmBaseActivity {

private BarChart mChart;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart_noseekbar);

mChart = (BarChart) findViewById(R.id.chart1);
setup(mChart);
}

@Override
protected void onResume() {
super.onResume(); // setup realm

// write some demo-data into the realm.io database
writeToDB(20);

// add data to the chart
setData();
}

private void setData() {

RealmResults<RealmDemoData> result = mRealm.where(RealmDemoData.class).findAll();

//RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "stackValues", "xIndex"); // normal entries
RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "xValue", "yValue"); // stacked entries
set.setColors(new int[] {ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4")});
set.setLabel("Realm BarDataSet");

ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set); // add the dataset

// create a data object with the dataset list
BarData data = new BarData(dataSets);
styleData(data);

// set data
mChart.setData(data);
mChart.setFitBars(true);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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.BubbleChart;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.realm.implementation.RealmBubbleDataSet;
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.R;
import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData;

import java.util.ArrayList;

import io.realm.RealmResults;

/**
* Created by Philipp Jahoda on 21/10/15.
*/
public class RealmDatabaseActivityBubble extends RealmBaseActivity {

private BubbleChart mChart;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_bubblechart_noseekbar);

mChart = (BubbleChart) findViewById(R.id.chart1);
setup(mChart);

mChart.getXAxis().setDrawGridLines(false);
mChart.getAxisLeft().setDrawGridLines(false);
mChart.setPinchZoom(true);
}

@Override
protected void onResume() {
super.onResume(); // setup realm

// write some demo-data into the realm.io database
writeToDBBubble(10);

// add data to the chart
setData();
}

private void setData() {

RealmResults<RealmDemoData> result = mRealm.where(RealmDemoData.class).findAll();

RealmBubbleDataSet<RealmDemoData> set = new RealmBubbleDataSet<RealmDemoData>(result, "xValue", "yValue", "bubbleSize");
set.setLabel("Realm BubbleDataSet");
set.setColors(ColorTemplate.COLORFUL_COLORS, 110);

ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
dataSets.add(set); // add the dataset

// create a data object with the dataset list
BubbleData data = new BubbleData(dataSets);
styleData(data);

// set data
mChart.setData(data);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
}
}
Loading

0 comments on commit 0789628

Please sign in to comment.