Achartengine:删除之前的图表并重新绘制新图表

时间:2013-07-22 04:42:11

标签: java android graph achartengine linechart

我真的很沮丧使用achartengine库重绘图表,我已删除视图,但我的图表只是在顶部图表上新建,我真的很困惑...我有搜索任何方法但我无法得到它..这是我之前的问题:achartengine : how to repaint / redraw chart

每次我滑动搜索栏时如何重绘我的图表?

这是我的源代码:

        @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        if (mChartView == null) {
   lnChart = (LinearLayout) findViewById(R.id.chart);
              mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
              mChartView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
                      double[] xy = mChartView.toRealPoint(0);
                      if (seriesSelection == null) {
                            Toast.makeText(getApplicationContext(), "tidak ada chart yang dipilih", Toast.LENGTH_SHORT)
                                .show();
                          } else {
                        Toast.makeText(
                                getApplicationContext(),
                                "Nilai " + listData.get(seriesSelection.getPointIndex()).getInuNilai()+
                                "  tanggal " + listData.get(seriesSelection.getPointIndex()).getTanggal(), 
                                Toast.LENGTH_SHORT).show();   
                          }
                    }
                  });
              lnChart.addView(mChartView, 0, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

            } 
        else {
mChartView.repaint();

            }
    }

    public void renderChart(ArrayList<NabDetail> listData) {
        // TODO Auto-generated method stub

        String seriesTitle = "";
        XYSeries series = new XYSeries(seriesTitle);
        mDataset.addSeries(series);
        mCurrentSeries = series;
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setFillBelowLine(true);
        renderer.setFillBelowLineColor(Color.TRANSPARENT);
        renderer.setColor(Color.YELLOW);
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.setInScroll(true);
        mRenderer.setShowLegend(false);
        //hide x label
        mRenderer.setXLabels(RESULT_OK);
        renderer.setFillPoints(true);
        mCurrentRenderer = renderer;


        for (int i = 0; i < listData.size(); i++) {

            mCurrentSeries.add(i,listData.get(i).getInuNilai() 
                    );

        }

    if (mChartView != null) {

        lnChart.removeViewAt(0);
            mChartView.repaint();
        }
    }
}

我使用了lnChart.removeViewAt(0); mChartView.repaint();,但我的图表仍然没有重绘...那么如何重绘和删除图表呢?我必须把正确的代码放在哪里?

请...我希望有人能帮我解决这个问题

1 个答案:

答案 0 :(得分:5)

这段代码应该适合你。

public void renderChart(ArrayList<NabDetail> listData) {
    // Clear your previous chart DataSet and Renderer.
    mDataSet.clear();
    mRenderer = new XYMultipleSeriesRenderer();

    String seriesTitle = "";
    XYSeries series = new XYSeries(seriesTitle);
    mDataset.addSeries(series);
    mCurrentSeries = series;
    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setFillBelowLine(true);
    renderer.setFillBelowLineColor(Color.TRANSPARENT);
    renderer.setColor(Color.YELLOW);
    mRenderer.addSeriesRenderer(renderer);
    mRenderer.setInScroll(true);
    mRenderer.setShowLegend(false);
    //hide x label
    mRenderer.setXLabels(RESULT_OK);
    renderer.setFillPoints(true);
    mCurrentRenderer = renderer;

    for (int i = 0; i < listData.size(); i++) {
        mCurrentSeries.add(i,listData.get(i).getInuNilai());
    }

    // repaint your chart with new mDataSet and mRenderer.
    repaint();
}

private void repaint() {
    lnChart = (LinearLayout) findViewById(R.id.chart);

    if (mChartView != null) {
        lnChart.removeView(mChartView);
    }

    mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);

    lnChart.addView(mChartView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}