AchartEngine重现奇怪的行为

时间:2013-10-18 09:01:15

标签: android achartengine

我有一个应用程序包含一些使用AChartEngine库的图表,在用户从服务器下载一些新数据后会自动更新。图表只包含固定数量的点,每次下载后,我将新数据添加到图表中并删除最旧的保存数量的显示数据,然后我调用重绘功能。但重绘不起作用,因为旧的数据继续显示在图表上,只有当我重新启动应用程序时,它们才会消失。这是实现Graph小部件的类的代码

public class LineGraph {
Context context;
GraphicalView graphicalView = null;
TimeSeries tempSeries = null;
TimeSeries humSeries = null;
XYMultipleSeriesDataset dataset = null;
XYSeriesRenderer rendererTemp = null;
XYSeriesRenderer rendererHum = null;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

public LineGraph(Context context) {
    this.context = context;
}

// build empty graph
public View buildGraph(Fragment f) {
    dataset = new XYMultipleSeriesDataset();
    // set properties
    if (f.getClass().equals(TempFragment.class)
            || f.getClass().equals(MixFragment.class)) {
        tempSeries = new TimeSeries("");
        dataset.addSeries(tempSeries);
        rendererTemp = new XYSeriesRenderer();
        rendererTemp.setPointStyle(PointStyle.CIRCLE);
        rendererTemp.setLineWidth(2);
        rendererTemp.setColor(Color.RED);
    }
    if (f.getClass().equals(HumFragment.class)
            || f.getClass().equals(MixFragment.class)) {
        humSeries = new TimeSeries("");
        dataset.addSeries(humSeries);
        rendererHum = new XYSeriesRenderer();
        rendererHum.setPointStyle(PointStyle.CIRCLE);
        rendererHum.setLineWidth(2);
        rendererHum.setColor(Color.BLUE);
    }

    XYMultipleSeriesRenderer multipleRenderer = new XYMultipleSeriesRenderer();
    multipleRenderer.setYAxisMin(15);
    multipleRenderer.setYAxisMax(80);
    if (tempSeries != null) {
        multipleRenderer.addSeriesRenderer(rendererTemp);
    }
    if (humSeries != null) {
        multipleRenderer.addSeriesRenderer(rendererHum);
    }
    multipleRenderer.setShowGrid(true);
    multipleRenderer.setPointSize(8);
    multipleRenderer.setClickEnabled(true);
    multipleRenderer.setShowLegend(false);
    multipleRenderer.setShowLabels(true);
    multipleRenderer.setLabelsTextSize(20);
    multipleRenderer.setXRoundedLabels(false);
    // create view
    if (graphicalView == null) {
        graphicalView = ChartFactory.getTimeChartView(context, dataset,
                multipleRenderer, "");
        graphicalView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                SeriesSelection seriesSelection = graphicalView
                        .getCurrentSeriesAndPoint();
                if (seriesSelection != null) {
                    Toast.makeText(
                            context,
                            "Data "
                                    + dateFormat.format(seriesSelection
                                            .getXValue())
                                    + " Valore rilevato: "
                                    + seriesSelection.getValue(),
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    return graphicalView;
}

/*
 * refresh graph with new data
 */
@SuppressLint("SimpleDateFormat")
public void refresh(ArrayList<String[]> list, int to_remove) {
    try {
        if (tempSeries != null) {
            for (int i = 0; i < list.size(); i++) {
                tempSeries.add(dateFormat.parse(list.get(i)[2]),
                        Double.valueOf(list.get(i)[0]));
            }
            if (to_remove > 0) {
                for (int i = 0; i < to_remove; i++) {
                    tempSeries.remove(i);
                }
            }
        }
        if (humSeries != null) {
            for (int i = 0; i < list.size(); i++) {
                double to_add = Double.valueOf(list.get(i)[1]);

                humSeries.add(dateFormat.parse(list.get(i)[2]), to_add);
            }
            if(to_remove>0) {
                for (int i = 0; i < to_remove; i++) {
                    humSeries.remove(i);
                }
            }
        }
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    graphicalView.repaint();
}
}

出了什么问题?

1 个答案:

答案 0 :(得分:0)

您正在以错误的方式删除值。让我们考虑一个例子:

int[] values = new int[] {1, 2, 3, 4};

让我们根据您的算法删除2个值:

for (int i = 0; i < 2; i++) {
  remove(i);
}

输入:1,2,3,4 删除(0)结果2,3,4 删除(1)结果2,4,这不是你想要的

改为remove(0)