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

发布于 2024-12-25 09:54:47 字数 4410 浏览 2 评论 0原文

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

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("单一缺点", "单一缺点"); main.xml

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;

}

>

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

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

I want to display the labels on pie chart items..please guide me..

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) {
super(context);
Log.w(" single cons ", " single cons");
}

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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‘画卷フ 2025-01-01 09:54:47

您不会使用 TextView,您只需在饼图的顶部绘制文本即可。

参考这个:
Android Canvas.drawText

You're not going to use a TextView, you're just going to draw the text right on top of your pies.

refer to this:
Android Canvas.drawText

剑心龙吟 2025-01-01 09:54:47

发布您想要的视图的 xml 布局。

Post xml layouts for the views that you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文