我正在为我的图表使用 AchartEngine 库。我有一个搜索栏,用于更改在搜索栏中选择的值的图表。每当我选择一个搜索栏时,我的图表就不会重新打印,即使我已经打电话mChartView.repaint();
而我也无法删除图表,即使我打电话给removeAllViews();
我的图表只是做了一个新的顶部
这是我的代码:
SeekTime.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (SeekTime.getProgress() <= 3){
SeekTime.setProgress(0);
period=7;}
if (SeekTime.getProgress() > 3 && SeekTime.getProgress() <= 8){
SeekTime.setProgress(5);
period =30;
}
if (SeekTime.getProgress() >8 && SeekTime.getProgress()<=13){
SeekTime.setProgress(10);
period = 90;
}
if (SeekTime.getProgress() >13 && SeekTime.getProgress()<=18){
SeekTime.setProgress(15);
period = 180;
}
if (SeekTime.getProgress() >18 && SeekTime.getProgress()<=23){
SeekTime.setProgress(20);
period = 270;
}
if (SeekTime.getProgress() >23){
SeekTime.setProgress(25);
period = 360;
}
String url = baseURL+"id="+strLjiId+"&nilai="+period;
new LoadData(url).execute();
}
});
}
@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);
lnChart.removeAllViews();
lnChart.addView(mChartView, 0, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
else {
mChartView.repaint();
}
}
private class LoadData extends AsyncTask<Void, Void, String>{
String url = "";
public LoadData(String url) {
// TODO Auto-generated constructor stub
this.url = url;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(NABActivityDetail.this, "", "Please wait");
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String result = "";
try {
result = Connection.get(url);
} catch (Exception e) {
// TODO: handle exception
result = "";
Log.d("TEST CHART", e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
fetchResponse(result.replace("\n", "").trim());
dialog.dismiss();
}
}
private void fetchResponse(String result) {
// TODO Auto-generated method stub
if (!result.equals("")) {
try {
JSONArray jsonArray = new JSONArray(result);
NabDetail item = null;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
item = new NabDetail(jsonObject.optString("lji_invest"),
jsonObject.optDouble("persen_hmin1"),
jsonObject.optDouble("lnu_nilai"),
jsonObject.optDouble("selisih"),
jsonObject.optDouble("persen_hke1"),
jsonObject.optString("tanggal"),
jsonObject.optString("lji_id")
);
listData.add(item);
}
renderChart(listData);
} catch (Exception e) {
}
}
}
private 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);
renderer.setFillPoints(true);
mCurrentRenderer = renderer;
for (int i = 0; i < listData.size(); i++) {
mCurrentSeries.add(i,listData.get(i).getInuNilai()
);
}
if (mChartView != null) {
mChartView.repaint();
}
我的代码有问题吗?或者有没有办法在之前删除图表并重新绘制或刷新新图表?
这是我的图表:
答案 0 :(得分:0)
mChartView.repaint()
是重绘图表的方法。但是,为了避免您遇到的问题,您还需要在设置图表时执行renderer.setInScroll(true)
。