问题
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