将字符串值设置为 MPAndroidChart 条形图的 x 轴标签

huangapple go评论67阅读模式
英文:

Setting string values as xaxis labels of an MPAndroidChart bar graph

问题

这里是格式化条形图标签的问题。在没有使用格式化器的情况下,标签显示为浮点数,但只显示一个标签。您想要将这些标签格式化为字符串。以下是您提供的格式化器内部类的代码:

private static class MyFormatter extends ValueFormatter {
    public String getAxisLabel(float value, AxisBase axis) {
        if (value == 1) {
            return "JAN";
        } else if (value == 2) {
            return "FEB";
        } else if (value == 3) {
            return "MAR";
        } else if (value == 4) {
            return "APR";
        } else if (value == 5) {
            return "MAY";
        } else if (value == 6) {
            return "JUN";
        } else if (value == 7) {
            return "JUL";
        } else if (value == 8) {
            return "AUG";
        } else if (value == 9) {
            return "SEP";
        } else if (value == 10) {
            return "OCT";
        } else if (value == 11) {
            return "NOV";
        } else if (value == 12) {
            return "DEC";
        } else {
            return ""; // 返回空字符串,用于不在 X 轴上打印任何内容的其他值
        }
    }
}

然后,在您将值添加到数据集并最终格式化标签的部分,您需要为 X 轴设置值格式化器:

XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new MyFormatter()); // 设置自定义格式化器

这将使用您的自定义格式化器来将 X 轴的标签从浮点数转换为相应的字符串。根据您的描述,这应该能够帮助您实现您的目标。

英文:

I am trying to format the labels of by bar chart from float to strings but It is only showing one label as shown here
将字符串值设置为 MPAndroidChart 条形图的 x 轴标签
without the formatter this is what I have
将字符串值设置为 MPAndroidChart 条形图的 x 轴标签
here it the formatter inner class code

    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
        }
    }
}

here is how I am adding the values to the dataset and eventually formatting the labels

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

note that the returned months from the database is an integer 1-12 but it is showing the floats of the four months may - august.

What could I be doing wrong and how can I achieve my target with this library?

答案1

得分: 0

你需要做的是使用Math.round()将你的浮点数值转换为整数,这将确保检查准确地评估为true,因为现在它们只在7.0时评估为true,因为它等于7,而其余的在应该是true的情况下评估为false。
因此,你的格式化代码应该是:

private static class MyFormatter extends ValueFormatter {
    public String getAxisLabel(float value, AxisBase axis) {
        if (Math.round(value) == 1) {
            return "JAN"; //转换为字符串并返回
        } else if (Math.round(value) == 2) {
            return "FEB"; //转换为字符串并返回
        } else if (Math.round(value) == 3) {
            return "MAR"; //转换为字符串并返回
        } else if (Math.round(value) == 4) {
            return "APR"; //转换为字符串并返回
        } else if (Math.round(value) == 5) {
            return "MAY"; //转换为字符串并返回
        } else if (Math.round(value) == 6) {
            return "JUN"; //转换为字符串并返回
        } else if (Math.round(value) == 7) {
            return "JUL"; //转换为字符串并返回
        } else if (Math.round(value) == 8) {
            return "AUG"; //转换为字符串并返回
        } else if (Math.round(value) == 9) {
            return "SEP"; //转换为字符串并返回
        } else if (Math.round(value) == 10) {
            return "OCT"; //转换为字符串并返回
        } else if (Math.round(value) == 11) {
            return "NOV"; //转换为字符串并返回
        } else if (Math.round(value) == 12) {
            return "DEC"; //转换为字符串并返回
        } else {
            return ""; // 对于其他你不想在X轴上打印任何内容的值,返回空字符串
        }
    }
}

同时,记得在x轴上添加粒度特性,以便在大屏幕上缩放图表时只显示一个标签。

要使用粒度特性,请将以下内容添加到你的图表创建代码中:

xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
英文:

What you need to do is convert your floating point values to integer using Math.round() this will ensure that the checks evaluate to true accurately since as at now they are only evaluating to true at 7.0 since that is equal to 7 while the rest are evaluating to false even when it should be true
your formatting code should thus be

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
}
}
}

Also remember to add the granularity feature to your x axis so that only one label is shown when the charts are zoomed for large screens.

to use granularity feature add this to your chart creation code

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

huangapple
  • 本文由 发表于 2020年10月7日 19:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64243261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定