MPAndroidChart PieChart how to set label text?

前端 未结 5 563
忘了有多久
忘了有多久 2021-01-05 07:28

got the following code:

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

This s

5条回答
  •  孤街浪徒
    2021-01-05 08:13

    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 transactions) {
    
            PieChart graph = rootView.findViewById(R.id.graph);
    
            ArrayList entries = new  ArrayList<>();
    
            HashMap 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 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 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

提交回复
热议问题