首页 > 解决方案 > 将字符串值设置为 MPAndroidChart 条形图的 xaxis 标签

问题描述

我正在尝试将条形图的标签从浮点数格式化为字符串,但它只显示一个标签,如图所示, 显示一个标签的图表 没有格式化程序,这就是我在 图表显示没有格式化程序的标签 这里拥有的格式化程序内部类代码

    private static class MyFormatter extends ValueFormatter {
      public String getAxisLabel(float value, AxisBase axis) {
        if (value == 1) {
            return "JAN"; //make it a string and return
        } else  if (value == 2) {
            return "FEB"; //make it a string and return
        } else if (value == 3) {
            return "MAR"; //make it a string and return
        } else  if (value == 4) {
            return "APR"; //make it a string and return
        } else  if (value == 5) {
            return "MAY"; //make it a string and return
        } else  if (value == 6) {
            return "JUN"; //make it a string and return
        } else  if (value == 7) {
            return "JUL"; //make it a string and return
        } else  if (value == 8) {
            return "AUG"; //make it a string and return
        } else  if (value == 9) {
            return "SEP"; //make it a string and return
        } else  if (value == 10) {
            return "OCT"; //make it a string and return
        } else  if (value == 11) {
            return "NOV"; //make it a string and return
        } else  if (value == 12) {
            return "DEC"; //make it a string and return
        } else {
            return ""; // return empty for other values where you don't want to print anything on the X Axis
        }
    }
}

这是我将值添加到数据集并最终格式化标签的方式

    if (response.body() != null) {
                List<BarEntry> barEntries = new ArrayList<>();
                for (MonthlySales monthlySales : response.body()) {
                    barEntries.add(new BarEntry(monthlySales.getMonth(), monthlySales.getTotal()));
                }
                BarDataSet dataSet = new BarDataSet(barEntries, "Monthly Sales");
                dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
                BarData data = new BarData(dataSet);
                //data.setBarWidth(10f);
                chart.setVisibility(View.VISIBLE);
                chart.animateXY(2000, 2000);
                chart.setData(data);
                chart.setFitBars(true);
                Description description = new Description();
                description.setText("Sales per month");
                chart.setDescription(description);
                chart.invalidate();
                XAxis xAxis = chart.getXAxis();
                //xAxis.setLabelCount(12, true);
                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                //xAxis.setTypeface();
                xAxis.setDrawGridLines(false);
                //xAxis.setValueFormatter(new MyFormatter());
            }

请注意,从数据库返回的月份是一个整数 1-12,但它显示了四个月的浮动可能 - 八月。

我可能做错了什么,如何使用这个库实现我的目标?

标签: javaandroidmpandroidchart

解决方案


您需要做的是使用 Math.round() 将浮点值转换为整数,这将确保检查准确地评估为真,因为目前它们仅在 7.0 时评估为真,因为它等于 7,而其余的正在评估为假,即使它应该是真的你的格式化代码应该是

private static class MyFormatter extends ValueFormatter {
    public String getAxisLabel(float value, AxisBase axis) {
        if (Math.round(value) == 1) {
            return "JAN"; //make it a string and return
        } else  if (Math.round(value) == 2) {
            return "FEB"; //make it a string and return
        } else if (Math.round(value) == 3) {
            return "MAR"; //make it a string and return
        } else  if (Math.round(value) == 4) {
            return "APR"; //make it a string and return
        } else  if (Math.round(value) == 5) {
            return "MAY"; //make it a string and return
        } else  if (Math.round(value) == 6) {
            return "JUN"; //make it a string and return
        } else  if (Math.round(value) == 7) {
            return "JUL"; //make it a string and return
        } else  if (Math.round(value) == 8) {
            return "AUG"; //make it a string and return
        } else  if (Math.round(value) == 9) {
            return "SEP"; //make it a string and return
        } else  if (Math.round(value) == 10) {
            return "OCT"; //make it a string and return
        } else  if (Math.round(value) == 11) {
            return "NOV"; //make it a string and return
        } else  if (Math.round(value) == 12) {
            return "DEC"; //make it a string and return
        } else {
            return ""; // return empty for other values where you don't want to print anything on the X Axis
        }
    }
}

还要记住将粒度功能添加到 x 轴,以便在为大屏幕缩放图表时只显示一个标签。

要使用粒度功能,请将其添加到您的图表创建代码中

    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);

推荐阅读