Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dynamically circle colors changes on LineChartRenderer #5231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILineDataSet> dataSets = chart.getData().getDataSets();
for (int i = 0; i < dataSets.size(); i++) {
ILineDataSet dataSet = dataSets.get(i);
if (dataSet instanceof LineDataSet) {
List<Integer> 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();
}
}
5 changes: 5 additions & 0 deletions MPChartExample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gray">#efefef</color>
<color name="black">#000</color>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -611,7 +612,7 @@ public void drawExtras(Canvas c) {
/**
* cache for the circle bitmaps of all datasets
*/
private HashMap<IDataSet, DataSetImageCache> mImageCaches = new HashMap<>();
private WeakHashMap<IDataSet, DataSetImageCache> mImageCaches = new WeakHashMap<>();

/**
* buffer for drawing the circles
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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);

Expand Down Expand Up @@ -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.
*
Expand Down