gpt4 book ai didi

java - achartengine : how to repaint/redraw chart

转载 作者:行者123 更新时间:2023-12-01 14:33:42 26 4
gpt4 key购买 nike

每次我选择一个 m 适配器时,我都想重新绘制我的图表,第一次绘制的图表看起来很完美,但是当我选择一个并返回时,看起来它绘制了两次相同的图表。

这是我的源代码:

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

那么如何重新绘制我的图表呢?我希望有人能告诉我我的错在哪里,并告诉我解决方法

最佳答案

使用chartView.removeView(Graphical_View)

在使用新值设置图 TableView 之前,然后使用“Graphical_View.repaint()”以便为布局设置新值

但是使用 multirenderer.setInScroll(true);

关于java - achartengine : how to repaint/redraw chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16665210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com