0

I am implementing achartengine within a ViewPager. The view holding the chart has a spinner with 7 options where you can choose which data you want to view. 6 of the options return a PieChartView and the last one returns a TimeChartView.

The PieChartView charts update and redraw as expected, but when selecting the TimeChartView option, nothing happens. The view stays with the previously selected PieChartView and doesn't throw an error. I've implemented TimeChartView successfully elsewhere in the app and comparisons of the code and the data feeding the chart haven't thrown up any flags, so I can only assume that my method of switching graph types is incorrect.

The code extract below is the part which deals with switching the views; any advice would be appreciated.

try{
        //if mChartView is already created (has data)
        //we need to invalidate it because it will only accept
        //data less than or equal to the length of data it currently has
        if(mChartView != null){
            mChartView.invalidate();
        }
        if(graphType >= 6){
            mChartView = ChartFactory.getTimeChartView(ctx, mDataset, genericXYRenderer, "MM/dd");
            genericXYRenderer.setClickEnabled(false);
        }else{
            mChartView = ChartFactory.getPieChartView(ctx, genericSeries, genericRenderer);
      genericRenderer.setClickEnabled(false);
      genericRenderer.setSelectableBuffer(10);
        }


      layout.addView(mChartView, new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
     mChartView.repaint();
    } catch (Exception e) {
        Log.e("[appname]", "Error trying to set ChartView", e);
        m_Logger = new Logger();
    m_Logger.error(e);
}

Many Thanks, Dave

DaveSav
  • 1,364
  • 5
  • 21
  • 41

1 Answers1

0

Ok. So the PieCharts were updating because mChart view held a reference to PieChartView and the spinner options were providing it with array data in the same format (albeit different sizes).

Calling

mChartView.invalidate();

(I'm guessing) just cleared the data it held, but still kept a reference to a PieChartView. When the TimeChartView option was selected, the array data was in a different format which was incompatible with a PieChartView. Not sure why it didn't throw an error.

Anyway, the solution was to replace the .invalidate() call with:

layout.removeView(mChartView);
DaveSav
  • 1,364
  • 5
  • 21
  • 41