如何在饼图项中显示标签(textview)?

时间:2012-01-05 09:48:57

标签: android pie-chart

我想在饼图项上显示标签..请指导我..

Main.java: -

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

        PieDetailsItem item;
       int maxCount=0;
        int itemCount=0;
        int items[]={2,6,0};
        int colors[]={-6777216,-16776961,-16711681};
       String itemslabel[]={" vauesr ur 100"," vauesr ur 200"," vauesr ur 300"};
        for(int i=0;i<items.length;i++)
        {
        itemCount=items[i];
        item=new PieDetailsItem();
        item.count=itemCount;
        item.label=itemslabel[i];
        item.color=colors[i];
        piedata.add(item);
       maxCount=maxCount+itemCount;
        }
        int size=155;
        int BgColor=0xffa11b1;
       Bitmap mBaggroundImage=Bitmap.createBitmap(size,size,Bitmap.Config.ARGB_8888);
        View_PieChart piechart=new View_PieChart(this);
        piechart.setLayoutParams(new LayoutParams(size,size));
        piechart.setGeometry(size, size, 2, 2, 2, 2, 2130837504);
        piechart.setSkinparams(BgColor);
        piechart.setData(piedata, maxCount);
        piechart.invalidate();
        piechart.draw(new Canvas(mBaggroundImage));
        piechart=null;
        ImageView mImageView=new ImageView(this);
        mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        mImageView.setBackgroundColor(BgColor);
        mImageView.setImageBitmap(mBaggroundImage);
        LinearLayout finalLayout=(LinearLayout)findViewById(R.id.pie_container);
        finalLayout.addView(mImageView);
    }

View_piechart.java: -

public View_PieChart(Context context){         超级(上下文);         Log.w(“单一利弊”,“单一利弊”);     }

public View_PieChart(Context context, AttributeSet attr) {
    super(context, attr);
    Log.w(" double cons ", " double cons");
}

@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;
    }

    mState = IS_DRAW;
}

public void setGeometry(int width, int height, int gapleft, int gapright,
        int gaptop, int gapbottom, int overlayid) {

    mWidth = width;
    mHeight = height;
    mGapleft = gapleft;
    mGapright = gapright;
    mGapBottm = gapbottom;
    mGapTop = gaptop;

}

public void setSkinparams(int bgcolor) {
    Log.w(" Set bg color  : ", bgcolor + "");
    mBgcolor = bgcolor;
}

public void setData(List<PieDetailsItem> data, int maxconnection) {
    mdataArray = data;
    mMaxConnection = maxconnection;
    Log.w(" Max Connection  ", maxconnection + " " + "  Adataarray :"
            + data.toString());
    mState = IS_READY_TO_DRAW;
}

public void setState(int state) {
    mState = state;
}

public int getColorValues(int index) {
    if (mdataArray == null) {
        return 0;
    }

    else if (index < 0)
        return ((PieDetailsItem) mdataArray.get(0)).color;
    else if (index > mdataArray.size())
        return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;
    else
        return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;

}

main.xml中

>

<LinearLayout android:id="@+id/pie_container" 
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content"

                    android:orientation="vertical" >
</LinearLayout> 

2 个答案:

答案 0 :(得分:1)

您不会使用TextView,您只需要在馅饼顶部绘制文字。

参考: Android Canvas.drawText

答案 1 :(得分:0)

发布所需视图的xml布局。