机器人:可点击饼图 [英] Android: Clickable piechart

查看:114
本文介绍了机器人:可点击饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个饼图我的应用程序,以显示不同部分的一些数据。 在饼图准备,它也能正常工作,但我需要一个可点击的事件在饼图。请一个特定部分触摸时让我知道了code 提前致谢。 这是我的Andr​​oid code

 公共类PieActivity扩展活动
{
/ **第一次创建活动时调用。 * /
浮点值[] = {300700100500};

@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    的LinearLayout线性=(的LinearLayout)findViewById(R.id.linear);
    值= calculateData(值);
    linear.addView(新MyGraphview(这一点,值));

}
私人浮法[] calculateData(浮动[]数据)
{
    // TODO自动生成方法存根
    总浮= 0;
    的for(int i = 0; I< data.length;我++)
    {
        共有+ =数据[I]
    }
    的for(int i = 0; I< data.length;我++)
    {
    数据[i] = 360 *(数据[I] /个);
    }
    返回的数据;

}
公共类MyGraphview扩展视图
{
    民营涂料粉刷=新的油漆(Paint.ANTI_ALIAS_FLAG);
    私人浮法[] value_degree;
    私人INT [] COLORS = {Color.BLUE,Color.GREEN,Color.GRAY,Color.CYAN,Color.RED};
    RectF rectf =新RectF(10,10,200,200);
    INT TEMP = 0;
    公共MyGraphview(上下文的背景下,浮法[]值){

        超(上下文);
        value_degree =新的浮动[values​​.length]
     //的System.out.println(值+ value_degree);
        的for(int i = 0; I< values​​.length;我++)
        {
            value_degree [I] =值[I];
            的System.out.println(度+ value_degree [I]);
        }
    }
    @覆盖
    保护无效的OnDraw(帆布油画){
        // TODO自动生成方法存根
        super.onDraw(画布);

        的for(int i = 0; I< value_degree.length;我++){//values​​2.length;我++){
            如果(我== 0){
                paint.setColor(COLORS [I]);
                canvas.drawArc(rectf,0,value_degree [I],真,油漆);
            }
            其他
            {
                    温度+ =(int)的value_degree [我 -  1];
                    paint.setColor(COLORS [I]);
                    canvas.drawArc(rectf,温度,value_degree [I],真实,油漆);
            }
        }
    }

}
}
 

解决方案

您可以试试这个:

  • 覆盖的onTouchEvent在MyGraphView并检查行动。通常情况下为ACTION_DOWN您应该返回true和ACTION_UP处理click。

  • 在你处理一下,提取从图表中心的相关事件的坐标,像

     浮动RELX = event.getX() - (rectf.right  -  rectf.left)* 0.5F;
    浮依靠= event.getY() - (rectf.bottom  -  rectf.top)* 0.5F;
     

  • 然后,你需要找到的角度:

     浮动angleInRad =(浮点)Math.atan2(靠,RELX);
     

  • 现在你已经得到了角度,但弧度和范围-PI..PI。所以:

      INT度=(INT)((angleInRad + Math.PI)* 180 / Math.PI);
     

  • 现在只要找到它的时间间隔(从value_degree)包含此值。

还要注意,由于该坐标系是上下颠倒,你可能需要使用-relY,而不是依赖。试试吧,如果需要改变它。

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

}
}

解决方案

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.

这篇关于机器人:可点击饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆