4

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();
          }
        }
      });
Abdelwahed
  • 1,694
  • 4
  • 21
  • 31

3 Answers3

2

This will do the stuff you need:

mRenderer.getSeriesRendererAt(seriesSelection.getPointIndex()).setColor(color);
mChartView.repaint();
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • where so we need to add this ? – Goofy Jan 15 '13 at 09:49
  • 1
    Next to the code in the question. See this: http://code.google.com/p/achartengine/source/browse/trunk/achartengine/demo/org/achartengine/chartdemo/demo/chart/PieChartBuilder.java – Dan D. Jan 15 '13 at 09:56
1

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();
          }
        }
      });
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
1

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

KuriaNdungu
  • 623
  • 8
  • 20