Skip to content

Commit 9f04d23

Browse files
committed
Static dynamic sample
1 parent 6ad316d commit 9f04d23

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
public class DynamicalAddingActivity extends DemoBase implements OnChartValueSelectedListener {
3030

3131
private LineChart chart;
32+
Double[] sampleValues = DataTools.Companion.getValues(102);
3233

3334
@Override
3435
protected void onCreate(Bundle savedInstanceState) {
@@ -70,11 +71,14 @@ private void addEntry() {
7071
data.addDataSet(set);
7172
}
7273

73-
int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount());
74-
ILineDataSet randomSet = data.getDataSetByIndex(randomDataSetIndex);
75-
float value = (float) (Math.random() * 50) + 50f * (randomDataSetIndex + 1);
74+
int lastDataSetIndex = data.getDataSetCount() - 1; // add data only to the last
75+
ILineDataSet lastSet = data.getDataSetByIndex(lastDataSetIndex);
7676

77-
data.addEntry(new Entry(randomSet.getEntryCount(), value), randomDataSetIndex);
77+
int cycleValue = (int) (lastSet.getEntryCount() % 100.0);
78+
79+
float value = (float) (sampleValues[cycleValue].floatValue() * 50) + 50f * (lastDataSetIndex + 1);
80+
81+
data.addEntry(new Entry(lastSet.getEntryCount(), value), lastDataSetIndex);
7882
data.notifyDataChanged();
7983

8084
// let the chart know it's data has changed
@@ -123,7 +127,9 @@ private void addDataSet() {
123127
ArrayList<Entry> values = new ArrayList<>();
124128

125129
for (int i = 0; i < amount; i++) {
126-
values.add(new Entry(i, (float) (Math.random() * 50f) + 50f * count));
130+
int cycleValue = (int) (i % 100.0);
131+
132+
values.add(new Entry(i, (float) (sampleValues[cycleValue].floatValue() * 50f) + 50f * count));
127133
}
128134

129135
LineDataSet set = new LineDataSet(values, "DataSet " + count);

0 commit comments

Comments
 (0)