Skip to content

Commit 26f8f65

Browse files
committed
Introducing the clearValues() method that only removes entries but not x-values.
1 parent 4484809 commit 26f8f65

4 files changed

Lines changed: 43 additions & 14 deletions

File tree

MPChartExample/res/menu/realtime.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
android:id="@+id/actionAdd"
66
android:title="Feed new Entry">
77
</item>
8+
<item
9+
android:id="@+id/actionClear"
10+
android:title="Clear Values">
11+
</item>
812
</menu>

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.view.Menu;
99
import android.view.MenuItem;
1010
import android.view.WindowManager;
11+
import android.widget.Toast;
1112

1213
import com.github.mikephil.charting.charts.LineChart;
1314
import com.github.mikephil.charting.components.Legend;
@@ -61,7 +62,7 @@ protected void onCreate(Bundle savedInstanceState) {
6162

6263
LineData data = new LineData();
6364
data.setValueTextColor(Color.WHITE);
64-
65+
6566
// add empty data
6667
mChart.setData(data);
6768

@@ -87,7 +88,7 @@ protected void onCreate(Bundle savedInstanceState) {
8788
leftAxis.setTextColor(Color.WHITE);
8889
leftAxis.setAxisMaxValue(120f);
8990
leftAxis.setDrawGridLines(true);
90-
91+
9192
YAxis rightAxis = mChart.getAxisRight();
9293
rightAxis.setEnabled(false);
9394
}
@@ -106,12 +107,17 @@ public boolean onOptionsItemSelected(MenuItem item) {
106107
addEntry();
107108
break;
108109
}
110+
case R.id.actionClear: {
111+
mChart.clearValues();
112+
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
113+
break;
114+
}
109115
}
110116
return true;
111117
}
112118

113119
private int year = 15;
114-
120+
115121
private void addEntry() {
116122

117123
LineData data = mChart.getData();
@@ -127,24 +133,26 @@ private void addEntry() {
127133
}
128134

129135
// add a new x-value first
130-
data.addXValue(mMonths[data.getXValCount() % 12] + " " + (year + data.getXValCount() / 12));
136+
data.addXValue(mMonths[data.getXValCount() % 12] + " "
137+
+ (year + data.getXValCount() / 12));
131138
data.addEntry(new Entry((float) (Math.random() * 40) + 40f, set.getEntryCount()), 0);
132139

133140
// let the chart know it's data has changed
134141
mChart.notifyDataSetChanged();
135142

136143
// limit the number of visible entries
137-
mChart.setVisibleXRange(6);
144+
mChart.setVisibleXRange(6);
138145
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
139-
140-
// move to the latest entry
141-
mChart.moveViewToX(data.getXValCount()-7);
142-
143-
// this automatically refreshes the chart (calls invalidate())
144-
// mChart.moveViewTo(data.getXValCount()-7, 55f, AxisDependency.LEFT);
146+
147+
// move to the latest entry
148+
mChart.moveViewToX(data.getXValCount() - 7);
149+
150+
// this automatically refreshes the chart (calls invalidate())
151+
// mChart.moveViewTo(data.getXValCount()-7, 55f,
152+
// AxisDependency.LEFT);
145153

146154
// redraw the chart
147-
// mChart.invalidate();
155+
// mChart.invalidate();
148156
}
149157
}
150158

MPChartLib/src/com/github/mikephil/charting/charts/Chart.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,24 @@ public void setData(T data) {
278278
}
279279

280280
/**
281-
* Clears the chart from all data and refreshes it (by calling
282-
* invalidate()).
281+
* Clears the chart from all data (sets it to null) and refreshes it (by
282+
* calling invalidate()).
283283
*/
284284
public void clear() {
285285
mData = null;
286286
mDataNotSet = true;
287287
invalidate();
288288
}
289289

290+
/**
291+
* Removes all DataSets (and thereby Entries) from the chart. Does not
292+
* remove the x-values.
293+
*/
294+
public void clearValues() {
295+
mData.clearValues();
296+
invalidate();
297+
}
298+
290299
/**
291300
* Returns true if the chart is empty (meaning it's data object is either
292301
* null or contains no entries).

MPChartLib/src/com/github/mikephil/charting/data/ChartData.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,4 +879,12 @@ public void setDrawValues(boolean enabled) {
879879
set.setDrawValues(enabled);
880880
}
881881
}
882+
883+
/**
884+
* Clears this data object from all DataSets and removes all Entries.
885+
*/
886+
public void clearValues() {
887+
mDataSets.clear();
888+
notifyDataChanged();
889+
}
882890
}

0 commit comments

Comments
 (0)