MPAndroidChart:如何反转 HorizontalBarChart 的顺序?
MPAndroidChart: How to reverse the order of a HorizontalBarChart?
只是希望这里有人可以解释如何反转 MPAndroidChart 屏幕截图中显示的 HorizontalBarChart 的顺序(因此 44.0 不是在顶部,而是在底部)。
下面的代码显示了我如何创建用于生成 HorizontalBarChart 的 BarDataSet。
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
yVals1.add(new BarEntry(i * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1");
我试图反转 for 循环,以便以相反的方式添加数据集,但似乎生成了相同的 HorizontalBarChart。
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = count-1; i >= 0; i--) {
float val = (float) (Math.random() * range);
yVals1.add(new BarEntry(i * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1");
希望有人能告诉我一个颠倒这个图表顺序的方法。
我已经尝试了下面的代码,这是建议的答案之一。我之前发现他们共享的 link 完全相同。这行代码翻转了图表,所以它在另一边(见第二个截图)
mChart.getAxisLeft().setInverted(true);
chart.getAxisLeft().setInverted(true);
摘自 GitHub PhilJay/MPAndroidChart.
Yes. You can invert y-axes if you want to:
chart.getAxisLeft().setInverted(true);
This will let high values appear on the bottom of the chart, low values on top.
The x-axis cannot be manipulated in order.
Be aware that this feature is not included in the latest pre-release, only in the commits ahead.
您刚刚错过了用 (count - i) * spaceForBar
替换 i * spaceForBar
。
所以渲染时的坐标顺序变为(假设count = 10
):
(10 * 2, 0 * 4) => (9 * 2, 1 * 4) => (8 * 2, 2 * 4) => ... => (0 * 2, 10 * 4)
因此您的代码变为:
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry> ();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random () * range);
yVals1.add (new BarEntry ((count - i) * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet (yVals1, "DataSet 1");
只是希望这里有人可以解释如何反转 MPAndroidChart 屏幕截图中显示的 HorizontalBarChart 的顺序(因此 44.0 不是在顶部,而是在底部)。
下面的代码显示了我如何创建用于生成 HorizontalBarChart 的 BarDataSet。
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
yVals1.add(new BarEntry(i * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1");
我试图反转 for 循环,以便以相反的方式添加数据集,但似乎生成了相同的 HorizontalBarChart。
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = count-1; i >= 0; i--) {
float val = (float) (Math.random() * range);
yVals1.add(new BarEntry(i * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1");
希望有人能告诉我一个颠倒这个图表顺序的方法。
我已经尝试了下面的代码,这是建议的答案之一。我之前发现他们共享的 link 完全相同。这行代码翻转了图表,所以它在另一边(见第二个截图)
mChart.getAxisLeft().setInverted(true);
chart.getAxisLeft().setInverted(true);
摘自 GitHub PhilJay/MPAndroidChart.
Yes. You can invert y-axes if you want to: chart.getAxisLeft().setInverted(true); This will let high values appear on the bottom of the chart, low values on top. The x-axis cannot be manipulated in order. Be aware that this feature is not included in the latest pre-release, only in the commits ahead.
您刚刚错过了用 (count - i) * spaceForBar
替换 i * spaceForBar
。
所以渲染时的坐标顺序变为(假设count = 10
):
(10 * 2, 0 * 4) => (9 * 2, 1 * 4) => (8 * 2, 2 * 4) => ... => (0 * 2, 10 * 4)
因此您的代码变为:
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry> ();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random () * range);
yVals1.add (new BarEntry ((count - i) * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet (yVals1, "DataSet 1");