0

我有一个应用程序包含一些使用 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();
}
}

怎么了?

4

1 回答 1

0

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

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

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

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

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

remove(0)改为这样做。

于 2013-10-23T13:31:33.333 回答