Why is a defined Color used for two entries, not just one
If you take a look at https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.java#L82
there is
mRenderPaint.setColor(dataSet.getColor(i / 2));
So, every color is used for two points, not just one.
Try to create a Scatter Chart with just two point, first colored red, second green. Both will be red
@volkerrichert Is right, for some reason color is used for two points. It leads to problem when I want to set different colors for different scatter chart points. Can this be somehow fixed?
Thanks.
I imagine the problem is that your getColor(int index) in ScatterDataSet is getting duplicate indices.
Let me explain. If you have 4 points your graph will try to get colors for them. But when getColor(int index) is called indices provided are 0, 0, 1, 1. And that's wrong. It should be 0, 1, 2, 3. That is why every two points are the same.
I have a solution but it's more a proof of concept but it can still help someone. It requires two iterations and won't work if you zoom in and want to only show one scatterShape* or if you zoom in and wan't to slide pixel by pixel as it will show wrong values (without correction) for one iteration but will correct itself if you move it just a bit more.
*You can fix the first issue by first detecting that you are getting the same index over and over as that tells you that you only have one left and then saving this value somewhere.
So now we know that if we have for example 5 points and they are all shown we will get indices as follows: 0, 0, 1, 1, 2 (and they should map as 0, 1, 2, 3, 4). But if we have 3 points visible (when zooming in), lets say points 3-5, we will get 1, 1, 2 (and should map as 2, 3, 4).
And we can see that the sum of first two wrong indicies is actually the offset we need to apply at the begining. And we need a counter which will reset itself when it detects that current index is lower than the previous. BUT this requires that we calculate this sum and use it in the next iteration. As I have said it's far from a perfect solution but it should work.
So what we do is as following.
First of all you have to override getColor(int index) as shown here.
Then in MyScatterDataSet
previous = Integer.MAX_VALUE;
offset = 0;
//values = ArrayList of your data
and in getColor(int index)
//if this is the second index save it for offset calculation
if(temp.size() < 2 && previous != Integer.MAX_VALUE)
temp.add(index);
//If we are starting over put in temp first digit of our correction and reset counter and calculate offset from previous iteration
if(index < previous)
{
counter = 0;
if(temp.size() == 2)
offset = temp.get(0) + temp.get(1);
temp.clear();
temp.add(index);
}
else
counter++;
previous = index;
//Check if error and return wrong value as it will not work if you only want to display one box, this can be fixed like mentioned above
if(counter + offset >= values.size())
{
return mColors.get(3);
}
//Get correct value for your right index which is counter + offset
if(values.get(counter + offset ) == 2)
return mColors.get(0);
I came across a similar issue dealing with this as well.
I had two types of datasets (systolic and diastolic) and wanted to change the color of the dataset based on their corresponding state.
originally I tried to get the entry based off the index but because of the whole
mRenderPaint.setColor(dataSet.getColor(i / 2));
would not work.
I fixed this by extending the class and adding a private variable and just doing something like this
class RecordScatterDataSet(yVals: List<Entry>, label: String, val type: RecordType) : ScatterDataSet(yVals, label) {
private var previous = 0
override fun getColor(index: Int): Int {
if (type == RecordType.BloodPressure) {
if(previous > values.count() - 1 ) {
previous = 0
}
val e = values[previous]
previous += 1
return when (e.data) {
"SYSTOLIC" -> return when (getCurrentSystLevel(e.y)) {
BPLevel.Normal -> colors[4]
BPLevel.Hypertensive -> colors[0]
BPLevel.SubOptimal -> colors[2]
BPLevel.Elevated -> colors[1]
BPLevel.Low -> colors[0]
}
"DIASTOLIC" -> {
return when (getCurrentDiasLevel(e.y)) {
BPLevel.Normal -> colors[4]
BPLevel.Hypertensive -> colors[0]
BPLevel.Elevated -> colors[1]
BPLevel.SubOptimal -> colors[2]
BPLevel.Low -> colors[0]
}
}
else -> colors[3]
}
} else return colors[4]
}
MPAndroidChart 4.0.0 fixes the scatter renderer: every entry now gets its own color from ScatterDataSet.colors instead of sharing one color between two entries, and value labels line up with the shapes again. See https://philjay.cc/mpandroidchart/docs/dataset-subclasses/. Closing this with the 4.0.0 release (https://github.com/PhilJay/MPAndroidChart/releases/tag/v4.0.0). Feel free to open a new issue if something is still missing.