Skip to content

Commit 25f700a

Browse files
authored
Merge pull request #145 from AppDevNext/StaticRealtimeSample
Static realtime sample
2 parents 1ff5d3c + 9f04d23 commit 25f700a

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
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);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void setData(int count) {
136136
if (count == 100) // initial
137137
y = (valuesData[Math.round(x)]).floatValue() * 50 + 50;
138138
else
139-
y = (float) (Math.random() * 50 + 50);
139+
y = (float) (Math.random() * 50 + 50); // manually triggered
140140
values.add(new Entry(x, y)); // add one entry per hour
141141
}
142142

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class RealtimeLineChartActivity extends DemoBase implements
3333
OnChartValueSelectedListener {
3434

3535
private LineChart chart;
36+
Double[] sampleValues = DataTools.Companion.getValues(102);
3637

3738
@Override
3839
protected void onCreate(Bundle savedInstanceState) {
@@ -110,7 +111,8 @@ private void addEntry() {
110111
data.addDataSet(set);
111112
}
112113

113-
data.addEntry(new Entry(set.getEntryCount(), (float) (Math.random() * 40) + 30f), 0);
114+
int cycleValue = (int) (set.getEntryCount() % 100.0);
115+
data.addEntry(new Entry(set.getEntryCount(), (float) (sampleValues[cycleValue].floatValue() * 40) + 30f), 0);
114116
data.notifyDataChanged();
115117

116118
// let the chart know it's data has changed

0 commit comments

Comments
 (0)