Fixed the wrong-color bug when selecting values in the PieChart.

This commit is contained in:
Philipp Jahoda 2014-05-29 10:38:34 +02:00
parent aba44b3786
commit 8f8f7aee70
3 changed files with 33 additions and 15 deletions

View file

@ -83,7 +83,7 @@ public class ColorTemplate {
/**
* Adds colors to the ColorTemplate. Each of the colors will create a new
* dataset color array in the template with just one color. This is
* DataSet color array in the template with just one color. This is
* especially useful when you want each of your DataSets only to be
* represented by one color and not multiple.
*
@ -136,15 +136,15 @@ public class ColorTemplate {
}
/**
* returns the color value at the given index from the DataSet at the given
* index
* Returns the color value at the given index from the DataSet at the given
* index. If index is outofbounds, it restart at lower indices.
*
* @param dataSetIndex
* @param colorIndex
* @return
*/
public int getDataSetColor(int dataSetIndex, int colorIndex) {
return mDataSetColors.get(dataSetIndex).get(colorIndex);
return mDataSetColors.get(dataSetIndex).get(colorIndex % mDataSetColors.get(dataSetIndex).size());
}
/**

View file

@ -166,6 +166,23 @@ public class DataSet {
return mType;
}
/**
* Returns the index of the Series object with the given x-index in the
* Series array of the DataSet. IMPORTANT: This method does calculations at
* runtime, do not over-use in performance critical situations.
*
* @param xIndex
* @return
*/
public int getIndexInSeries(int xIndex) {
for(int i = 0; i < mYVals.size(); i++) {
if(xIndex == mYVals.get(i).getXIndex()) return i;
}
return -1;
}
/**
* Convenience method to create multiple DataSets of different types with
* various double value arrays. Each double array represents the data of one

View file

@ -6,7 +6,6 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Typeface;
@ -302,16 +301,16 @@ public class PieChart extends Chart {
for (int i = 0; i < mIndicesToHightlight.length; i++) {
// get the index to highlight
int index = mIndicesToHightlight[i].getXIndex();
if (index >= mDrawAngles.length || index > mDeltaX)
int xIndex = mIndicesToHightlight[i].getXIndex();
if (xIndex >= mDrawAngles.length || xIndex > mDeltaX)
continue;
if (index == 0)
if (xIndex == 0)
angle = mChartAngle;
else
angle = mChartAngle + mAbsoluteAngles[index - 1];
angle = mChartAngle + mAbsoluteAngles[xIndex - 1];
float sliceDegrees = mDrawAngles[index];
float sliceDegrees = mDrawAngles[xIndex];
float shiftangle = (float) Math.toRadians(angle + sliceDegrees / 2f);
@ -321,11 +320,14 @@ public class PieChart extends Chart {
RectF highlighted = new RectF(mCircleBox.left + xShift, mCircleBox.top + yShift,
mCircleBox.right
+ xShift, mCircleBox.bottom + yShift);
ArrayList<Integer> colors = mCt.getDataSetColors(mIndicesToHightlight[i]
DataSet set = mData.getDataSetByIndex(mIndicesToHightlight[i]
.getDataSetIndex());
int color = mCt.getDataSetColor(mIndicesToHightlight[i]
.getDataSetIndex(), set.getIndexInSeries(xIndex));
mRenderPaint.setColor(colors.get(index % colors.size()));
mRenderPaint.setColor(color);
// redefine the rect that contains the arc so that the
// highlighted pie is not cut off
@ -354,12 +356,11 @@ public class PieChart extends Chart {
for (int j = 0; j < series.size(); j++) {
mRenderPaint.setColor(colors.get(j % colors.size()));
float newangle = mDrawAngles[cnt];
if (!needsHighlight(series.get(j).getXIndex(), i)) {
mRenderPaint.setColor(colors.get(j % colors.size()));
mDrawCanvas.drawArc(mCircleBox, angle, newangle, true, mRenderPaint);
}