Android: Clickable piechart

空扰寡人 提交于 2019-12-04 14:55:15

问题


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 section in pie chart .Please let me know the code Thanks in advance. this is my android code

  public class PieActivity extends Activity 
{
/** Called when the activity is first created. */
float values[]={300,700,100,500};

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout linear=(LinearLayout) findViewById(R.id.linear);
    values=calculateData(values);
    linear.addView(new MyGraphview(this,values));

}
private float[] calculateData(float[] data)
{
    // TODO Auto-generated method stub
    float total=0;
    for(int i=0;i<data.length;i++)
    {
        total+=data[i];
    }
    for(int i=0;i<data.length;i++)
    {
    data[i]=360*(data[i]/total);            
    }
    return data;

}
public class MyGraphview extends View
{
    private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
    private float[] value_degree;
    private int[] COLORS={Color.BLUE,Color.GREEN,Color.GRAY,Color.CYAN,Color.RED};
    RectF rectf = new RectF (10, 10, 200, 200);
    int temp=0;
    public MyGraphview(Context context, float[] values) {

        super(context);
        value_degree=new float[values.length];
     //   System.out.println("values"+value_degree);
        for(int i=0;i<values.length;i++)
        {
            value_degree[i]=values[i];
            System.out.println("degree"+value_degree[i]);
        }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        for (int i = 0; i < value_degree.length; i++) {//values2.length; i++) {
            if (i == 0) {
                paint.setColor(COLORS[i]);
                canvas.drawArc(rectf, 0, value_degree[i], true, paint);
            } 
            else
            {
                    temp += (int) value_degree[i - 1];
                    paint.setColor(COLORS[i]);
                    canvas.drawArc(rectf, temp, value_degree[i], true, paint);
            }
        }
    }

}
}

回答1:


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.



来源:https://stackoverflow.com/questions/8532996/android-clickable-piechart

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