MPAndroidChart Graphic Chart, How to Change the Color of Each Label

MPAndroidChartThis is a histogram that I am building using the MPAndroidChart library. Now I need to change the color of each tag, and I can’t find a solution using an Internet search. Can someone please help me solve this problem.

+5
source share
3 answers

There are two parts to the answer.

1) If you want to have a special label in your legend for your barricade, you would add all your bars to one data set and use the setColors (int [] colors, android.content.Context c) method to assign the color of each bar.

2) , ( = ).

. , "", .

public class SO extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.androidchart_mp);
    BarChart chart = (BarChart) findViewById(R.id.chart_bar_mp);

    // replace
    ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry (1, 5));
    entries.add(new BarEntry (3, 7));
    entries.add(new BarEntry (5,3));
    entries.add(new BarEntry (7,4));
    entries.add(new BarEntry (9,1));
    BarDataSet dataset = new BarDataSet(entries, "First");
    dataset.setColors(new int[] {Color.RED, Color.GREEN, Color.GRAY, Color.BLACK, Color.BLUE});
    BarData data = new BarData(dataset);
    chart.setData(data);
    // replace



    // below is simply styling decisions on code that I have)
    YAxis left = chart.getAxisLeft();
    left.setAxisMaxValue(10);//dataset.getYMax()+2);
    left.setAxisMinValue(0);
    chart.getAxisRight().setEnabled(false);
    XAxis bottomAxis = chart.getXAxis();
    bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    bottomAxis.setAxisMinValue(0);

    bottomAxis.setLabelCount(10);
    bottomAxis.setAxisMaxValue(10);
    bottomAxis.setDrawGridLines(false);
    chart.setDrawValueAboveBar(false);
    chart.setDescription("");
    // legend
    Legend legend = chart.getLegend();
    legend.setYOffset(40);
    legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
    legend.setTextSize(200);
}

:

    ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry (1, 5));
    ArrayList<BarEntry> entries2 = new ArrayList<>();
    entries2.add(new BarEntry (3, 2));
    ArrayList<BarEntry> entries3 = new ArrayList<>();
    entries3.add(new BarEntry (5, 7));
    ArrayList<BarEntry> entries4 = new ArrayList<>();
    entries4.add(new BarEntry (7, 7));
    ArrayList<BarEntry> entries5 = new ArrayList<>();
    entries5.add(new BarEntry (9, 1));
    List<IBarDataSet> bars = new ArrayList<IBarDataSet>();
    BarDataSet dataset = new BarDataSet(entries, "First");
    dataset.setColor(Color.RED);
    bars.add(dataset);
    BarDataSet dataset2 = new BarDataSet(entries2, "Second");
    dataset2.setColor(Color.BLUE);
    bars.add(dataset2);
    BarDataSet dataset3 = new BarDataSet(entries3, "Third");
    dataset3.setColor(Color.GREEN);
    bars.add(dataset3);
    BarDataSet dataset4 = new BarDataSet(entries4, "Fourth");
    dataset4.setColor(Color.GRAY);
    bars.add(dataset4);
    BarDataSet dataset5 = new BarDataSet(entries5, "Fifth");
    dataset5.setColor(Color.BLACK);
    bars.add(dataset5);
    BarData data = new BarData(bars);
    chart.setData(data);

, , , , !

+8

setColors(...) DataSet. . BaseDataSet javadocs:

, , , , , , , :

BarDataSet dataSet = ...
int[] colors = new int[] {Color.GREEN, Color.YELLOW, Color.GREEN, Color.BLUE, Color.GRAY, Color.BLACK};
dataSet.setColors(colors);

:

dataSet.setValueTextColors(...);
+5

setColors() setColor() - .

, : ColorTemplate.COLORFUL_COLORS

, , .

-1

Source: https://habr.com/ru/post/1650795/


All Articles