2013-03-10 32 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組件。也用於識別用戶點擊的餅圖塊將自己的顏色傳遞給餅圖塊,以便點擊ü可以給出該塊圖塊的詳細描述......