我只想在图表线的正上方显示 Y 值,我发现执行此操作的方法是将 setDisplayChartValues 设置为 true。但是,当我使用它时,应用程序崩溃并显示以下错误消息:java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3 is 3 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;
}
通过使用 renderer.setDisplayChartValues(true); 应用程序崩溃,没有它,工作得很好。