问题描述
-
在条形图中,我正在创建的第一行和最后一行始终被切成两半(即使我添加了其他条形).这也会导致条形上方的值不正确.我把它夸大了.
In the bar chart I am creating the first and last rows are consistently being cut in half (even if I add additional bars). This also causes the values above the bars to be out of place. I am inflating this within a fragment.
轴也仅增加0.9而不是1.要解决此问题,我需要实现AxisValueFormatter接口吗?
The axis are also only increasing by 0.9 instead of 1. To fix this do I need to implement the AxisValueFormatter interface?
图片:
代码:.java
chart = (BarChart) view.findViewById(R.id.chart1);
// Chart settings
chart.setDrawGridBackground(false);
chart.setHighlightFullBarEnabled(true);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.setDescription("");
// Settings for X-Axis
XAxis xAxis = chart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setEnabled(true);
xAxis.setDrawLabels(true);
xAxis.setPosition(XAxisPosition.BOTTOM);
// Settings for Y-Axis
YAxis leftAxis = chart.getAxisLeft();
YAxis rightAxis = chart.getAxisRight();
leftAxis.setAxisMinValue(0f);
rightAxis.setAxisMinValue(0f);
BARENTRY = new ArrayList<>();
BarEntryLabels = new ArrayList<String>();
BARENTRY.add(new BarEntry(2f, 1));
BARENTRY.add(new BarEntry(3f, 2));
BARENTRY.add(new BarEntry(4f, 3));
BARENTRY.add(new BarEntry(5f, 4));
BARENTRY.add(new BarEntry(6f, 5));
Bardataset = new BarDataSet(BARENTRY, "Projects");
Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
BARDATA = new BarData(Bardataset);
chart.setData(BARDATA);
chart.invalidate(); // refresh
chart.animateY(1500);
return view;
推荐答案
要回答您的问题:
-
要正确显示条形,您需要像这样对条形进行调整(不要问我为什么默认情况下未启用它):
In order to properly show your bars you need to fit the bars, like this (don't ask me why it's not enabled by default):
chart.setFitBars(true)
您可以在BarChart
javadocs中找到更多信息:
You can find more information in the BarChart
javadocs:
如果您有CombinedChart
,则可以使用 setSpaceMin()
和 setSpaceMax()
可以在轴的两端添加额外的间距:
If you have a CombinedChart
you could use setSpaceMin()
and setSpaceMax()
to add additional spacing on both ends of the axis:
XAxis xAxis = chart.getXAxis();
xAxis.setSpaceMin(barData.getBarWidth() / 2f);
xAxis.setSpaceMax(barData.getBarWidth() / 2f);
当前不可能影响轴上渲染的值位置.创建ValueFormatter
仅更改显示的文本,而不更改标签的实际位置.
It is currently impossible to infuence the value positions rendered on the axis. Creating a ValueFormatter
only changes the displayed text, not the actual position of the label.
这篇关于MPAndroidChart-第一栏和最后一栏显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!