2013-03-10 34 views
0

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

View类

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和我可以看到没有真正看到图表显示正确的消息

+0

我甚至可以在o​​nDraw(Canvas canvas)中做到这一点,像canvas =新的画布(位图),但具有相同的结果。我已经没有想法了。关键是要了解Canvas与创建的位图关联的方式......我需要你的帮助 – ozzycoca 2013-03-10 21:26:41

回答

0

在视图上设置一个onTouchListener,并做你已经在做什么,即获得像素RGB组件。也用于识别用户点击的饼图块将自己的颜色传递给饼图块,以便点击ü可以给出该块图块的详细描述......