2012-03-06 87 views

回答

0
@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if (mState != IS_READY_TO_DRAW) { 
    return; 
    } 
    canvas.drawColor(mBgcolor); 
    mBagpaints.setAntiAlias(true); 
    mBagpaints.setStyle(Paint.Style.FILL); 
    mBagpaints.setColor(0x88FF0000); 
    mBagpaints.setStrokeWidth(0.0f); 
    mLinePaints.setAntiAlias(true); 
    mLinePaints.setColor(0xff000000); 
    mLinePaints.setStrokeWidth(3.0f); 
    mLinePaints.setStyle(Paint.Style.STROKE); 
    RectF mOvals = new RectF(mGapleft, mGapTop, mWidth - mGapright, mHeight 
    - mGapBottm); 
    mStart = START_INC; 
    PieDetailsItem item; 
    for (int i = 0; i < mdataArray.size(); i++) { 
    item = (PieDetailsItem) mdataArray.get(i); 
    mBagpaints.setColor(item.color); 
    mSweep = (float) 360* ((float) item.count/(float) mMaxConnection); 
    canvas.drawArc(mOvals, mStart, mSweep, true, mBagpaints); 
    canvas.drawArc(mOvals, mStart, mSweep, true, mLinePaints); 
    mStart = mStart + mSweep; 


    // set your text here 
    canvas.drawText("here is some text", mStart, someYvalue, mLinePaints); 
    } 

    mState = IS_DRAW; 
} 

只是一些的drawText()調用應該正常工作。 你必須做一些數學來決定在哪裏你y座標應該是

+0

嗨,感謝您的回覆....你能給我舉一些關於如何設置y座標的例子嗎? – RaagaSudha 2012-03-06 11:05:34

1

要繪製的餅圖你不得不用很長的過程.....希望這有助於你..

public class Demo extends Activity { 
    /** Called when the activity is first created. */ 
    float values[]={500,400,300,200,100}; 
@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]; 
     for(int i=0;i<values.length;i++) 
     { 
      value_degree[i]=values[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); 
      } 
     } 
    } 

} 

}

哪組根據值的顏色在decending爲了... 而對於文本,您可以單獨設置動態文本,給色塊在文本的前面:)

+0

嗨,感謝您的迴應...在上面的例子中,你在哪裏設置文本? – RaagaSudha 2012-03-06 11:24:32

+0

在這個例子中,我沒有設置任何文本,但可以根據顏色設置文本。例如,具有最高值500的文本在上面的代碼中具有藍色...等。 – 2012-03-06 11:52:09

1

要在每個中心繪製文本你需要的餅圖段來計算每個分段的中心。要在該段上繪製的每個文本項目的中心應該與該中心點對齊 - 通過從中心x座標減去文本邊界寬度的一半(或使用paint.setTextAlign(Align.CENTER);)和文本的一半從中央y座標的高度。

至於找到一個段的中心,它只需要比使用簡單幾何體更多的考慮。

x = (/* radius of pie chart */ /2)*cos(/*angle in RADIANS */) [angle in radians = Math.toRadians(/*half the sweep angle in degrees*/)

y = (/* radius of pie chart */ /2)*sin(/*angle in RADIANS */)

幾乎沒有......不要忘記添加餅圖中心的X和Y座標:

段的中心COORDS可以通過發現到上面的x和y值,否則你試圖在以你的自定義視圖中的(0,0)爲中心的圓上繪畫!

說出你的餅圖在您的視圖的實際中心爲中心,要待補充說:

x += getWidth()/2; y += getHeight()/2;

最後但並非最不重要的,佔文本的長度被塗 - 讓你的文字的使用範圍,例如:

Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setTextAlign(Align.CENTER); // This centres the text horizontally

String labelText = "TEST"; Rect textBounds = new Rect(); ​​

y -= textBounds.height()/2;

那麼你的文本應正確顯示。

1

您可以使用名爲MPAndroidChart的庫,它非常簡單易用。簡單地導入該

compile 'com.github.PhilJay:MPAndroidChart:v3.0.1' 

,並在你的文件的gradle加入這一行

allprojects { 
repositories { 
    maven { url "https://jitpack.io" } 
} 
} 

希望這會幫助你。

相關問題