Highlighting Pie Chart Slice When Clicked in AChartEngine

◇◆丶佛笑我妖孽 提交于 2019-12-05 15:53:49

This will do the stuff you need:

mRenderer.getSeriesRendererAt(seriesSelection.getPointIndex()).setColor(color);
mChartView.repaint();

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();
          }
        }
      });

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!