Skip to content

Commit 47eb012

Browse files
committed
Combined sample with fix data
1 parent 1f1f8c4 commit 47eb012

File tree

4 files changed

+126
-16
lines changed

4 files changed

+126
-16
lines changed

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class CombinedChartActivity extends DemoBase {
4242

4343
private CombinedChart chart;
4444
private final int sampleCount = 12;
45+
Double[] values = DataTools.Companion.getValues(sampleCount * 2);
4546

4647
@Override
4748
protected void onCreate(Bundle savedInstanceState) {
@@ -112,7 +113,7 @@ private LineData generateLineData() {
112113
ArrayList<Entry> entries = new ArrayList<>();
113114

114115
for (int index = 0; index < sampleCount; index++)
115-
entries.add(new Entry(index + 0.5f, getRandom(15, 5)));
116+
entries.add(new Entry(index + 0.5f, values[index].floatValue() * 15 + 5));
116117

117118
LineDataSet set = new LineDataSet(entries, "Line DataSet");
118119
set.setColor(Color.rgb(240, 238, 70));
@@ -137,10 +138,10 @@ private BarData generateBarData() {
137138
ArrayList<BarEntry> entries2 = new ArrayList<>();
138139

139140
for (int index = 0; index < sampleCount; index++) {
140-
entries1.add(new BarEntry(0, getRandom(25, 25)));
141+
entries1.add(new BarEntry(0, values[index].floatValue() * 25 + 25));
141142

142143
// stacked
143-
entries2.add(new BarEntry(0, new float[]{getRandom(13, 12), getRandom(13, 12)}));
144+
entries2.add(new BarEntry(0, new float[]{values[index].floatValue() * 13 + 12, values[index].floatValue() * 13 + 12}));
144145
}
145146

146147
BarDataSet set1 = new BarDataSet(entries1, "Bar 1");
@@ -177,7 +178,7 @@ private ScatterData generateScatterData() {
177178
ArrayList<Entry> entries = new ArrayList<>();
178179

179180
for (float index = 0; index < sampleCount; index += 0.5f)
180-
entries.add(new Entry(index + 0.25f, getRandom(10, 55)));
181+
entries.add(new Entry(index + 0.25f, values[Math.round(index*2)].floatValue() * 10 + 55));
181182

182183
ScatterDataSet set = new ScatterDataSet(entries, "Scatter DataSet");
183184
set.setColors(ColorTemplate.MATERIAL_COLORS);
@@ -216,8 +217,8 @@ private BubbleData generateBubbleData() {
216217
ArrayList<BubbleEntry> entries = new ArrayList<>();
217218

218219
for (int index = 0; index < sampleCount; index++) {
219-
float y = getRandom(10, 105);
220-
float size = getRandom(100, 105);
220+
float y = values[index].floatValue() * 10 + 105;
221+
float size = values[index].floatValue() * 100 + 105;
221222
entries.add(new BubbleEntry(index + 0.5f, y, size));
222223
}
223224

@@ -266,7 +267,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
266267
break;
267268
}
268269
case R.id.actionRemoveDataSet: {
269-
int rnd = (int) getRandom(chart.getData().getDataSetCount(), 0);
270+
int rnd = (int) values[sampleCount].floatValue() * chart.getData().getDataSetCount();
270271
chart.getData().removeDataSet(chart.getData().getDataSetByIndex(rnd));
271272
chart.getData().notifyDataChanged();
272273
chart.notifyDataSetChanged();

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/DataTools.kt

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,109 @@ class DataTools {
1919
private const val VAL_COUNT = 45
2020
private const val VAL_RANGE = 180f
2121

22+
private val VAL_100 = arrayOf(
23+
0.31704906,
24+
0.11076527,
25+
0.67421365,
26+
0.30298668,
27+
0.37076905,
28+
0.21838248,
29+
0.7565924,
30+
0.8588904,
31+
0.33838162,
32+
0.63905376,
33+
0.052202724,
34+
0.3856282,
35+
0.73625267,
36+
0.007399183,
37+
0.5608851,
38+
0.8627598,
39+
0.331517,
40+
0.4500423,
41+
0.7748036,
42+
0.09964817,
43+
0.96290344,
44+
0.7727165,
45+
0.50386846,
46+
0.1740686,
47+
0.6228984,
48+
0.113764636,
49+
0.63975865,
50+
0.23871686,
51+
0.98804605,
52+
0.88095695,
53+
0.963798,
54+
0.20052797,
55+
0.9914091,
56+
0.047342073,
57+
0.7767942,
58+
0.92713374,
59+
0.21969187,
60+
0.17188361,
61+
0.08304611,
62+
0.31545073,
63+
0.8872535,
64+
0.8319247,
65+
0.29608163,
66+
0.7693036,
67+
0.8902154,
68+
0.5070619,
69+
0.95028555,
70+
0.376154,
71+
0.9760939,
72+
0.33779907,
73+
0.7004318,
74+
0.34741813,
75+
0.37786564,
76+
0.59739006,
77+
0.9702337,
78+
0.09252104,
79+
0.35609993,
80+
0.16156897,
81+
0.97137845,
82+
0.6337493,
83+
0.9687681,
84+
0.738406,
85+
0.456114,
86+
0.9371279,
87+
0.045567524,
88+
0.12931462,
89+
0.48930678,
90+
0.13396108,
91+
0.78207386,
92+
0.3322763,
93+
0.7299913,
94+
0.53834146,
95+
0.58876187,
96+
0.86963767,
97+
0.19778427,
98+
0.827845,
99+
0.83091146,
100+
0.3253732,
101+
0.26613656,
102+
0.7477944,
103+
0.8776427,
104+
0.7104973,
105+
0.6722489,
106+
0.2238866,
107+
0.441799,
108+
0.9403467,
109+
0.6625779,
110+
0.8236447,
111+
0.53712654,
112+
0.40374467,
113+
0.0032116973,
114+
0.09406879,
115+
0.5051711,
116+
0.33159724,
117+
0.47599393,
118+
0.42102405,
119+
0.47154334,
120+
0.43126026,
121+
0.65412974,
122+
0.7053179
123+
)
124+
22125
private val VAL_FIX = arrayOf(
23126
94.84043, -19.610321, 34.980606, 137.20502, 3.3113098, 18.506958,
24127
72.16055, 36.291832, 135.97142, 122.0381, 85.873055, 68.582016, -13.099461, 85.85466,
@@ -28,6 +131,8 @@ class DataTools {
28131
44.768654, -25.790316, 5.9754066, 99.64748, 141.99321, -17.990795, 38.272446
29132
)
30133

134+
fun getValues(size: Int) = VAL_100.copyOf(size)
135+
31136
fun setData(context: Context, lineChart: LineChart, count: Int = VAL_COUNT, range: Float = VAL_RANGE) {
32137
Log.d("setData", "$count= range=$range")
33138
val values = ArrayList<Entry>()

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,24 @@ public String getFormattedValue(float value, AxisBase axis) {
119119
rightAxis.setEnabled(false);
120120
}
121121

122-
private void setData(int count, float range) {
122+
private void setData(int count) {
123123

124124
// now in hours
125-
long now = TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis());
125+
long now = 0; //470044; //TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis());
126126

127127
ArrayList<Entry> values = new ArrayList<>();
128128

129129
// count = hours
130130
float to = now + count;
131131

132+
Double[] valuesData = DataTools.Companion.getValues(Math.round(to));
132133
// increment by 1 hour
133134
for (float x = now; x < to; x++) {
134-
135-
float y = getRandom(range, 50);
135+
float y;
136+
if (count == 100) // initial
137+
y = (valuesData[Math.round(x)]).floatValue() * 50 + 50;
138+
else
139+
y = (float) (Math.random() * 50 + 50);
136140
values.add(new Entry(x, y)); // add one entry per hour
137141
}
138142

@@ -299,7 +303,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
299303

300304
tvX.setText(String.valueOf(seekBarX.getProgress()));
301305

302-
setData(seekBarX.getProgress(), 50);
306+
setData(seekBarX.getProgress());
303307

304308
// redraw
305309
chart.invalidate();

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/notimportant/DemoBase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
import android.os.Bundle;
88
import androidx.annotation.NonNull;
99
import androidx.annotation.Nullable;
10+
11+
import com.github.mikephil.charting.data.BubbleEntry;
1012
import com.google.android.material.snackbar.Snackbar;
1113

1214
import androidx.appcompat.app.AppCompatActivity;
1315
import androidx.core.app.ActivityCompat;
16+
17+
import android.util.Log;
1418
import android.view.View;
1519
import android.widget.Toast;
1620

@@ -48,10 +52,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4852
tfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
4953
}
5054

51-
protected float getRandom(float range, float start) {
52-
return (float) (Math.random() * range) + start;
53-
}
54-
5555
@Override
5656
public void onBackPressed() {
5757
super.onBackPressed();

0 commit comments

Comments
 (0)