How to use icons as the labels of piechart in MPandroidchart library

ⅰ亾dé卋堺 提交于 2019-12-11 03:42:08

问题


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?

My Fragment class

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();

    }
}

回答1:


Here's a sample from my code :

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);

(I know it's not well explained but since you're asking about icons then you're already over the simple implementation of the library)

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



来源:https://stackoverflow.com/questions/44863021/how-to-use-icons-as-the-labels-of-piechart-in-mpandroidchart-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!