我写了一些代码来更新AChartEngine中条形图的数据。当我在调试模式下运行代码时,新点将添加到数组中。但新的酒吧没有显示在屏幕上。我使用AsyncTask进行更新。
的AsyncTask:
private class ChartTask2 extends AsyncTask<Void, String, Void>{
// Generates dummy data in a non-ui thread
@Override
protected Void doInBackground(Void... params) {
try{
for(int i = 0; i < 5; i++) {
Log.v("Asyntask", "doInBackground");
String [] values = new String[2];
values[0] = Double.toString(6+ i);
values[1] = Double.toString(6 +i);
publishProgress(values);
Thread.sleep(1000);
Log.v("i", Integer.toString(6+i));
}
}catch(Exception e){ }
return null;
}
// Plotting generated data in the graph
@Override
protected void onProgressUpdate(String... values) {
_series.add("Bar" + Double.parseDouble(values[0]),Double.parseDouble(values[1]));
_mChartView.repaint();
Log.v("Asyntask", "repaint onProgressUpdate");
}
}
OneCreate,因为它是一项活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("inCreate","inCreate");
// Get ALL the data!!!
Bundle extras = getIntent().getExtras();
if (extras != null) {
this._chartTitle = extras.getString("chartTitle");
this._xTitle = extras.getString("xTitle");
// and other variables are filled here
}
setData();
Log.v("method","call Asynctask");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
new ChartTask2().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
else
new ChartTask2().execute();
}
设置所有数据:
public void setData() {
Log.v("method","setData");
// Add the bar data
dataset = new XYMultipleSeriesDataset();
for( int bars = 0; bars < (_yCoords.length); bars++){
_series = new CategorySeries(_barNames[bars]);
for (int point = 0; point < (_yCoords[bars].length); point++){
_series.add("Bar " + _yCoords[bars][point], _yCoords[bars][point]);
}
dataset.addSeries(_series.toXYSeries());
}
// Holds a collection of XYSeriesRenderer and customizes the graph
mRenderer = new XYMultipleSeriesRenderer();
// Customize Bars
for(int i = 0; i < _yCoords.length; i++){
_renderer = new XYSeriesRenderer();
_renderer.setColor(_colors[i]);
_renderer.setDisplayChartValues(_displayValues[i]);
_renderer.setChartValuesSpacing(_valuesSpacing[i]);
mRenderer.addSeriesRenderer(_renderer);
}
// Set optional settings
mRenderer.setChartTitle(_chartTitle);
//and more data is set here
setContentView(drawData());
}
我如何绘制数据:
public View drawData() {
Log.v("method","tekenData");
// Set background
View view = View.inflate(this, _layout, null);
// Getting a reference to view group linear layout chart_container
LinearLayout chartContainer = (LinearLayout) view.findViewById(_wrapper);
// Getting LineChartView to add to the custom layout
_mChartView = ChartFactory.getBarChartView(this, dataset, mRenderer, Type.DEFAULT);
// Adding the line chart to the custom layout
chartContainer.addView(_mChartView);
return view;
}
奇怪的是,同样的结构可以通过折线图正常工作。但是通过这个条形图它并没有重现良好。
感谢您的帮助:)
答案 0 :(得分:2)
在我向dataset
中的_series
变量添加值后,您似乎没有更新onProgressUpdate
。您只是在setData()
中更新/创建它,您不会再从AsyncTask中调用它。 getBarChartView
从数据集中读取数据,但数据集现在已过时。
答案 1 :(得分:0)
代码现在可以使用,这就是我改变它的方式:
将此添加到顶部的变量:
XYSeries _xySeries;
更改了OnprogressUpdate:
@Override
protected void onProgressUpdate(String... values) {
_xySeries.add(Double.parseDouble(values[0]),Double.parseDouble(values[1]));
_mChartView.repaint();
}
和setData:
public void setData() {
// Add the bar data
dataset = new XYMultipleSeriesDataset();
for( int bars = 0; bars < (_yCoords.length); bars++){
_series = new CategorySeries(_barNames[bars]);
for (int point = 0; point < (_yCoords[bars].length); point++){
_series.add("Bar " + _yCoords[bars][point], _yCoords[bars][point]);
}
_xySeries = _series.toXYSeries();
dataset.addSeries(_xySeries);
}
//.... and more of the old setData
所以我制作了一个新系列的xyserie类型。将数据添加到旧系列,然后将其转换为xyseries。因此,您可以随处更新它并将其添加到数据集中。