如何在MPandroidchart库中使用图标作为饼图的标签 [英] How to use icons as the labels of piechart in MPandroidchart library

查看:81
本文介绍了如何在MPandroidchart库中使用图标作为饼图的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mpandroidchart库来构建饼图.饼图的要求是,每个条目中都应包含图标.

I am using mpandroidchart library to build a pie chart. The requirement for pie chart is that it should contain icons in each entry.

在我的情况下,它显示每个条目值的百分比.

In my case it is showing up the percentage of each entry values.

是否可以将标签更改为图标?

Is there any way to change the labels to icons?

我的片段类

public class MonitorOverallFragment extends Fragment {
    private int[] CHART_COLORS = {Color.rgb(253,151,39), Color.rgb(103,63,180), Color.rgb(204,217,72), Color.rgb(44,152,240)};
    private Context mContext;
    private Activity mActivity;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        icons1 = new Bitmap[]{BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.ic_card_analitics_white),
                BitmapFactory.decodeResource(mContext.getResources(),
                        R.drawable.ic_kredit_analitics_white),
                BitmapFactory.decodeResource(mContext.getResources(),
                        R.drawable.ic_vklad_analitics_white),
                BitmapFactory.decodeResource(mContext.getResources(),
                        R.drawable.ic_nps_analitics_white)};
    }

    public MonitorOverallFragment(){

    }

    int [] sampledata = {30, 40, 20, 10};

        Bitmap[] icons1;

    private String icons[] = {"a", "b", "c", "d"};



    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_monitor_overall, container, false);

        setUpPieChart(rootView);
        return  rootView;
    }
    private void setUpPieChart(View v){
        List<PieEntry> pieEntries = new ArrayList<>();
        for(int i = 0; i<sampledata.length; i++){
            pieEntries.add(new PieEntry(sampledata[i], icons1[i]));
        }
        PieDataSet dataSet = new PieDataSet(pieEntries, "");
        dataSet.setColors(CHART_COLORS);
        PieData data = new PieData(dataSet);

        PieChart chart = (PieChart)v.findViewById(R.id.pie_monitor_overall);
        chart.setData(data);
        chart.setDrawEntryLabels(true);

        Description description = new Description();
        description.setText("");
        chart.setDescription(description);

        chart.invalidate();

    }
}

推荐答案

以下是我的代码示例:

Drawable user_icon = getDrawable(R.drawable.user);
myData = new ArrayList<>();
myData.add(new PieEntry(46, user_icon));
PieDataSet dataSet = new PieDataSet(myData, "");
PieData data = new PieData(dataSet);
pieChart.setData(data);

(我知道这没有得到很好的解释,但是由于您在询问图标,因此您已经对库的简单实现有所了解了)

请注意,我使用的是3.0.2版本,以前的版本可能没有此功能.

Please note that I'm using the version 3.0.2, this feature may have not been there in previous versions.

这篇关于如何在MPandroidchart库中使用图标作为饼图的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆