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

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

image1

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

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

    }
}

Upvotes: 2

Views: 3056

Answers (1)

Motassem Kassab
Motassem Kassab

Reputation: 1815

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.

Upvotes: 3

Related Questions