|
| 1 | +package com.xxmassdeveloper.mpchartexample.realm; |
| 2 | + |
| 3 | +import android.graphics.Color; |
| 4 | +import android.graphics.Typeface; |
| 5 | +import android.os.Bundle; |
| 6 | + |
| 7 | +import com.github.mikephil.charting.charts.BarLineChartBase; |
| 8 | +import com.github.mikephil.charting.charts.Chart; |
| 9 | +import com.github.mikephil.charting.components.XAxis; |
| 10 | +import com.github.mikephil.charting.components.YAxis; |
| 11 | +import com.github.mikephil.charting.data.ChartData; |
| 12 | +import com.github.mikephil.charting.formatter.PercentFormatter; |
| 13 | +import com.xxmassdeveloper.mpchartexample.custom.RealmDemoData; |
| 14 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 15 | + |
| 16 | +import io.realm.Realm; |
| 17 | +import io.realm.RealmConfiguration; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by Philipp Jahoda on 05/11/15. |
| 21 | + */ |
| 22 | +public abstract class RealmBaseActivity extends DemoBase { |
| 23 | + |
| 24 | + protected Realm mRealm; |
| 25 | + |
| 26 | + protected Typeface mTf; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void onCreate(Bundle savedInstanceState) { |
| 30 | + super.onCreate(savedInstanceState); |
| 31 | + setTitle("Realm.io Examples"); |
| 32 | + } |
| 33 | + |
| 34 | + protected void setup(Chart<?> chart) { |
| 35 | + |
| 36 | + mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| 37 | + |
| 38 | + // no description text |
| 39 | + chart.setDescription(""); |
| 40 | + chart.setNoDataTextDescription("You need to provide data for the chart."); |
| 41 | + |
| 42 | + // enable touch gestures |
| 43 | + chart.setTouchEnabled(true); |
| 44 | + |
| 45 | + if (chart instanceof BarLineChartBase) { |
| 46 | + |
| 47 | + BarLineChartBase mChart = (BarLineChartBase) chart; |
| 48 | + |
| 49 | + mChart.setDrawGridBackground(false); |
| 50 | + |
| 51 | + // enable scaling and dragging |
| 52 | + mChart.setDragEnabled(true); |
| 53 | + mChart.setScaleEnabled(true); |
| 54 | + |
| 55 | + // if disabled, scaling can be done on x- and y-axis separately |
| 56 | + mChart.setPinchZoom(false); |
| 57 | + |
| 58 | + YAxis leftAxis = mChart.getAxisLeft(); |
| 59 | + leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines |
| 60 | + leftAxis.setTypeface(mTf); |
| 61 | + leftAxis.setTextSize(8f); |
| 62 | + leftAxis.setTextColor(Color.DKGRAY); |
| 63 | + leftAxis.setValueFormatter(new PercentFormatter()); |
| 64 | + |
| 65 | + XAxis xAxis = mChart.getXAxis(); |
| 66 | + xAxis.setTypeface(mTf); |
| 67 | + xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); |
| 68 | + xAxis.setTextSize(8f); |
| 69 | + xAxis.setTextColor(Color.DKGRAY); |
| 70 | + |
| 71 | + mChart.getAxisRight().setEnabled(false); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + protected void styleData(ChartData data) { |
| 76 | + data.setValueTypeface(mTf); |
| 77 | + data.setValueTextSize(8f); |
| 78 | + data.setValueTextColor(Color.DKGRAY); |
| 79 | + data.setValueFormatter(new PercentFormatter()); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected void onResume() { |
| 84 | + super.onResume(); |
| 85 | + |
| 86 | + // Create a RealmConfiguration that saves the Realm file in the app's "files" directory. |
| 87 | + RealmConfiguration realmConfig = new RealmConfiguration.Builder(getApplicationContext()).build(); |
| 88 | + Realm.setDefaultConfiguration(realmConfig); |
| 89 | + |
| 90 | + mRealm = Realm.getDefaultInstance(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void onPause() { |
| 95 | + super.onPause(); |
| 96 | + mRealm.close(); |
| 97 | + } |
| 98 | + |
| 99 | + protected void writeToDB(int objectCount) { |
| 100 | + |
| 101 | + mRealm.beginTransaction(); |
| 102 | + |
| 103 | + mRealm.delete(RealmDemoData.class); |
| 104 | + |
| 105 | + for (int i = 0; i < objectCount; i++) { |
| 106 | + |
| 107 | + float value = 40f + (float) (Math.random() * 60f); |
| 108 | + |
| 109 | + RealmDemoData d = new RealmDemoData(i, value); |
| 110 | + mRealm.copyToRealm(d); |
| 111 | + } |
| 112 | + |
| 113 | + mRealm.commitTransaction(); |
| 114 | + } |
| 115 | + |
| 116 | + protected void writeToDBStack(int objectCount) { |
| 117 | + |
| 118 | + mRealm.beginTransaction(); |
| 119 | + |
| 120 | + mRealm.delete(RealmDemoData.class); |
| 121 | + |
| 122 | + for (int i = 0; i < objectCount; i++) { |
| 123 | + |
| 124 | + float val1 = 34f + (float) (Math.random() * 12.0f); |
| 125 | + float val2 = 34f + (float) (Math.random() * 12.0f); |
| 126 | + float[] stack = new float[]{val1, val2, 100 - val1 - val2}; |
| 127 | + |
| 128 | + RealmDemoData d = new RealmDemoData(i, stack); |
| 129 | + mRealm.copyToRealm(d); |
| 130 | + } |
| 131 | + |
| 132 | + mRealm.commitTransaction(); |
| 133 | + } |
| 134 | + |
| 135 | + protected void writeToDBCandle(int objectCount) { |
| 136 | + |
| 137 | + mRealm.beginTransaction(); |
| 138 | + |
| 139 | + mRealm.delete(RealmDemoData.class); |
| 140 | + |
| 141 | + for (int i = 0; i < objectCount; i++) { |
| 142 | + |
| 143 | + float mult = 50; |
| 144 | + float val = (float) (Math.random() * 40) + mult; |
| 145 | + |
| 146 | + float high = (float) (Math.random() * 9) + 8f; |
| 147 | + float low = (float) (Math.random() * 9) + 8f; |
| 148 | + |
| 149 | + float open = (float) (Math.random() * 6) + 1f; |
| 150 | + float close = (float) (Math.random() * 6) + 1f; |
| 151 | + |
| 152 | + boolean even = i % 2 == 0; |
| 153 | + |
| 154 | + RealmDemoData d = new RealmDemoData(i, val + high, val - low, even ? val + open : val - open, |
| 155 | + even ? val - close : val + close); |
| 156 | + |
| 157 | + mRealm.copyToRealm(d); |
| 158 | + } |
| 159 | + |
| 160 | + mRealm.commitTransaction(); |
| 161 | + } |
| 162 | + |
| 163 | + protected void writeToDBBubble(int objectCount) { |
| 164 | + |
| 165 | + mRealm.beginTransaction(); |
| 166 | + |
| 167 | + mRealm.delete(RealmDemoData.class); |
| 168 | + |
| 169 | + for (int i = 0; i < objectCount; i++) { |
| 170 | + |
| 171 | + float value = 30f + (float) (Math.random() * 100.0); |
| 172 | + float size = 15f + (float) (Math.random() * 20.0); |
| 173 | + |
| 174 | + RealmDemoData d = new RealmDemoData(i, value, size); |
| 175 | + mRealm.copyToRealm(d); |
| 176 | + } |
| 177 | + |
| 178 | + mRealm.commitTransaction(); |
| 179 | + } |
| 180 | + |
| 181 | + protected void writeToDBPie() { |
| 182 | + |
| 183 | + mRealm.beginTransaction(); |
| 184 | + |
| 185 | + mRealm.delete(RealmDemoData.class); |
| 186 | + |
| 187 | + float value1 = 15f + (float) (Math.random() * 8f); |
| 188 | + float value2 = 15f + (float) (Math.random() * 8f); |
| 189 | + float value3 = 15f + (float) (Math.random() * 8f); |
| 190 | + float value4 = 15f + (float) (Math.random() * 8f); |
| 191 | + float value5 = 100f - value1 - value2 - value3 - value4; |
| 192 | + |
| 193 | + float[] values = new float[] { value1, value2, value3, value4, value5 }; |
| 194 | + String[] labels = new String[]{ "iOS", "Android", "WP 10", "BlackBerry", "Other"}; |
| 195 | + |
| 196 | + for (int i = 0; i < values.length; i++) { |
| 197 | + RealmDemoData d = new RealmDemoData(values[i], labels[i]); |
| 198 | + mRealm.copyToRealm(d); |
| 199 | + } |
| 200 | + |
| 201 | + mRealm.commitTransaction(); |
| 202 | + } |
| 203 | +} |
0 commit comments