Skip to content Skip to sidebar Skip to footer

Mpandroidchart Piechart How To Set Label Text?

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

Solution 1:

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 2:

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, newString[] { "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 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

privatevoidchartData() {

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


        final int[] piecolors = newint[]{
                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:

privatevoidvisualizeAnalytics(ArrayList<Transaction> transactions) {

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

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

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

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

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

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

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

        PieDataSetcategories_dataSet=newPieDataSet(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);

        PieDatapieData=newPieData(categories_dataSet);

        graph.setData(pieData);

        Legendlegend= graph.getLegend();
        List<LegendEntry> legendEntries = newArrayList<>();

        RandomColorrandomColor=newRandomColor();
        int[] colors = randomColor.randomColor(categories.size());

        for (inti=0; i < categories.size(); i++) {
            LegendEntrylegendEntry=newLegendEntry();
            legendEntry.formColor = colors[i];
            legendEntry.label = categories.get(i);
            legendEntries.add(legendEntry);
        }

        categories_dataSet.setColors(colors);

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

        Descriptiondescription=newDescription();
        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

Post a Comment for "Mpandroidchart Piechart How To Set Label Text?"