Skip to content

Commit af10006

Browse files
committed
Added filtering for both line and barchart.
1 parent 96edb78 commit af10006

4 files changed

Lines changed: 37 additions & 84 deletions

File tree

‎MPChartExample/res/menu/bar.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
<item android:id="@+id/actionToggle3D" android:title="Toggle 3D"></item>
99
<item android:id="@+id/actionToggleStartzero" android:title="Toggle StartZero"></item>
1010
<item android:id="@+id/actionToggleAdjustXLegend" android:title="Toggle AdjustXLegend"></item>
11+
<item
12+
android:id="@+id/actionToggleFilter"
13+
android:title="Toggle filter">
14+
</item>
1115
<item android:id="@+id/actionSave" android:title="Save to Gallery"></item>
1216
</menu>

‎MPChartExample/src/com/example/mpchartexample/BarChartActivity.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.github.mikephil.charting.data.ChartData;
1616
import com.github.mikephil.charting.data.DataSet;
1717
import com.github.mikephil.charting.data.Entry;
18+
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
1819
import com.github.mikephil.charting.utils.ColorTemplate;
1920

2021

@@ -131,6 +132,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
131132
mChart.invalidate();
132133
break;
133134
}
135+
case R.id.actionToggleFilter:
136+
if (mChart.isFilterSet()) {
137+
mChart.setFilter(ApproximatorType.NONE, 0);
138+
} else {
139+
mChart.setFilter(ApproximatorType.DOUGLAS_PEUCKER, 2);
140+
}
141+
mChart.invalidate();
142+
break;
134143
case R.id.actionSave: {
135144
// mChart.saveToGallery("title"+System.currentTimeMillis());
136145
mChart.saveToPath("title" + System.currentTimeMillis(), "");

‎MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.util.Log;
1515

1616
import com.github.mikephil.charting.data.Entry;
17+
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
1718
import com.github.mikephil.charting.listener.BarLineChartTouchListener;
1819
import com.github.mikephil.charting.utils.Highlight;
1920
import com.github.mikephil.charting.utils.PointD;
@@ -1039,6 +1040,29 @@ public void setLegendDigits(int digits) {
10391040
public int getLegendDigits() {
10401041
return mYLegendDigitsToUse;
10411042
}
1043+
1044+
/**
1045+
* Sets a filter on the whole ChartData. If the type is NONE, the filtering is reset. Be aware that the original
1046+
* DataSets are not modified. Instead there are modified copies of the data. All methods return the filtered values
1047+
* if a filter is set. To receive the original values despite a set filter, call getOriginalDataSets() of the class
1048+
* ChartData. The ChartData can be received by calling getData().
1049+
*
1050+
* @param type
1051+
* the filter type. NONE to reset filtering
1052+
* @param tolerance
1053+
* the tolerance
1054+
*/
1055+
public void setFilter(ApproximatorType type, double tolerance) {
1056+
mData.setFilter(type, tolerance);
1057+
}
1058+
1059+
/**
1060+
* returns true if a filter has been set, flase if not
1061+
* @return
1062+
*/
1063+
public boolean isFilterSet() {
1064+
return mData.isApproximatedData();
1065+
}
10421066

10431067
@Override
10441068
public void setPaint(Paint p, int which) {

‎MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java‎

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -261,90 +261,6 @@ protected void drawAdditional() {
261261
}
262262
}
263263

264-
// /**
265-
// * Returns the x index of the touch. If touch is out of the chart, the first or last index will be returned
266-
// *
267-
// * @param x
268-
// * @param y
269-
// * @return
270-
// */
271-
// public int getXIndexByTouchPoint(float x, float y) {
272-
// // create an array of the touch-point
273-
// float[] pts = new float[2];
274-
// pts[0] = x;
275-
// pts[1] = y;
276-
//
277-
// Matrix tmp = new Matrix();
278-
//
279-
// // invert all matrixes to convert back to the original value
280-
// mMatrixOffset.invert(tmp);
281-
// tmp.mapPoints(pts);
282-
//
283-
// mMatrixTouch.invert(tmp);
284-
// tmp.mapPoints(pts);
285-
//
286-
// mMatrixValueToPx.invert(tmp);
287-
// tmp.mapPoints(pts);
288-
//
289-
// double xTouchVal = pts[0];
290-
// double yTouchVal = pts[1];
291-
// double base = Math.floor(xTouchVal);
292-
//
293-
// Log.i(LOG_TAG, "touchindex x: " + xTouchVal + ", touchindex y: " + yTouchVal);
294-
//
295-
// // touch out of chart
296-
// if (xTouchVal < 0)
297-
// return 0;
298-
// if (xTouchVal > mDeltaX) {
299-
// return mData.getXValCount() - 1;
300-
// }
301-
//
302-
// return (int) base;
303-
// }
304-
//
305-
// public double getYValueByTouchPoint(float x, float y) {
306-
// // create an array of the touch-point
307-
// float[] pts = new float[2];
308-
// pts[0] = x;
309-
// pts[1] = y;
310-
//
311-
// Matrix tmp = new Matrix();
312-
//
313-
// // invert all matrixes to convert back to the original value
314-
// mMatrixOffset.invert(tmp);
315-
// tmp.mapPoints(pts);
316-
//
317-
// mMatrixTouch.invert(tmp);
318-
// tmp.mapPoints(pts);
319-
//
320-
// mMatrixValueToPx.invert(tmp);
321-
// tmp.mapPoints(pts);
322-
//
323-
// double xTouchVal = pts[0];
324-
// double yTouchVal = pts[1];
325-
//
326-
// return yTouchVal;
327-
// }
328-
329-
/**
330-
* Sets a filter on the whole ChartData. If the type is NONE, the filtering is reset. Be aware that the original
331-
* DataSets are not modified. Instead there are modified copies of the data. All methods return the filtered values
332-
* if a filter is set. To receive the original values despite a set filter, call getOriginalDataSets() of the class
333-
* ChartData. The ChartData can be received by calling getData().
334-
*
335-
* @param type
336-
* the filter type. NONE to reset filtering
337-
* @param tolerance
338-
* the tolerance
339-
*/
340-
public void setFilter(ApproximatorType type, double tolerance) {
341-
mData.setFilter(type, tolerance);
342-
}
343-
344-
public boolean isFilterSet() {
345-
return mData.isApproximatedData();
346-
}
347-
348264
/**
349265
* set this to true to enable the drawing of circle indicators
350266
*

0 commit comments

Comments
 (0)