Android 如何在图表中设置图例标签

Android 如何在图表中设置图例标签,android,mpandroidchart,Android,Mpandroidchart,我正在尝试自定义图例,但无法自定义。我的目的是为图例添加不同的标签。我正在使用MPChart库进行自定义 ArrayList<BarEntry> entries = new ArrayList<>(); entries.add(new BarEntry(4f, 0)); entries.add(new BarEntry(8f, 1)); entries.add(new BarEntry(6f, 2)); entries.add(new

我正在尝试自定义图例,但无法自定义。我的目的是为图例添加不同的标签。我正在使用MPChart库进行自定义

  ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry(4f, 0));
    entries.add(new BarEntry(8f, 1));
    entries.add(new BarEntry(6f, 2));
    entries.add(new BarEntry(12f, 3));
    entries.add(new BarEntry(18f, 4));
    mColors.add(R.color.red);
    mColors.add(R.color.text_color_gray);
    mColors.add(R.color.text_color_blue);
    mColors.add(R.color.green);
    mColors.add(R.color.black);
   BarDataSet dataset = new BarDataSet(entries, null);
    ArrayList<String> labels = new ArrayList<String>();
    labels.add("05");
    labels.add("06");
    labels.add("07");
    labels.add("08");
    labels.add("09");
    BarData data = new BarData(labels, dataset);
    Legend legend = mChart.getLegend();
  legend.setEnabled(true);
    legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
    legend.setForm(Legend.LegendForm.SQUARE);
    legend.setColors(mColors);
    legend.setLabels(mLabels);
    mChart.setData(data);
    mChart.animateY(2000);

    LimitLine line = new LimitLine(10f);
    YAxis yAxis = mChart.getAxisLeft();
    yAxis.addLimitLine(line);
    yAxis.setDrawAxisLine(true);
    mChart.setDrawValueAboveBar(true);
    mChart.setDrawBarShadow(false);
    mChart.setVisibleXRange(4);
    mChart.moveViewToX(2);
    mChart.setDrawValueAboveBar(false);
    mChart.invalidate();
ArrayList entries=new ArrayList();
新增(新巴伦特里(4f,0));
增加(新巴伦特里(8f,1));
增加(新巴伦特里(6f,2));
增加(新巴伦特里(12f,3));
增加(新巴伦特里(18f,4));
mColors.add(R.color.red);
mColors.add(R.color.text\u color\u gray);
mColors.add(R.color.text\u color\u blue);
mColors.add(R.color.green);
mColors.add(R.color.black);
BarDataSet数据集=新的BarDataSet(条目,空);
ArrayList标签=新的ArrayList();
标签。添加(“05”);
标签。添加(“06”);
标签。添加(“07”);
标签。添加(“08”);
标签。添加(“09”);
BarData数据=新的BarData(标签、数据集);
Legend=mChart.getLegend();
图例.setEnabled(真);
图例.设置位置(图例.图例位置.图表下方\中心);
legend.setForm(legend.LegendForm.SQUARE);
图例。设置颜色(mColors);
图例.设置标签(mLabels);
mChart.setData(数据);
麦克哈特·阿尼泰(2000);
限位线=新限位线(10f);
YAxis YAxis=mChart.getAxisLeft();
yAxis.addLimitLine(行);
yAxis.setDrawAxisLine(真);
mChart.SetDrawValueUpperBar(真实);
mChart.settrabarshadow(假);
mChart.setVisibleXRange(4);
mChart.moveViewToX(2);
mChart.SetDrawValueOverBar(假);
mChart.invalidate();

请告诉我任何解决方案。

如果您想要4个图例标签,您需要4个BarDataSet对象

使用不同的颜色只会将不同的颜色分组到将生成的一个图例上

您需要将颜色传递到数据集,它将与图例一起映射


最后,数据集需要一个用于图例的标签。您可以将标签指定为构造函数中的第二个参数。

您可以像这样自定义图例

LegendEntry legendEntryA = new LegendEntry();
legendEntryA.label = "a";
legendEntryA.formColor = Color.GREEN;
并将它们添加到图表的图例中

legend.setCustom(Arrays.asList(legendEntryA, legendEntryB, legendEntryC, legendEntryD));

关于代码正在做什么和应该做什么,您能更精确一点吗?你是说轴上的标签吗?@JDenais我想在轴上设置legend label not label。Ex-如果我使用了4种颜色,那么我想将4种颜色显示为图例及其对应的图例值当限制线值不是整数时,我们可以将图例添加到限制线值吗?