问题
I just want to display the Y values right above the chart line, I found that the method to do this is setting setDisplayChartValues to true. However when I use it the app crashes with the following error message: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3 being 3 the number of points in the chart.
public class LineGraph {
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
private double max = 0;
private double min = 999;
public View getView(Context ctx, String[] dates, double[] y){
for(int i = 0; i < y.length; i++){
if(y[i] > max)
max = y[i];
if(y[i] < min)
min = y[i];
}
TimeSeries series = new TimeSeries("progress");
for(int i = 0; i < dates.length; i++){
String temp[] = dates[i].split("/");
int year = Integer.parseInt(temp[0]);
int month = Integer.parseInt(temp[1]);
int day = Integer.parseInt(temp[2]);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DAY_OF_MONTH, day);
Date dt = cal.getTime();
series.add(dt, y[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.GREEN);
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(true);
renderer.setLineWidth(3f); // Girth of the chart line
//mRenderer.setZoomButtonsVisible(true);
//mRenderer.setZoomEnabled(false, false);
renderer.setDisplayChartValues(true);
mRenderer.addSeriesRenderer(renderer);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.BLACK);
//mRenderer.setShowGrid(true);
//mRenderer.setXTitle("dias");
mRenderer.setYTitle("Weight (Kg)");
mRenderer.setPointSize(5f);
mRenderer.setPanEnabled(true, false);
mRenderer.setLabelsTextSize(17f);
//Setting the max and min height of the chart
mRenderer.setYAxisMax(max + 2);
mRenderer.setYAxisMin(min - 2);
View view = ChartFactory.getTimeChartView(ctx, dataset, mRenderer, "dd/MMM/yyyy");
return view;
}
by using renderer.setDisplayChartValues(true); the app crashes, without it, works just fine.
回答1:
I suggest you upgrade to the latest 1.1.0 release as this issue has been fixed a while ago. You can download the version here.
回答2:
It's a strange issue, but adding data to your TimeSeries
out of order will cause this. I used a TreeMap
to mimic the TimeSeries
. Then, just iterate through the keys of your sorted TreeMap
and add the data to the TimeSeries
that way.
来源:https://stackoverflow.com/questions/15955081/android-achartengine-setdisplaychartvalues-crashes-app