Highlighting Pie Chart Slice When Clicked in AChartEngine

懵懂的女人 提交于 2019-12-22 08:29:31

问题


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

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