diff --git a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java index dd43b056eb..d9decf652d 100644 --- a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java +++ b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java @@ -442,13 +442,33 @@ public void onStopTrackingTouch(SeekBar seekBar) {} @Override public void onValueSelected(Entry e, Highlight h) { + toggleCirclesColorOnGraphSelection(true); Log.i("Entry selected", e.toString()); - Log.i("LOW HIGH", "low: " + chart.getLowestVisibleX() + ", high: " + chart.getHighestVisibleX()); - Log.i("MIN MAX", "xMin: " + chart.getXChartMin() + ", xMax: " + chart.getXChartMax() + ", yMin: " + chart.getYChartMin() + ", yMax: " + chart.getYChartMax()); + Log.i("LOW HIGH", + "low: " + chart.getLowestVisibleX() + ", high: " + chart.getHighestVisibleX()); + Log.i("MIN MAX", + "xMin: " + chart.getXChartMin() + ", xMax: " + chart.getXChartMax() + ", yMin: " + + chart.getYChartMin() + ", yMax: " + chart.getYChartMax()); } @Override public void onNothingSelected() { + toggleCirclesColorOnGraphSelection(false); Log.i("Nothing selected", "Nothing selected."); } + + private void toggleCirclesColorOnGraphSelection(boolean isSelected) { + List dataSets = chart.getData().getDataSets(); + for (int i = 0; i < dataSets.size(); i++) { + ILineDataSet dataSet = dataSets.get(i); + if (dataSet instanceof LineDataSet) { + List colors = new ArrayList<>(); + for (int i1 = 0; i1 < dataSet.getEntryCount(); i1++) { + colors.add(getResources().getColor(isSelected ? R.color.gray : R.color.black)); + } + ((LineDataSet) dataSet).setCircleColors(colors); + } + } + chart.notifyDataSetChanged(); + } } diff --git a/MPChartExample/src/main/res/values/colors.xml b/MPChartExample/src/main/res/values/colors.xml new file mode 100644 index 0000000000..249a715763 --- /dev/null +++ b/MPChartExample/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #efefef + #000 + \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java index a86c16f76b..458f899370 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java @@ -26,6 +26,7 @@ import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.List; +import java.util.WeakHashMap; public class LineChartRenderer extends LineRadarRenderer { @@ -611,7 +612,7 @@ public void drawExtras(Canvas c) { /** * cache for the circle bitmaps of all datasets */ - private HashMap mImageCaches = new HashMap<>(); + private WeakHashMap mImageCaches = new WeakHashMap<>(); /** * buffer for drawing the circles @@ -660,12 +661,7 @@ protected void drawCircles(Canvas c) { mImageCaches.put(dataSet, imageCache); } - boolean changeRequired = imageCache.init(dataSet); - - // only fill the cache with new bitmaps if a change is required - if (changeRequired) { - imageCache.fill(dataSet, drawCircleHole, drawTransparentCircleHole); - } + imageCache.fill(dataSet, drawCircleHole, drawTransparentCircleHole); int boundsRangeCount = mXBounds.range + mXBounds.min; @@ -767,28 +763,7 @@ private class DataSetImageCache { private Path mCirclePathBuffer = new Path(); private Bitmap[] circleBitmaps; - - /** - * Sets up the cache, returns true if a change of cache was required. - * - * @param set - * @return - */ - protected boolean init(ILineDataSet set) { - - int size = set.getCircleColorCount(); - boolean changeRequired = false; - - if (circleBitmaps == null) { - circleBitmaps = new Bitmap[size]; - changeRequired = true; - } else if (circleBitmaps.length != size) { - circleBitmaps = new Bitmap[size]; - changeRequired = true; - } - - return changeRequired; - } + private int[] circleColors; /** * Fills the cache with bitmaps for the given dataset. @@ -797,14 +772,16 @@ protected boolean init(ILineDataSet set) { * @param drawCircleHole * @param drawTransparentCircleHole */ - protected void fill(ILineDataSet set, boolean drawCircleHole, boolean drawTransparentCircleHole) { - + protected void fill(ILineDataSet set, boolean drawCircleHole, + boolean drawTransparentCircleHole) { + init(set); int colorCount = set.getCircleColorCount(); float circleRadius = set.getCircleRadius(); float circleHoleRadius = set.getCircleHoleRadius(); for (int i = 0; i < colorCount; i++) { - + if (!changeRequired(set, colorCount, i)) + continue; Bitmap.Config conf = Bitmap.Config.ARGB_4444; Bitmap circleBitmap = Bitmap.createBitmap((int) (circleRadius * 2.1), (int) (circleRadius * 2.1), conf); @@ -850,6 +827,27 @@ protected void fill(ILineDataSet set, boolean drawCircleHole, boolean drawTransp } } + private void init(ILineDataSet set) { + int size = set.getCircleColorCount(); + + if (circleBitmaps == null) { + circleBitmaps = new Bitmap[size]; + } else if (circleBitmaps.length != size) { + circleBitmaps = new Bitmap[size]; + } + } + + private boolean changeRequired(ILineDataSet set, int colorCount, int i) { + if (circleColors == null || circleColors.length != colorCount) { + circleColors = new int[colorCount]; + } + if (circleColors[i] == set.getCircleColor(i)) { + return true; + } + circleColors[i] = set.getCircleColor(i); + return false; + } + /** * Returns the cached Bitmap at the given index. *