我想在每次选择m适配器之一时重新绘制我的图表。第一次是绘制的图形看起来很完美,但是当我选择一个并且返回时看起来它会绘制两次相同的图形。< / p>
这是我的源代码:
public class MainActivity extends Activity {
private Button btnConnect;
private Spinner spnTime;
private String[] durationData = new String[]{
"Pilih Durasi",
"1 Minggu",
"1 Bulan",
"3 Bulan",
"9 Bulan",
"12 Bulan"
};
int period = 0;
private LinearLayout lnChart;
private ProgressDialog dialog;
public String baseURL = "http://www.abcj.com/i=";
private ArrayList<DataItem> listData;
//CHART VARIABLES
public static final String TYPE = "type";
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
private XYSeries mCurrentSeries;
private XYSeriesRenderer mCurrentRenderer;
private String mDateFormat;
private GraphicalView mChartView;
private int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConnect = (Button)findViewById(R.id.btnDownloadChartData);
spnTime = (Spinner)findViewById(R.id.spOptDuration);
listData = new ArrayList<DataItem>();
//CHART INITIATION
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPointSize(10);
btnConnect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line, durationData);
spnTime.setAdapter(adapter);
spnTime.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
switch (arg2+1) {
case 0:
break;
case 1:
period = 7;
break;
case 2:
period = 30;
break;
case 3:
period = 90;
break;
case 4:
period = 180;
break;
case 5:
period = 360;
break;
default:
break;
}
new LoadData(baseURL+period).execute();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
});
}
@SuppressLint("ShowToast")
@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);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(100);
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(MainActivity.this, "No chart element was clicked", Toast.LENGTH_SHORT)
.show();
}
}
});
mChartView.setOnLongClickListener(new View.OnLongClickListener() {
@SuppressLint("ShowToast")
@Override
public boolean onLongClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(MainActivity.this, "No chart element was long pressed",
Toast.LENGTH_SHORT);
return false; // no chart element was long pressed, so let something
// else handle the event
} else {
Toast.makeText(MainActivity.this, "Chart element in series index "
+ seriesSelection.getSeriesIndex() + " data point index "
+ seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
return true; // the element was long pressed - the event has been
// handled
}
}
});
mChartView.addZoomListener(new ZoomListener() {
public void zoomApplied(ZoomEvent e) {
String type = "out";
if (e.isZoomIn()) {
type = "in";
}
System.out.println("Zoom " + type + " rate " + e.getZoomRate());
}
public void zoomReset() {
System.out.println("Reset");
}
}, true, true);
mChartView.addPanListener(new PanListener() {
public void panApplied() {
System.out.println("New X range=[" + mRenderer.getXAxisMin() + ", " + mRenderer.getXAxisMax()
+ "], Y range=[" + mRenderer.getYAxisMax() + ", " + mRenderer.getYAxisMax() + "]");
}
});
lnChart.removeAllViews();
lnChart.addView(mChartView, 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(MainActivity.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);
DataItem item = null;
for (int i = 1; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
item = new DataItem(jsonObject.optString("lji_invest"),
jsonObject.optDouble("persen_hmin1"),
jsonObject.optDouble("lnu_nilai"),
jsonObject.optDouble("selisih"),
jsonObject.optString("lji_id"),
jsonObject.optString("tanggal"));
listData.add(item);
}
renderChart(listData);
} catch (Exception e) {
Log.d("TEST CHART", e.getMessage());
}
}
}
private void renderChart(ArrayList<DataItem> listData) {
// TODO Auto-generated method stub
String seriesTitle = "Series Data";
XYSeries series = new XYSeries(seriesTitle);
mDataset.addSeries(series);
mCurrentSeries = series;
XYSeriesRenderer renderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
renderer.setFillPoints(true);
mCurrentRenderer = renderer;
for (int i = 0; i < listData.size(); i++) {
mCurrentSeries.add(listData.get(i).getInuNilai(),
listData.get(i).getPersenHmint1());
}
if (mChartView != null) {
mChartView.repaint();
}
}
@Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
mDataset = (XYMultipleSeriesDataset) savedState.getSerializable("dataset");
mRenderer = (XYMultipleSeriesRenderer) savedState.getSerializable("renderer");
mCurrentSeries = (XYSeries) savedState.getSerializable("current_series");
mCurrentRenderer = (XYSeriesRenderer) savedState.getSerializable("current_renderer");
mDateFormat = savedState.getString("date_format");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("dataset", mDataset);
outState.putSerializable("renderer", mRenderer);
outState.putSerializable("current_series", mCurrentSeries);
outState.putSerializable("current_renderer", mCurrentRenderer);
outState.putString("date_format", mDateFormat);
}
那么如何重新绘制我的图表?我希望有人能告诉我我的错在哪里,告诉我解决问题的方法
答案 0 :(得分:1)
使用chartView.removeView(Graphical_View)
在使用新值设置图表视图之前,然后使用'Graphical_View.repaint()'以便为布局设置新值
但是请使用multirenderer.setInScroll(true);
答案 1 :(得分:0)
很难理解这个问题,但我会尝试猜测。
要重新绘制图表,您需要致电chartView.repaint();
但是,您可以通过拨打mRenderer.setInScroll(true);
来解决问题。
答案 2 :(得分:0)
我使用此代码解决了这个问题
if(layout != null)
layout.removeAllViews();
layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mChartView = null;