英文:
MPAndroidChart - Bar Chart not showing all X-axis labels
问题
2020年8月14日:以下是更新的信息
我在我的活动中有一个条形图,我正在使用IndexAxisValueFormatter
来显示自定义标签。我希望在显示的所有条形下方显示标签。但是,如下面的截图所示,图表只在某些条形下显示标签。即使缩放,这个问题仍然存在。
当显示图表时,标签的位置是无法阅读的(见下面的截图),但在与图表的任何交互之后,这个问题得到了修复(但并不是所有的标签都显示)。
我尝试过使用XAxis.setLabelCount(entries.size(), true)
,并将强制布尔值设置为true,但渲染没有变化。还设置了通过BarChart.setVisibleXRangeMaximum(3);
显示的条形的限制,但也没有帮助。
显示数据的方法如下:
private void plotDataBarGraph(int[] xCoordinates, int[] yCoordinates, String label, String[] labels){
BarChart graph = findViewById(R.id.chart);
graph.setScaleYEnabled(false);
List<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < xCoordinates.length; i++){
entries.add(new BarEntry(xCoordinates[i], yCoordinates[i])) ;
}
Collections.sort(entries, new EntryXComparator());
BarDataSet dataSet = new BarDataSet(entries,label);
dataSet.setColor(Color.BLUE);
dataSet.setValueTextColor(Color.BLACK);
BarData lineData = new BarData(dataSet);
graph.setData(lineData);
XAxis xAxis = graph.getXAxis();
xAxis.setLabelCount(entries.size(), true);
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
xAxis.setLabelRotationAngle(-45);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
graph.invalidate(); // 刷新
}
这是活动首次创建时图表的显示方式:
这是与图表互动(双击或缩放)后图表的显示方式:
我不确定如何解决这个问题,或者为什么会发生这种情况。非常感谢您的任何帮助!
**更新:**如果我用CombinedChart
替换我的BarChart
,那么标签会按预期显示。但是,这对于我需要创建的内容不合适,因为我想要创建一个分组的条形图(显示同一月份的两组数据)。有没有办法使用CombinedChart
来实现这一点?分配两个BarData
给它只会绘制分配的第二个。
英文:
14-Aug-2020: Posted an Update Below
I have a BarChart in my activity, and I am using an IndexAxisValueFormatter
to display custom labels. I want labels under all of the bars that are displayed. However, the graph only shows labels under certain bars, as in the screenshot below. This problem persists even when zooming.
When the graph is displayed, the positioning of the labels is such that they are unreadable (see screenshot below), but this gets fixed after any interaction with the chart (but all labels never show).
I have tried using the XAxis.setLabelCount(entries.size(), true)
with the force boolean set to true, but there was no change in the rendering. Setting a limit on the bars shown with BarChart.setVisibleXRangeMaximum(3);
also does not help.
The method that displays the data is:
private void plotDataBarGraph(int[] xCoordinates, int[] yCoordinates, String label, String[] labels){
BarChart graph = findViewById(R.id.chart);
graph.setScaleYEnabled(false);
List<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < xCoordinates.length; i++){
entries.add(new BarEntry(xCoordinates[i], yCoordinates[i])) ;
}
Collections.sort(entries, new EntryXComparator());
BarDataSet dataSet = new BarDataSet(entries,label);
dataSet.setColor(Color.BLUE);
dataSet.setValueTextColor(Color.BLACK);
BarData lineData = new BarData(dataSet);
graph.setData(lineData);
XAxis xAxis = graph.getXAxis();
xAxis.setLabelCount(entries.size(), true);
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
xAxis.setLabelRotationAngle(-45);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
graph.invalidate(); // refresh
}
This is how the chart displays when the activity is first created:
This is how the chart displays when the chart is interacted with (double tap or zoom):
I am not sure how to fix this issue, or why it is happening. Any assistance will be greatly appreciated!
Update: If I replace my BarChart
with a CombinedChart
, then the labels appear as expected. However, this is not suitable for what I need as I want to create a grouped bar chart (displaying two sets of data for the same month). Is there any way to achieve this using a CombinedChart
? Assigning it two BarData
makes it only draw the second one that was assigned.
答案1
得分: 1
尝试删除 xAxis.setLabelCount(entries.size(), true)
。默认情况下,X 轴会显示所有其值。除非您希望指定数量,否则无需强制设置标签计数。
来自文档:
/**
* 设置y轴标签条目的数量,最大=25,最小=2,默认值=6,注意这个数量不是固定的(如果force==false),并且只能近似。
*
* @param count 要显示的y轴标签数量
* @param force 如果启用,将强制设置标签计数,意味着将绘制精确指定数量的标签,并均匀分布在轴旁边 - 这可能会导致标签具有不均匀的值
*/
public void setLabelCount(int count, boolean force) {
setLabelCount(count);
mForceLabels = force;
}
英文:
Try removing xAxis.setLabelCount(entries.size(), true)
X axis shows all its values by default. There is no need to set the labels count by force unless you want a specified number.
From documentation:
/**
* sets the number of label entries for the y-axis max = 25, min = 2, default: 6, be aware
* that this number is not
* fixed (if force == false) and can only be approximated.
*
* @param count the number of y-axis labels that should be displayed
* @param force if enabled, the set label count will be forced, meaning that the exact
* specified count of labels will
* be drawn and evenly distributed alongside the axis - this might cause labels
* to have uneven values
*/
public void setLabelCount(int count, boolean force) {
setLabelCount(count);
mForceLabels = force;
}
答案2
得分: 1
我也遇到了相同的问题。您应该使用false
来强制显示所有标签。
我的问题已经通过以下方式解决:
barChart.getXAxis().setLabelCount(entries.size(), false);
true
强制显示所有标签无效。我正在使用版本 v3.1.0。
英文:
I also had the same problem. You should use force false
to show all labels.
My issue was resolved with this:
barChart.getXAxis().setLabelCount(entries.size(), false);
force true
not showing all labels. I am using this version v3.1.0
答案3
得分: 0
对于 v3.1.0
我遇到了相同的问题。我通过以下方式解决了它:xAxis.setLabelCount(entries.size, true)
如果不起作用,请检查库版本。
英文:
For v3.1.0
I had the same problem. I solved it with this: xAxis.setLabelCount(entries.size, true)
If it doesn't work, check the library version.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论