如何使饼图可点击?

时间:2013-03-10 17:57:30

标签: android jfreechart android-canvas pie-chart

我想让饼图可点击。我正在研究afreechart网站上发布的例子(用于制作条形图和饼图)。我试图理解它,我会进一步使饼图可点击。 可以在此处找到示例:https://code.google.com/p/afreechart/

查看课程

protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        bitmap =  Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                Bitmap.Config.ARGB_8888);

        canvas.setBitmap(bitmap);

       inertialMove();


      paintComponent(canvas);
    }

 public  void paintComponent(Canvas canvas) {

        // first determine the size of the chart rendering area...



        Dimension size = getSize();
        RectangleInsets insets = getInsets();
        RectShape available = new RectShape(insets.getLeft(), insets.getTop(),
                size.getWidth() - insets.getLeft() - insets.getRight(),
                size.getHeight() - insets.getTop() - insets.getBottom());

        double drawWidth = available.getWidth();
        double drawHeight = available.getHeight();
        this.scaleX = 1.0;
        this.scaleY = 1.0;

        if (drawWidth < this.minimumDrawWidth) {
            this.scaleX = drawWidth / this.minimumDrawWidth;
            drawWidth = this.minimumDrawWidth;
        }
        else if (drawWidth > this.maximumDrawWidth) {
            this.scaleX = drawWidth / this.maximumDrawWidth;
            drawWidth = this.maximumDrawWidth;
        }

        if (drawHeight < this.minimumDrawHeight) {
            this.scaleY = drawHeight / this.minimumDrawHeight;
            drawHeight = this.minimumDrawHeight;
        }
        else if (drawHeight > this.maximumDrawHeight) {
            this.scaleY = drawHeight / this.maximumDrawHeight;
            drawHeight = this.maximumDrawHeight;
        }

        RectShape chartArea = new RectShape(0.0, 0.0, drawWidth,
                drawHeight);

        this.chart.draw(canvas,chartArea, this.anchor, this.info);

 }

活动

然后我想点击该区域(例如红色区域)来显示消息。 问题是因为我知道图表来自afreechart网站的总示例代码,我可以去每个不同颜色的区域,点击并显示相应的消息但是图表隐藏了某些地方(这是不可见的)。问题可能是因为我使用BITMAP和CANVAS的方式......

public boolean onTouchEvent(MotionEvent event){
            if(event.getAction()==MotionEvent.ACTION_DOWN){


          }

            else if (event.getAction()==MotionEvent.ACTION_UP){







                int color = bitmap.getPixel((int) event.getX(), (int) event.getY());
                System.out.println("color =" +color);

                int redValue = Color.red(color);
                int blueValue = Color.blue(color);
                int greenValue = Color.green(color);

                System.out.println("redValue =" +redValue);
                System.out.println("blueValue =" +blueValue);
                System.out.println("greenValue =" +greenValue);





                     if (redValue == 255){
                        Toast.makeText(getBaseContext(), "Is Red", Toast.LENGTH_SHORT).show();

                    }


                     if (blueValue == 255){
                        Toast.makeText(getBaseContext(), "Is BLUE", Toast.LENGTH_SHORT).show();

                    }


                     if (greenValue == 255){
                          Toast.makeText(getBaseContext(), "Is GREEN", Toast.LENGTH_SHORT).show();

                        }
                //}

            }
            return true; 

        }


    }

请允许我很高兴得到你的帮助......真的很令人沮丧,因为我是Android的新手,我可以看到正确的信息显示而没有真正看到图表

1 个答案:

答案 0 :(得分:0)

在视图上设置onTouchListener并执行您正在执行的操作,即获取像素RGB组件。另外,为了识别用户点击的饼图,将你自己的颜色传递给饼图,这样点击你就可以详细描述那个饼图......