Skip to content

Commit 6f0e284

Browse files
authored
Merge pull request #143 from AppDevNext/ExtractSetData
Extract setData to Kotlin and use fix sample data
2 parents 1fcf6ea + 2811687 commit 6f0e284

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+367
-194
lines changed

MPChartExample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</intent-filter>
2020
</activity>
2121
<activity android:name="LineChartActivity1" />
22-
<activity android:name="LineChartActivity2" />
22+
<activity android:name=".LineChartDualAxisActivity" />
2323
<activity android:name="LineChartTime" />
2424
<activity android:name="BarChartActivity" />
2525
<activity android:name="HorizontalBarChartActivity" />

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
public class AnotherBarActivity extends DemoBase implements OnSeekBarChangeListener {
3131

32+
private static final int DEFAULT_VALUE = 10;
3233
private BarChart chart;
3334
private SeekBar seekBarX, seekBarY;
3435
private TextView tvX, tvY;
@@ -72,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) {
7273
chart.getAxisLeft().setDrawGridLines(false);
7374

7475
// setting data
75-
seekBarX.setProgress(10);
76+
seekBarX.setProgress(DEFAULT_VALUE);
7677
seekBarY.setProgress(100);
7778

7879
// add a nice and smooth animation
@@ -88,10 +89,11 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
8889
tvY.setText(String.valueOf(seekBarY.getProgress()));
8990

9091
ArrayList<BarEntry> values = new ArrayList<>();
92+
Double[] sampleValues = DataTools.Companion.getValues(100);
9193

9294
for (int i = 0; i < seekBarX.getProgress(); i++) {
9395
float multi = (seekBarY.getProgress() + 1);
94-
float val = (float) (Math.random() * multi) + multi / 3;
96+
float val = (float) (sampleValues[i].floatValue() * multi) + multi / 3;
9597
values.add(new BarEntry(i, val));
9698
}
9799

@@ -100,7 +102,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
100102
if (chart.getData() != null &&
101103
chart.getData().getDataSetCount() > 0) {
102104
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
103-
set1.setValues(values);
105+
set1.setEntries(values);
104106
chart.getData().notifyDataChanged();
105107
chart.notifyDataSetChanged();
106108
} else {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ private void setData(int count, float range) {
141141
float start = 1f;
142142

143143
ArrayList<BarEntry> values = new ArrayList<>();
144+
Double[] sampleValues = DataTools.Companion.getValues(100);
144145

145146
for (int i = (int) start; i < start + count; i++) {
146-
float val = (float) (Math.random() * (range + 1));
147+
float val = (float) (sampleValues[i].floatValue() * (range + 1));
147148

148-
if (Math.random() * 100 < 25) {
149+
if (val * 100 < 25) {
149150
values.add(new BarEntry(i, val, getResources().getDrawable(R.drawable.star)));
150151
} else {
151152
values.add(new BarEntry(i, val));

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,13 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
138138
ArrayList<BarEntry> values4 = new ArrayList<>();
139139

140140
float randomMultiplier = seekBarY.getProgress() * 100000f;
141+
Double[] sampleValues = DataTools.Companion.getValues(100 + 2);
141142

142143
for (int i = startYear; i < endYear; i++) {
143-
values1.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
144-
values2.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
145-
values3.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
146-
values4.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
144+
values1.add(new BarEntry(i, (float) (sampleValues[i - startYear].floatValue() * randomMultiplier)));
145+
values2.add(new BarEntry(i, (float) (sampleValues[i - startYear + 1].floatValue() * randomMultiplier)));
146+
values3.add(new BarEntry(i, (float) (sampleValues[i - startYear + 2].floatValue() * randomMultiplier)));
147+
values4.add(new BarEntry(i, (float) (sampleValues[i - startYear].floatValue() * randomMultiplier)));
147148
}
148149

149150
BarDataSet set1, set2, set3, set4;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
110110
ArrayList<BubbleEntry> values1 = new ArrayList<>();
111111
ArrayList<BubbleEntry> values2 = new ArrayList<>();
112112
ArrayList<BubbleEntry> values3 = new ArrayList<>();
113+
Double[] sampleValues = DataTools.Companion.getValues(100);
113114

114115
for (int i = 0; i < count; i++) {
115-
values1.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range), getResources().getDrawable(R.drawable.star)));
116-
values2.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range), getResources().getDrawable(R.drawable.star)));
117-
values3.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range)));
116+
values1.add(new BubbleEntry(i, (float) (sampleValues[i+1] * range), (float) (sampleValues[i].floatValue() * range), getResources().getDrawable(R.drawable.star)));
117+
values2.add(new BubbleEntry(i, (float) (sampleValues[i+2] * range), (float) (sampleValues[i+1].floatValue() * range), getResources().getDrawable(R.drawable.star)));
118+
values3.add(new BubbleEntry(i, (float) (sampleValues[i] * range), (float) (sampleValues[i+2].floatValue() * range)));
118119
}
119120

120121
// create a dataset and give it a type

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,17 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
100100
chart.resetTracking();
101101

102102
ArrayList<CandleEntry> values = new ArrayList<>();
103+
Double[] sampleValues = DataTools.Companion.getValues(100);
103104

104105
for (int i = 0; i < progress; i++) {
105106
float multi = (seekBarY.getProgress() + 1);
106-
float val = (float) (Math.random() * 40) + multi;
107+
float val = (float) (sampleValues[i].floatValue() * 40) + multi;
107108

108-
float high = (float) (Math.random() * 9) + 8f;
109-
float low = (float) (Math.random() * 9) + 8f;
109+
float high = (float) (sampleValues[i].floatValue() * 9) + 8f;
110+
float low = (float) (sampleValues[i].floatValue() * 8) + 8f;
110111

111-
float open = (float) (Math.random() * 6) + 1f;
112-
float close = (float) (Math.random() * 6) + 1f;
112+
float open = (float) (sampleValues[i].floatValue() * 6) + 1f;
113+
float close = (float) (sampleValues[i].floatValue() * 7) + 1f;
113114

114115
boolean even = i % 2 == 0;
115116

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/CubicLineChartActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ protected void onCreate(Bundle savedInstanceState) {
105105
private void setData(int count, float range) {
106106

107107
ArrayList<Entry> values = new ArrayList<>();
108+
Double[] sampleValues = DataTools.Companion.getValues(100);
108109

109110
for (int i = 0; i < count; i++) {
110-
float val = (float) (Math.random() * (range + 1)) + 20;
111+
float val = (float) (sampleValues[i].floatValue() * (range + 1)) + 20;
111112
values.add(new Entry(i, val));
112113
}
113114

0 commit comments

Comments
 (0)