问题
I want to highlight (change color) of a pie graph specific slice when clicked by the user. I can find in the samples (the code below) that it is possible to show the index of the slice and the exact point. but what about recoloring the slice ?
mChartView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast
.makeText(PieChartBuilder.this, "No chart element was clicked", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(
PieChartBuilder.this,
"Chart element data point index " + seriesSelection.getPointIndex()
+ " was clicked" + " point value=" + seriesSelection.getValue(),
Toast.LENGTH_SHORT).show();
}
}
});
回答1:
This will do the stuff you need:
mRenderer.getSeriesRendererAt(seriesSelection.getPointIndex()).setColor(color);
mChartView.repaint();
回答2:
Code for onClick on the chart:
Use this:
mChartView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(PieChartBuilder.this, "No chart element selected", Toast.LENGTH_SHORT)
.show();
} else {
for (int i = 0; i < mSeries.getItemCount(); i++) {
mRenderer.getSeriesRendererAt(i).setHighlighted(i == seriesSelection.getPointIndex());
}
mChartView.repaint();
Toast.makeText(
PieChartBuilder.this,
"Chart data point index " + seriesSelection.getPointIndex() + " selected"
+ " point value=" + seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});
回答3:
You also need to ensure the chart is clickable. I spent quite some time wondering why I couldnt get it to work. the line
mRenderer.setClickEnabled(true);
should be included
来源:https://stackoverflow.com/questions/11120125/highlighting-pie-chart-slice-when-clicked-in-achartengine