我自定义了一个IAxisValueFormatter来自定义X轴的值,
/**
* Created by Philipp Jahoda on 20/09/15.
* Custom formatter interface that allows formatting of
* axis labels before they are being drawn.
*/
public interface IAxisValueFormatter
{
/**
* Called when a value from an axis is to be formatted
* before being drawn. For performance reasons, avoid excessive calculations
* and memory allocations inside this method.
*
* @param value the value to be formatted
* @param axis the axis the value belongs to
* @return
*/
String getFormattedValue(float value, AxisBase axis);
}
就是这个接口,刚开始我以为它传给我的只会是1.0,2.0,3.0这样的浮点值,结果发现不是的,什么样的值都会有。
情况是这样的,我的界面中有个LineChart这个控件,这个控件第一次显示是正常的,也就是说
String getFormattedValue(float value, AxisBase axis);
这个方法里传给我的value都是1.0,2.0,3.0这样的值,由于项目功能需要,有的时候需要清除掉之前的数据,重新加载数据并显示所以我就先清除数据。
//清除数据
private void clearChartData(LineChart chart) {
LineData data = chart.getLineData();
if (data != null) {
//data.clearValues();
//chart.getXAxis().resetAxisMaximum();
// chart.clearValues();
chart.clear();
// chart.setVisibleXRangeMaximum(10);
//chart.moveViewToX(0);
//chart.setLeft(0);
}
}
再重新加载数据:
/**
* 增加数据
*
* @param data 增加的数据的y轴的值
*/
public static void addDataForTime(float data, LineChart chart, Context context) {
if (chart.getData() == null ||
chart.getData().getDataSetCount() < 1 || chart.getData().getEntryCount() == 0) {
ChartUtils.initMPChart(chart, context, data, 10, new DayAxisValueFormatter(20));
return;
}
chart.getData().getDataSetByIndex(0).addEntry(new Entry(chart.getData().getDataSetByIndex(0).getEntryCount(),
data, context.getResources().getDrawable(R.drawable.ic_launcher)));
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
if (chart.getData().getDataSetByIndex(0).getEntryCount() > 10) {
Log.e("infoo", "maxAxis = " + chart.getXAxis().getAxisMaximum() + " entryCount = "
+ chart.getData().getDataSetByIndex(0).getEntryCount() + " left = " + chart.getLeft());
chart.getXAxis().resetAxisMaximum();
chart.setVisibleXRangeMaximum(10);
chart.moveViewToX(chart.getData().getDataSetByIndex(0).getEntryCount() - 10);
// chart.setLeft(chart.getData().getDataSetByIndex(0).getEntryCount() - 10);
} else {
chart.invalidate();
}
}
/**
* 对表格进行初始化
*
* @param chart 初始化的MPChart
* @param context activity上下文
* @param value 第一个值
*/
public static void initMPChart(LineChart chart, Context context, float value, int count, IAxisValueFormatter iAxisValueFormatter) {
Log.e("infoo", "初始化表格");
ArrayList<Entry> values = new ArrayList<>();
values.add(new Entry(0, value, context.getResources().getDrawable(R.drawable.ic_launcher)));
LineDataSet set1;
// create a dataset and give it a type
set1 = new LineDataSet(values, "");
set1.setDrawIcons(false);
// 设置数据的说明
chart.getLegend().setForm(Legend.LegendForm.EMPTY);
Description description = new Description();
description.setText("");
chart.setDescription(description);
//禁用缩放图表上的两个轴
chart.setScaleEnabled(false);
//修改x轴线的样式
XAxis xAxis = chart.getXAxis();
xAxis.setAxisLineWidth(1);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisLineColor(ContextCompat.getColor(context, R.color.black));
xAxis.setGridColor(ContextCompat.getColor(context, R.color.LightGrey));
xAxis.setGridLineWidth(1);
xAxis.enableGridDashedLine(3, 3, 0);
xAxis.setTextSize(18);
xAxis.setAxisMaximum(count);
xAxis.setAxisMinimum(0);
if (iAxisValueFormatter != null) {
xAxis.setValueFormatter(iAxisValueFormatter);
}
//修改y轴的样式
YAxis leftAxis = chart.getAxisLeft();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
leftAxis.setGridLineWidth(1);
leftAxis.enableGridDashedLine(3, 3, 0);
leftAxis.setAxisLineColor(ContextCompat.getColor(context, R.color.black));
leftAxis.setStartAtZero(true);
leftAxis.setTextSize(18);
//设置折线的一些属性
set1.setDrawValues(false);
set1.setColor(ContextCompat.getColor(context, R.color.DeepSkyBlue));
set1.setLineWidth(1f);
set1.setCircleRadius(3f);
set1.setDrawCircleHole(false);
set1.setDrawCircles(false);
set1.setValueTextSize(9f);
set1.setDrawFilled(true);
set1.setFormLineWidth(1f);
set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
set1.setFormSize(15.f);
if (Utils.getSDKInt() >= 18) {
// 填充 drawable
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.fade_red);
set1.setFillDrawable(drawable);
} else {
set1.setFillColor(ContextCompat.getColor(context, R.color.LightBLue));
}
// create a data object with the datasets
LineData data = new LineData(set1);
// set data
chart.setData(data);
chart.setNoDataText("还没有任何数据哦");
chart.moveViewToX(0);
//chart.invalidate();
/* for (float f : xAxis.mEntries) {
Log.e("infoo", "float = " + f);
}*/
}
有的时候是正常的,还是会传给我1.0,2.0,3.0这样的值,但是有的时候,会传给我一些莫名其妙的float值,比如说,0,02,0,32这样的,很无赖。然后去看它的源码,发现它会在BarLineChartBase.java里面调用这样一个方法:
if (mXAxis.isEnabled()){
// Log.e("infoo", "min = " + mXAxis.mAxisMinimum + " max = " + mXAxis.mAxisMaximum);
mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false);
}
然后一直往里面走,会发现是这样的方法:
/**
* Transforms the given array of touch positions (pixels) (x, y, x, y, ...)
* into values on the chart.
*
* @param pixels
*/
public void pixelsToValue(float[] pixels) {
Matrix tmp = mPixelToValueMatrixBuffer;
tmp.reset();
mMatrixOffset.invert(tmp);
tmp.mapPoints(pixels);
mViewPortHandler.getMatrixTouch().invert(tmp);
tmp.mapPoints(pixels);
mMatrixValueToPx.invert(tmp);
tmp.mapPoints(pixels);
}
看着很懵逼,有没有人遇到过这个情况的?