Skip to content

Commit 2811687

Browse files
committed
several samples with fix data
1 parent 9bf61a6 commit 2811687

34 files changed

+122
-90
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/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

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

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

22-
private val VAL_100 = arrayOf(
22+
private val VAL_102 = arrayOf(
2323
0.31704906,
2424
0.11076527,
2525
0.67421365,
@@ -119,7 +119,9 @@ class DataTools {
119119
0.47154334,
120120
0.43126026,
121121
0.65412974,
122-
0.7053179
122+
0.7053179,
123+
0.77,
124+
0.69
123125
)
124126

125127
private val VAL_FIX = arrayOf(
@@ -131,7 +133,14 @@ class DataTools {
131133
44.768654, -25.790316, 5.9754066, 99.64748, 141.99321, -17.990795, 38.272446
132134
)
133135

134-
fun getValues(size: Int) = VAL_100.copyOf(size)
136+
fun getValues(size: Int) = VAL_102.copyOf(size)
137+
138+
fun getMuchValues(size: Int): Array<Double?> {
139+
var result = VAL_102.copyOf(VAL_102.size)
140+
while (result.size < size)
141+
result = result.plus(VAL_102.copyOf(VAL_102.size))
142+
return result
143+
}
135144

136145
fun setData(context: Context, lineChart: LineChart, count: Int = VAL_COUNT, range: Float = VAL_RANGE) {
137146
Log.d("setData", "$count= range=$range")

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* This works by inverting the background and desired "fill" color. First, we draw the fill color
2828
* that we want between the lines as the actual background of the chart. Then, we fill the area
2929
* above the highest line and the area under the lowest line with the desired background color.
30-
*
3130
* This method makes it look like we filled the area between the lines, but really we are filling
3231
* the area OUTSIDE the lines!
3332
*/
@@ -75,24 +74,25 @@ protected void onCreate(Bundle savedInstanceState) {
7574
chart.getAxisRight().setEnabled(false);
7675

7776
// add data
78-
setData(100, 60);
77+
setData(60);
7978

8079
chart.invalidate();
8180
}
8281

83-
private void setData(int count, float range) {
84-
82+
private void setData(float range) {
83+
int count = 100;
8584
ArrayList<Entry> values1 = new ArrayList<>();
85+
Double[] sampleValues = DataTools.Companion.getValues(count + 2);
8686

8787
for (int i = 0; i < count; i++) {
88-
float val = (float) (Math.random() * range) + 50;
88+
float val = (float) (sampleValues[i].floatValue() * range) + 50;
8989
values1.add(new Entry(i, val));
9090
}
9191

9292
ArrayList<Entry> values2 = new ArrayList<>();
9393

9494
for (int i = 0; i < count; i++) {
95-
float val = (float) (Math.random() * range) + 450;
95+
float val = (float) (sampleValues[i+1].floatValue() * range) + 450;
9696
values2.add(new Entry(i, val));
9797
}
9898

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected void onCreate(Bundle savedInstanceState) {
7171
chart.setRotationAngle(180f);
7272
chart.setCenterTextOffset(0, -20);
7373

74-
setData(4, 100);
74+
setData(100);
7575

7676
chart.animateY(1400, Easing.EaseInOutQuad);
7777

@@ -90,12 +90,13 @@ protected void onCreate(Bundle savedInstanceState) {
9090
chart.setEntryLabelTextSize(12f);
9191
}
9292

93-
private void setData(int count, float range) {
94-
93+
private void setData(float range) {
94+
int count = 4;
9595
ArrayList<PieEntry> values = new ArrayList<>();
96+
Double[] sampleValues = DataTools.Companion.getValues(count);
9697

9798
for (int i = 0; i < count; i++) {
98-
values.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i % parties.length]));
99+
values.add(new PieEntry((float) ((sampleValues[i].floatValue() * range) + range / 5), parties[i % parties.length]));
99100
}
100101

101102
PieDataSet dataSet = new PieDataSet(values, "Election Results");

0 commit comments

Comments
 (0)