Lutske
Lutske

Reputation: 117

Android update bar chart

I did write some code to update the data of a bar chart in AChartEngine. When I run the code in debug mode the new points are added to the array. But the new bars aren't showing on the screen. I use AsyncTask to update.

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 because it's an activity

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

Set all the data:

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

How I draw the data:

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

The strange thing is, the same construction works fine by a line chart. But by this bar chart it doesn't repaint good.

Thanks for the help :)

Upvotes: 1

Views: 2124

Answers (2)

Lutske
Lutske

Reputation: 117

The code works now, this is how I changed it:

Added this to the variables on the top:

    XYSeries _xySeries;

Changed the OnprogressUpdate:

  @Override
  protected void onProgressUpdate(String... values) {
                    _xySeries.add(Double.parseDouble(values[0]),Double.parseDouble(values[1]));
                    _mChartView.repaint();
   }

And the 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

So I made a new series of the xyserie type. Add the data to the old series and then convert it to the xyseries. So you can update it everywhere and add it to the dataset.

Upvotes: 1

dmon
dmon

Reputation: 30168

Seems to me like you're not updating dataset after you add values to the _series variable in your onProgressUpdate. You're only updating/creating it in setData(), which you don't call again from your AsyncTask. getBarChartView reads the data from dataset, but dataset is now stale.

Upvotes: 3

Related Questions