MPAndroidChart条形图如何更改每个标签的颜色 [英] MPAndroidChart bar chart how to change color of each label

查看:142
本文介绍了MPAndroidChart条形图如何更改每个标签的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用MPAndroidChart库构建的条形图.现在,我必须更改每个标签的颜色,并且无法通过Internet搜索找到解决方案.谁能帮助我解决这个问题.

This is a bar chart that i build using MPAndroidChart library. Now I have to change the color of each label and I can't find the solution through searching over the internet. Can any one please help me to get through this problem.

推荐答案

此答案分为两部分.

1)如果要在条形图的图例中使用单个标签,则可以将所有条形添加到一个数据集中,并使用setColors(int [] colors,android.content.Context c)方法进行分配每个条形都有一种颜色.

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

2)如果要在每个条形图例中使用不同的标签,则需要在图表中包括多个数据集,并为每个数据集分配颜色(标签数=数据集数).

2) If you want to have different labels in your legend for each bar, you would need to include multiple datasets into your chart and assign a color to each dataset (number of labels = number of datasets).

我在下面提供了示例代码供您参考.最初的代码块代表第一个选项,而第二个代码块则可以在标题为"replace"的注释之间替换以获取第二个选项.

I have included example code below for you to reference. The initial block of code is representative of the first option and the second block of code you can substitute in between the comments titled "replace" to get the second option.

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

希望对您有帮助,如果您还有其他疑问,请告诉我!

I hope this helps, if you have any other questions, please let me know!

这篇关于MPAndroidChart条形图如何更改每个标签的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆