Achartengine : remove chart before and redraw new chart

南笙酒味 提交于 2020-01-15 07:54:46

问题


i really frustased to redraw chart using achartengine library, i have remove view, but my chart just make new on top chart, i really confused... i have search any method but i can't get it.. this is my question before : achartengine : how to repaint / redraw chart

how to redraw my chart everytime i slide my seekbar?

this is my source code :

        @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();
        }
    }
}

i have used lnChart.removeViewAt(0); mChartView.repaint(); but my chart still not redraw... so how to redraw and remove chart before? where am i must put the right code?

please.. i hope someone can help me to solve this problem


回答1:


This piece of code should work for you.

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));
}


来源:https://stackoverflow.com/questions/17780085/achartengine-remove-chart-before-and-redraw-new-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!