gpt4 book ai didi

android - x 轴值未在堆积条形图中全部显示

转载 作者:行者123 更新时间:2023-11-29 23:46:58 24 4
gpt4 key购买 nike

您可以在下面找到屏幕截图以及我的代码;

enter image description here

代码如下;

      mChart = view.findViewById(R.id.bar_chart);
barWidth = 0.3f;
barSpace = 0.02f;
groupSpace = 0.4f;

mChart.setDescription(null);
mChart.setPinchZoom(false);
mChart.setScaleEnabled(false);
mChart.setDrawBarShadow(false);
mChart.setDrawGridBackground(false);

mChart.getAxisLeft().setDrawGridLines(false);
mChart.getXAxis().setDrawGridLines(false);
mChart.setBackgroundColor(Color.TRANSPARENT);

ArrayList<BarEntry> yValues = new ArrayList<>();
yValues.add(new BarEntry(5, new float[]{10, 20, 30, 50}));
yValues.add(new BarEntry(15, new float[]{12, 13}));
yValues.add(new BarEntry(25, new float[]{15, 15}));
yValues.add(new BarEntry(35, new float[]{17, 17}));

BarDataSet set = new BarDataSet(yValues, "");
set.setColors(new int[]{Color.rgb(67, 67, 72), Color.rgb(124, 181, 236),
Color.rgb(124, 181, 236), Color.rgb(124, 181, 236)});
set.setStackLabels(new String[]{
"Men", "Women", "fgdgfx", "gfdrhd"
});
BarData data = new BarData(set);
data.setBarWidth(1.9f);
mChart.setData(data);
mChart.invalidate();

final ArrayList xVals = new ArrayList();
xVals.add("New");
xVals.add("Accepted");
xVals.add("Completed");
xVals.add("Cancelled");

XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

xAxis.setTextColor(Color.RED);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
xAxis.setCenterAxisLabels(true);
xAxis.setAxisMinimum(0.4f);
xAxis.setGranularity(4f);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));

mChart.getAxisRight().setEnabled(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setValueFormatter(new LargeValueFormatter());
leftAxis.setDrawGridLines(false);
leftAxis.setSpaceTop(35f);
leftAxis.setAxisMinimum(0f);
leftAxis.setGranularity(1.0f);
leftAxis.setGranularityEnabled(true);

Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT);
l.setWordWrapEnabled(true);

最佳答案

问题是 float value 是在 Formatter 的 getFormattedValue 方法中获取的。您得到的值是 (0,10,20,30) 并且您为 x 轴值提供的数组的大小仅为 4。这就是为什么它只打印第一个 x 值而不打印其余值的原因。

请按以下方式更改您的代码。

    xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
try {
return xVals.get((int) (value / 10));//dividing the value by 10 to get the multiplied value.
} catch (Exception e){
return "";
}
}
});

关于android - x 轴值未在堆积条形图中全部显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51242601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com