BarChart 上的 Int 值
Int values on BarChart
我希望条形图上的值是整数。我只设法找出如何使用 ValueFormatter 修改轴上表示的值,但这并不影响图表上的值。这是我目前拥有的:
这是我的代码:
HorizontalBarChart barChart = findViewById(R.id.bar_chart);
ArrayList<BarEntry> yValues = new ArrayList<>();
SharedPreferences appData = getSharedPreferences("app_data", Context.MODE_PRIVATE);
String[] supermarkets = new String[appData.getAll().size()];
int i = 0;
for (Map.Entry<String, ?> entry : appData.getAll().entrySet()) {
int value = (Integer)entry.getValue();
BarEntry e = new BarEntry(i, value, 3);
yValues.add(e);
supermarkets[i] = entry.getKey();
i++;
}
BarDataSet dataSet = new BarDataSet(yValues, "Supermarkets");
dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData data = new BarData(dataSet);
barChart.getXAxis().setDrawGridLines(false);
barChart.getXAxis().setDrawAxisLine(false);
barChart.getAxisLeft().setDrawGridLines(false);
barChart.getAxisRight().setEnabled(false);
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisRight().setDrawGridLines(false);
barChart.getAxisLeft().setAxisMinimum(0);
barChart.getAxisRight().setAxisMinimum(0);
barChart.getXAxis().setValueFormatter(new LabelFormatter(supermarkets));
barChart.getXAxis().setGranularity(1);
barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
barChart.setData(data);
public class LabelFormatter implements IAxisValueFormatter {
private final String[] mLabels;
LabelFormatter(String[] labels) {
mLabels = labels;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mLabels[(int) value];
}
}
您可能想要向 YAxis 添加一个 ValueFormatter,将值作为字符串返回,如下所示:
barChart.getYAxis().setValueFormatter(new IntegerFormatter());
使用这个简单的 class:
public class IntegerFormatter implements IAxisValueFormatter {
private DecimalFormat mFormat;
public MyAxisValueFormatter() {
mFormat = new DecimalFormat("###,###,##0");
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mFormat.format(value);
}
}
我将 DecimalFormat 用于额外的逗号。例如“1234567”应该画成“1,234,567”。有关详细信息,请参阅规范 here. Source
您只能自定义数据集或图表数据,在我的例子中,我选择数据,因为它涵盖了所有数据集。
BarData data = new BarData(set1);
data.setValueFormatter(new IntegerFormatter());
新建class
public class IntegerFormatter extends ValueFormatter {
private DecimalFormat mFormat;
public IntegerFormatter() {
mFormat = new DecimalFormat("###,##0");
}
@Override
public String getBarLabel(BarEntry barEntry) {
return mFormat.format(barEntry.getY());
}
}
我希望条形图上的值是整数。我只设法找出如何使用 ValueFormatter 修改轴上表示的值,但这并不影响图表上的值。这是我目前拥有的:
这是我的代码:
HorizontalBarChart barChart = findViewById(R.id.bar_chart);
ArrayList<BarEntry> yValues = new ArrayList<>();
SharedPreferences appData = getSharedPreferences("app_data", Context.MODE_PRIVATE);
String[] supermarkets = new String[appData.getAll().size()];
int i = 0;
for (Map.Entry<String, ?> entry : appData.getAll().entrySet()) {
int value = (Integer)entry.getValue();
BarEntry e = new BarEntry(i, value, 3);
yValues.add(e);
supermarkets[i] = entry.getKey();
i++;
}
BarDataSet dataSet = new BarDataSet(yValues, "Supermarkets");
dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData data = new BarData(dataSet);
barChart.getXAxis().setDrawGridLines(false);
barChart.getXAxis().setDrawAxisLine(false);
barChart.getAxisLeft().setDrawGridLines(false);
barChart.getAxisRight().setEnabled(false);
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisRight().setDrawGridLines(false);
barChart.getAxisLeft().setAxisMinimum(0);
barChart.getAxisRight().setAxisMinimum(0);
barChart.getXAxis().setValueFormatter(new LabelFormatter(supermarkets));
barChart.getXAxis().setGranularity(1);
barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
barChart.setData(data);
public class LabelFormatter implements IAxisValueFormatter {
private final String[] mLabels;
LabelFormatter(String[] labels) {
mLabels = labels;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mLabels[(int) value];
}
}
您可能想要向 YAxis 添加一个 ValueFormatter,将值作为字符串返回,如下所示:
barChart.getYAxis().setValueFormatter(new IntegerFormatter());
使用这个简单的 class:
public class IntegerFormatter implements IAxisValueFormatter {
private DecimalFormat mFormat;
public MyAxisValueFormatter() {
mFormat = new DecimalFormat("###,###,##0");
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mFormat.format(value);
}
}
我将 DecimalFormat 用于额外的逗号。例如“1234567”应该画成“1,234,567”。有关详细信息,请参阅规范 here. Source
您只能自定义数据集或图表数据,在我的例子中,我选择数据,因为它涵盖了所有数据集。
BarData data = new BarData(set1);
data.setValueFormatter(new IntegerFormatter());
新建class
public class IntegerFormatter extends ValueFormatter {
private DecimalFormat mFormat;
public IntegerFormatter() {
mFormat = new DecimalFormat("###,##0");
}
@Override
public String getBarLabel(BarEntry barEntry) {
return mFormat.format(barEntry.getY());
}
}