MP android图表在水平条形图的X轴上显示浮点值? [英] MP android chart displaying float values at X-Axis of Horizontal bar Chart?
问题描述
我正在使用MP图表显示评论,除了X轴值显示为浮动值(针对特定评论类型的数量)之外,其他所有功能都正常运行.
I am using MP chart for displaying reviews, everything is working fine except that the the X-axis values are displaying as a floating value (for number of particular type of review).
这是我正在使用的代码:
This is the code I am using:
BarData data = new BarData(getXAxisValues(), getDataSet(properties));
chart.setData(data);
chart.getXAxis().setEnabled(false); // hides horizontal grid lines inside chart
YAxis leftAxis = chart.getAxisLeft();
chart.getAxisRight().setEnabled(false); // hides horizontal grid lines with below line
leftAxis.setEnabled(false); // hides vertical grid lines inside chart
/*chart.animateXY(2000, 2000);*/ // for animating reviews display
chart.invalidate();
chart.setClickable(false);
chart.setDescription(""); // Hide the description
chart.getLegend().setEnabled(false);
chart.setDoubleTapToZoomEnabled(false);
chart.setPinchZoom(false);
leftAxis.setDrawLabels(true);
private ArrayList<BarDataSet> getDataSet(Properties properties) {
ArrayList<BarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
BarEntry v1e1 = new BarEntry(properties.getRating10().intValue(), 0);
valueSet1.add(v1e1);
BarEntry v1e2 = new BarEntry(properties.getRating20().intValue(), 1);
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(properties.getRating30().intValue(), 2);
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(properties.getRating40().intValue(), 3);
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(properties.getRating50().intValue(), 4);
valueSet1.add(v1e5);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Asset");
barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("12.0");
xAxis.add("4.0");
xAxis.add("4");
xAxis.add("4");
xAxis.add("4");
return xAxis;
}
在这里,我正在将数据设置为水平条形图.
Here I am setting data to horizontal bar chart.
我希望红色圆圈中的值是整数而不是浮点数. 谁能帮我吗?
I want the red circled values to be in integers instead of float. Can anyone help me?
推荐答案
我已经解决了(再次感谢Philipp),这是解决方案
I have got it solved (thanks again Philipp), This is the solution
创建自己的实现给定ValueFormatter的值格式化程序类
Create Your own value formatter class that implements the given ValueFormatter
这是我的课程:
public class MyValueFormatter implements ValueFormatter{
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return Math.round(value)+"";
}
}
然后将值Formatter设置为BarData:
Then set the value Formatter to your BarData:
BarData data = new BarData(getXAxisValues(), getDataSet(properties));
data.setValueFormatter(new MyValueFormatter());
就是这样,这就是输出-
That's it this is the output-
这篇关于MP android图表在水平条形图的X轴上显示浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!