我正在使用 MPAndroidChart 库。我正在使用 CustomValueFormatter 格式化 Float 值,使其精度为 1。
CustomValueFormatter 代码:
public class CustomYAxisValueFormatter implements YAxisValueFormatter {
private DecimalFormat mFormat;
public CustomYAxisValueFormatter() {
mFormat = new DecimalFormat("###,###,###,##0.0"); // sets precision to 1
}
@Override
public String getFormattedValue(float value, YAxis yAxis) {
return mFormat.format(value);
}
}
我正在将格式化程序设置为 y 轴。
设置格式化程序:
YAxis yAxis = lineChart.getAxisLeft(); //show left y-axis line
yAxis.setValueFormatter(new CustomYAxisValueFormatter()); // set value formatter to format y-values.
作为 setValueFormatter(YAxisValueFormatter)
的结果,上面的格式化程序 (CustomYAxisValueFormatter)
是默认创建的。
问题是无法在缩放时重新创建 CustomYAxisValueFormatter,从而导致重复的 y 值。
是否可以创建一个根据缩放级别更改值精度的 CustomValueFormatter?
最佳答案
你试过setOnChartGestureListener
lineChart.setOnChartGestureListener(new OnChartGestureListener() {
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
}
@Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
if(lastPerformedGesture == ChartTouchListener.ChartGesture.PINCH_ZOOM) {
YAxis yAxis = lineChart.getAxisLeft(); //show left y-axis line
yAxis.setValueFormatter(new CustomYAxisValueFormatter()); // setting your new value formatter
}
}
@Override
public void onChartLongPressed(MotionEvent me) {
}
@Override
public void onChartDoubleTapped(MotionEvent me) {
}
@Override
public void onChartSingleTapped(MotionEvent me) {
}
@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
}
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
}
@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
}
});
关于android - MPAndroidChart - 如何在缩放时更改 ValueFormatter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36028356/