Android: Clickable piechart

前端 未结 1 1912
抹茶落季
抹茶落季 2021-02-11 12:00

I need a piechart for my App to display some data in different sections. the piechart is ready and it also works fine but i need a clickable event when touched on a particular s

相关标签:
1条回答
  • 2021-02-11 12:26

    You could try this:

    • Override onTouchEvent in MyGraphView and check the action. Normally for ACTION_DOWN you should return true and on ACTION_UP handle the click.

    • When you handle the click, extract the relative event coordinates from the center of the chart, something like

      float relX = event.getX() - (rectf.right - rectf.left) * 0.5f;
      float relY = event.getY() - (rectf.bottom - rectf.top) * 0.5f;
      
    • Then you need to find the angle:

      float angleInRad = (float)Math.atan2(relY, relX);
      
    • Now you've got the angle but in radians and in the range -PI..PI. So:

      int degree = (int)((angleInRad + Math.PI) * 180 / Math.PI);
      
    • Now just find which interval (from value_degree) contains this value.

    Also note that since the coordinate system is upside down, you might need to use -relY instead of relY. Just try it and change it if needed.

    0 讨论(0)
提交回复
热议问题