1

I want to implement a PieChart with a SelectionWidget. Upon clicking on a segment within an AndroidPlot PieChart, I would like the selection widget label text to display info about the current selected segment. There is an example to do this for an XYPlot within the AndroidPlot demo but it does not translate over well to the PieChart. Any help would be appreciated. Thank you.

1 Answers1

1

I just posted a solution to a similar question here. It was necessary to add a new method to the PieRenderer class but there's a link to a build of Androidplot containing the necessary changes. It's not a production build but for whatever it's worth, its at least as stable as the current production version of Androidplot. Once you have the new build, you'll be able to do something like this:

        // detect segment clicks:
        pie.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                PointF click = new PointF(motionEvent.getX(), motionEvent.getY());
                if(pie.getPieWidget().containsPoint(click)) {
                    Segment segment = pie.getRenderer(PieRenderer.class).getContainingSegment(click);
                    if(segment != null) {
                        // handle the segment click...for now, just print
                        // the clicked segment's title to the console:
                        System.out.println("Clicked Segment: " + segment.getTitle());
                    }
                }
                return false;
            }
        });

Just replace System.out.println(...) with your code to update the SelectionWidget.

Nick
  • 8,181
  • 4
  • 38
  • 63
  • 1
    I have been a developer for 10 years and never asked a question on a forum personally. Thank you for answering so damn fast. Your solution helped me complete the job fast. Love your work. THANK YOU! – user3656706 May 20 '14 at 15:18
  • Any possible reasons as to why this might crash? For some reason my implementation crashes on this line: Segment segment = pie.getRenderer(PieRenderer.class).getContainingSegment(click); @Nick – Carlo Chum Jun 16 '14 at 07:39
  • Hard to say without seeing the stack trace. If you create a new question (I'll see it as soon as you post it) and include your stack trace I'll take a look and see if I can tell you. – Nick Jun 16 '14 at 13:14