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

Enable the use of a custom highlight color for selected pie chart segments #3777

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ protected void onCreate(Bundle savedInstanceState) {
// chart.setDrawUnitsInChart(true);

// add a selection listener
chart.setOnChartValueSelectedListener(this);
mChart.setOnChartValueSelectedListener(this);
// mChart.setUseCustomHighLightColor(true);

seekBarX.setProgress(4);
seekBarY.setProgress(10);
Expand Down Expand Up @@ -286,9 +287,64 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setData(seekBarX.getProgress(), seekBarY.getProgress());
}

@Override
protected void saveToGallery() {
saveToGallery(chart, "PieChartActivity");
private void setData(int count, float range) {

float mult = range;

ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

// NOTE: The order of the entries when being added to the entries array determines their position around the center of
// the chart.
for (int i = 0; i < count ; i++) {
entries.add(new PieEntry((float) ((Math.random() * mult) + mult / 5),
mParties[i % mParties.length],
getResources().getDrawable(R.drawable.star)));
}

PieDataSet dataSet = new PieDataSet(entries, "Election Results");

dataSet.setDrawIcons(false);

dataSet.setSliceSpace(3f);
dataSet.setIconsOffset(new MPPointF(0, 40));
dataSet.setSelectionShift(5f);
// dataSet.setHighLightColor(Color.BLACK);

// add a lot of colors

ArrayList<Integer> colors = new ArrayList<Integer>();

for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);

for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);

for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);

for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);

for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);

colors.add(ColorTemplate.getHoloBlue());

dataSet.setColors(colors);
//dataSet.setSelectionShift(0f);

PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
data.setValueTypeface(mTfLight);
mChart.setData(data);

// undo all highlights
mChart.highlightValues(null);

mChart.invalidate();
}

private SpannableString generateCenterSpannableText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public class PieChart extends PieRadarChartBase<PieData> {
*/
private boolean mDrawCenterText = true;

/**
* When useCustomHighLightColor is true, the renderer will use the custom color
* instead of the segment's color
*/
private boolean mUseCustomHighLightColor = false;

private float mCenterTextRadiusPercent = 100.f;

protected float mMaxAngle = 360f;
Expand Down Expand Up @@ -480,6 +486,20 @@ public boolean isDrawCenterTextEnabled() {
return mDrawCenterText;
}

/**
* Set this to true to use a custom color for highlighting selected segments
* of the pie chart
*
* @param enabled
*/
public void setUseCustomHighLightColor(boolean enabled) {
this.mUseCustomHighLightColor = enabled;
}

public boolean useCustomHighLightColor() {
return mUseCustomHighLightColor;
}

@Override
protected float getRequiredLegendOffset() {
return mLegendRenderer.getLabelPaint().getTextSize() * 2.f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class PieDataSet extends DataSet<PieEntry> implements IPieDataSet {
private ValuePosition mYValuePosition = ValuePosition.INSIDE_SLICE;
private boolean mUsingSliceColorAsValueLineColor = false;
private int mValueLineColor = 0xff000000;
private int mHighLightColor = 0xff000000;
private float mValueLineWidth = 1.0f;
private float mValueLinePart1OffsetPercentage = 75.f;
private float mValueLinePart1Length = 0.3f;
Expand Down Expand Up @@ -161,6 +162,16 @@ public void setValueLineColor(int valueLineColor) {
/**
* When valuePosition is OutsideSlice, indicates line width
*/
public void setHighLightColor(int color) {
this.mHighLightColor = color;
}

@Override
public int getCustomHighlightColor() {
return this.mHighLightColor;
}

/** When valuePosition is OutsideSlice, indicates line width */
@Override
public float getValueLineWidth() {
return mValueLineWidth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public interface IPieDataSet extends IDataSet<PieEntry> {
* */
int getValueLineColor();

/**
* Returns the custom highlight color specified when creating the pie data set
*/
int getCustomHighlightColor();

/**
* When valuePosition is OutsideSlice, indicates line width
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ public void drawValues(Canvas c) {

angle = angle + angleOffset;

final float minimumSliceSize = 0.15f * 360.0f;
final int entryThresholdForHiding = 2;

final float transformedAngle = rotationAngle + angle * phaseY;

float value = mChart.isUsePercentValuesEnabled() ? entry.getY()
Expand All @@ -513,7 +516,7 @@ public void drawValues(Canvas c) {
final boolean drawYInside = drawValues &&
yValuePosition == PieDataSet.ValuePosition.INSIDE_SLICE;

if (drawXOutside || drawYOutside) {
if ((drawXOutside || drawYOutside) && !(sliceAngle <= minimumSliceSize && entryCount > entryThresholdForHiding)) {

final float valueLineLength1 = dataSet.getValueLinePart1Length();
final float valueLineLength2 = dataSet.getValueLinePart2Length();
Expand Down Expand Up @@ -857,7 +860,11 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {

final boolean accountForSliceSpacing = sliceSpace > 0.f && sliceAngle <= 180.f;

mRenderPaint.setColor(set.getColor(index));
if (mChart.useCustomHighLightColor()) {
mRenderPaint.setColor(set.getCustomHighlightColor());
} else {
mRenderPaint.setColor(set.getColor(index));
}

final float sliceSpaceAngleOuter = visibleAngleCount == 1 ?
0.f :
Expand Down