我正在学习如何使用MPAndroidChart创建一个堆叠的BarChart。遵循本指南:https://github.com/PhilJay/MPAndroidChart/wiki/The-AxisValueFormatter-interface
我已经建立了新的类,实现了那个接口,并设置了新的格式。
barChart.getXAxis().setValueFormatter(new MyXAxisValueFormatter(arrayLabel));但是Android studio显示了一个错误:方法setValueFormatter需要一个ValueFormatter作为参数。IDE建议MyXAxisValueFormatter需要扩展ValueFormatter。我也这样做了,但它不起作用。
如果有人能帮我的话我将不胜感激。
MyXAxisValueFormatter.java
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import java.text.SimpleDateFormat;
import java.util.Date;
@SuppressWarnings("deprecation")
public class MyXAxisValueFormatter implements IAxisValueFormatter {
private String[] mValues;
public MyXAxisValueFormatter(String[] values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
// "value" represents the position of the label on the axis (x or y)
return mValues[(int) value];
}
/** this is only needed if numbers are returned, else return 0 */
public int getDecimalDigits() { return 0; }
}我用来设置图表的代码:
barChart = root.findViewById(R.id.barChart);
barChart.setTouchEnabled(true);
barChart.setPinchZoom(true);
barChart.clear();
BarDataSet dataSet;
ArrayList<BarEntry> values = new ArrayList<>();
Calendar start = Calendar.getInstance();
Calendar end = start.add(Calendar.DATE, 1);
Calendar dateCounter = start;
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
ArrayList<String> xAxisLabel = new ArrayList<>();
int i = 0;
while(dateCounter.compareTo(end) <= 0) {
values.add(new BarEntry(
i,
new float[]{0.0, 1.0, 2.0, 3.0},
getResources().getDrawable(R.drawable.star)));
xAxisLabel.add(i,sdf.format(dateCounter.getTime()));
i++;
dateCounter.add(Calendar.DATE, 1);
}
if (barChart.getData() != null &&
barChart.getData().getDataSetCount() > 0) {
dataSet = (BarDataSet) barChart.getData().getDataSetByIndex(0);
dataSet.setValues(values);
barChart.getData().notifyDataChanged();
barChart.notifyDataSetChanged();
} else {
dataSet = new BarDataSet(values, "test chart");
dataSet.setDrawIcons(false);
dataSet.setColors(getColors());
dataSet.setStackLabels(new String[]{"A", "B", "C", "D"});
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(dataSet);
BarData data = new BarData(dataSets);
data.setValueFormatter(new StackedValueFormatter(true, "", 1));
data.setValueTextColor(Color.WHITE) ;
//data.setValueFormatter(new FlooringValueFormatter(2, 0.5f));
barChart.setData(data);
}
String[] arrayLabel = new String[xAxisLabel.size()];
for(int count = 0; count < xAxisLabel.size(); count++){
arrayLabel[count] = xAxisLabel.get(count);
}
barChart.getXAxis().setValueFormatter(new MyXAxisValueFormatter(arrayLabel));
barChart.setFitBars(false);
barChart.invalidate();发布于 2020-05-21 06:08:03
使用
extends ValueFormatter而不是
implements IAxisValueFormatter并更改此方法:
@Override
public String getFormattedValue(float value, AxisBase axis) {
if((int) value < 0 && (int) value > mValues.length - 1){
return "";
}
return mValues[(int) value];
}您的barEntries应该是这样的:
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry(0, value1);
barEntries.add(new BarEntry(1, value2);如下所示设置轴位置:
barChart.getXAxis().setValueFormatter(new MyXAxisValueFormatter(arrayLabel));
barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);发布于 2020-11-17 07:01:04
ValueFormatter中的getFormattedValue有多个重写,所以我的解决方案是:
**data_or_axis**.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return months[(int)value];
}
});最好的
https://stackoverflow.com/questions/61923489
复制相似问题