MPAndroidChart PieChart how to set label text?

15,720

Solution 1

You can set custom labels with colors:

First make sure Legend is enable. Unless enable legend.

legend.setEnabled(true);

With com.github.PhilJay:MPAndroidChart:v3.0.0:-

legend .setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "aaaaa", "bbbbb", "ccccc"});

setCustom(int[] colors, String[] labels): Sets a custom legend's labels and colors arrays. The colors count should match the labels count. Each color is for the form drawn at the same index.

Solution 2

I could not find the method setCustom(int[] color, String[] labels) in v3.0.0. Only setCustom(LegendEntry[]) for which you have to pass LegendEntry objects.

 List<LegendEntry> entries = new ArrayList<>();

 for (int i = 0; i < titleList.size(); i++) {
     LegendEntry entry = new LegendEntry();
     entry.formColor = colorList.get(i);
     entry.label = titleList.get(i);
     entries.add(entry);
 }

 legend.setCustom(entries);

Solution 3

Pass the label name as second parameter to constructor PieEntry(). (For version > 3.0.0)

Example:

ArrayList<PieEntry> yvalues = new ArrayList<PieEntry>();
yvalues.add(new PieEntry(8f, "JAN"));
yvalues.add(new PieEntry(15f, "FEB"));
yvalues.add(new PieEntry(12f, "MAR"));
yvalues.add(new PieEntry(25f, "APR"));
yvalues.add(new PieEntry(23f, "MAY"));
yvalues.add(new PieEntry(17f, "JUNE"));
PieDataSet dataSet = new PieDataSet(yvalues, "Election Results");
PieData data = new PieData();
data.addDataSet(dataSet);
data.setValueFormatter(new PercentFormatter());
pieChart.setData(data);

Solution 4

1)Add dependency to build.gradle app level compile 'com.github.PhilJay:MPAndroidChart:v2.1.0' 2)make function chartData

 private void chartData() {

        ArrayList<Entry> entries = new ArrayList<>();
        entries.add(new Entry(50, 0));
        entries.add(new Entry(60, 1));


        final int[] piecolors = new int[]{
                Color.rgb(183, 28, 28),
                Color.rgb(27, 94, 32)};

        PieDataSet dataset = new PieDataSet(entries, "");

        ArrayList<String> labels = new ArrayList<String>();
        labels.add("Borrowing");
        labels.add("Pending");


        PieData data = new PieData(labels, dataset);
        dataset.setColors(ColorTemplate.createColors(piecolors)); //
        data.setValueTextColor(Color.WHITE);
        pieChart.setDescription("Description");
        pieChart.setData(data);

    }

3)Call chartData() in onCreate()

Solution 5

Thanks to vikas singh and other hints elsewhere, here's how I solved both coloring for the piechart and legend, for MPAndroidChart v3.0.3:

 private void visualizeAnalytics(ArrayList<Transaction> transactions) {

        PieChart graph = rootView.findViewById(R.id.graph);

        ArrayList<PieEntry> entries = new  ArrayList<>();

        HashMap<String, Integer> categoryFrequencyMap = new HashMap<>();

        for(Transaction T: transactions) {
            String category = T.getType().name();

           if(categoryFrequencyMap.containsKey(category)){
               categoryFrequencyMap.put(category, categoryFrequencyMap.get(category) + T.getAmount());
           }else {
               categoryFrequencyMap.put(category, T.getAmount());
           }
        }

        ArrayList<String> categories = new ArrayList<>();

        int e = 0;
        for(String category: categoryFrequencyMap.keySet()){
            categories.add(e, category);
            entries.add(new PieEntry(categoryFrequencyMap.get(category), category));
            e++;
        }

        PieDataSet categories_dataSet = new PieDataSet(entries, "Categories");

        categories_dataSet.setSliceSpace(2f);
        categories_dataSet.setValueTextSize(15f);
        //categories_dataSet.setValueTextColor(Color.RED);/* this line not working */
        graph.setEntryLabelColor(Color.RED);

        categories_dataSet.setSelectionShift(10f);
        categories_dataSet.setValueLinePart1OffsetPercentage(80.f);
        categories_dataSet.setValueLinePart1Length(1f);
        categories_dataSet.setValueLinePart2Length(0.9f);
        categories_dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        PieData pieData = new PieData(categories_dataSet);

        graph.setData(pieData);

        Legend legend = graph.getLegend();
        List<LegendEntry> legendEntries = new ArrayList<>();

        RandomColor randomColor = new RandomColor();
        int[] colors = randomColor.randomColor(categories.size());

        for (int i = 0; i < categories.size(); i++) {
            LegendEntry legendEntry = new LegendEntry();
            legendEntry.formColor = colors[i];
            legendEntry.label = categories.get(i);
            legendEntries.add(legendEntry);
        }

        categories_dataSet.setColors(colors);

        legend.setEnabled(false);
        legend.setCustom(legendEntries);

        Description description = new Description();
        description.setText("Analyzing Transaction Vol by Category");
        graph.setDescription(description);

        graph.animateY(5000);

        graph.invalidate(); // refresh
}

For the nice random colors, I sourced from this: https://github.com/lzyzsd/AndroidRandomColor

Share:
15,720
Fomovet
Author by

Fomovet

Please don't see me off.The journey I'm walking on alone is lonely and dangerous. ­

Updated on July 28, 2022

Comments

  • Fomovet
    Fomovet almost 2 years

    got the following code:

        Legend legend = mChart.getLegend();
        legend.setLabels(new String[]{"aaaaa", "bbbbb", "ccccc"});
    

    This setting does not take effect Is there an other way to set the text ?

  • Fomovet
    Fomovet over 7 years
    Thank you, but I didn't find the legend .setCustom()method
  • Fomovet
    Fomovet over 7 years
    build.gradle:compile 'com.github.PhilJay:MPAndroidChart:v2.1.0'
  • Chandana Kumara
    Chandana Kumara over 7 years
    Check with this compile 'com.github.PhilJay:MPAndroidChart:v3.0.0'
  • Fomovet
    Fomovet over 7 years
    Thank you !for helping me a lot.
  • JWL
    JWL almost 6 years
    This is the only approach that's worked for me with v3.0.3, full solution shared...
  • nafees ahmed
    nafees ahmed over 4 years
    how to increase only label's text size?
  • Moeez
    Moeez about 3 years
    how to override float with text ? Like yvalues.add(new PieEntry(8f, "JAN")); in place of 8f I want to add a text value