【问题标题】:Set Label is not working in Bar Chart using MPAndroid Library使用 MPAndroid 库在条形图中设置标签不起作用
【发布时间】:2018-07-02 01:49:31
【问题描述】:

我正在使用 MPAndroid 库。 实施 'com.github.PhilJay:MPAndroidChart:v3.0.3' 我需要用标签创建条形图。 我的代码如下,但它不起作用: 这是要显示的条形数组。

public ArrayList<BarEntry> getBarEntryArrayList() {

    ArrayList<BarEntry> barEntries = new ArrayList<BarEntry>();
    barEntries.add(new BarEntry(2f, 0));
    barEntries.add(new BarEntry(4f, 1));
    barEntries.add(new BarEntry(6f, 2));
    barEntries.add(new BarEntry(8f, 3));
    barEntries.add(new BarEntry(7f, 4));
    barEntries.add(new BarEntry(3f, 5));
    return barEntries;

}

这是显示在底部的标签数组。

public ArrayList<String> getBarEntryLabels() {

    ArrayList<String> BarEntryLabels = new ArrayList<String>();
    BarEntryLabels.add("J");
    BarEntryLabels.add("F");
    BarEntryLabels.add("M");
    BarEntryLabels.add("A");
    BarEntryLabels.add("M");
    BarEntryLabels.add("J");
    BarEntryLabels.add("J");
    BarEntryLabels.add("A");
    BarEntryLabels.add("S");
    BarEntryLabels.add("O");
    BarEntryLabels.add("N");
    BarEntryLabels.add("D");
    return BarEntryLabels;

}

标签数组和入口数组设置问题

private void setBarChartData() {
    BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), getBarEntryLabels() );
    barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
    BarData barData = new BarData(barDataSet);
    barChart.setData(barData);
    barChart.animateY(3000);

}

这条线不工作。

BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), getBarEntryLabels() );

如果制作没有标签的代码,它就可以工作。

BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), "Dummy text" );

我必须使用标签数组。 提前致谢。

我需要像这样创建图表。 请帮帮我。

【问题讨论】:

  • 哥们,您想要 xAxis 上的标签或作为条形上方或内部条形上的值吗?

标签: java android char mpandroidchart


【解决方案1】:

创建栏条目时,添加标签作为第三个参数:

    barEntries.add(new BarEntry(2f, 0, "J"));
    barEntries.add(new BarEntry(4f, 1, "F"));
    barEntries.add(new BarEntry(6f, 2, "M"));
    barEntries.add(new BarEntry(8f, 3, "A"));
    barEntries.add(new BarEntry(7f, 4, "M"));
    barEntries.add(new BarEntry(3f, 5, "P"));

barChart.setData(barData); 之后,您可以使用setValueFormatter 设置条形上方或下方的标签:

    barChart.getBarData().setValueFormatter(new IValueFormatter() {
        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
            return entry.getData().toString();
        }
    });

同时增加字体大小:

barChart.getData().setValueTextSize(15);

附:你不需要getBarEntryLabels 方法。

结果如下:

【讨论】:

  • 3.0.3 版本支持,测试项目和屏幕是用最新版本的库制作的。
  • 哦,对不起,伙计,对不起,我试过了,但没用。可能是我做错了什么。我收回我的话,伙计:)
  • 但用户希望在 xAxis 上显示标签,而不是我理解的条形上方的值。
  • 我在那种情况下说你是对的,但两个概念存在混淆。所以让我们问问原始海报他到底想要什么:)
  • 没问题 ;) 这里的主要思想是使用setValueFormatter。对于映射值,最好将标签放在 BarEntry 对象中作为第三个参数。他可以在用户喜欢的任何地方在顶部、底部、X 轴等处进行操作。没有明确说明应该在哪里显示标签
【解决方案2】:

您需要执行以下操作:

barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(BarEntryLabels));

快乐编码:)

【讨论】:

【解决方案3】:

你可以按如下方式设置,

在 Kotlin 中,

val xAxis = chart.xAxis
xAxis.valueFormatter = IndexAxisValueFormatter(labels)

如果您仍然遇到问题,请确保您没有隐藏 x 轴。如果您要隐藏 x 轴,则不会显示标签。如果你想隐藏 x 轴,你必须让 x 轴可见并给它一个透明的颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多