【问题标题】:How do I make a pie chart clickable?如何使饼图可点击?
【发布时间】:2013-03-10 17:57:30
【问题描述】:

我想让饼图可以点击。我正在研究 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 网站的全部示例代码中绘制的,我可以转到不同颜色的每个区域,单击并显示适当的消息,但图表隐藏在某个地方(它不可见)。问题可能是因为我使用位图和画布的方式......

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 新手,我可以在没有实际看到图表的情况下看到正确的消息

【问题讨论】:

  • 我什至在 onDraw(Canvas canvas) 中执行此操作,例如 canvas = new Canvas(bitmap) 但结果相同。我已经没有主意了。关键是要了解与创建的位图相关的 Canvas 的工作方式......我需要你的帮助

标签: android jfreechart android-canvas pie-chart


【解决方案1】:

在视图上设置onTouchListener 并执行您已经在执行的操作,即获取像素 RGB 分量。还用于识别用户点击的饼图,将您自己的颜色传递给饼图,以便在点击时您可以给出该饼图的详细描述......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2017-06-22
    • 2021-07-14
    • 2014-09-20
    相关资源
    最近更新 更多