Pie Rendering issues

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
chunkyks
Posts: 8
Joined: Sat Sep 24, 2011 2:56 am
antibot: No, of course not.

Pie Rendering issues

Post by chunkyks » Mon Nov 28, 2011 12:35 am

Afternoon,

I'm having a weird problem with pie charts, I'm wondering if anyone can point me in the right direction.
Here's the problem: The colors aren't being rendered correctly on the chart, but the legend has it fine:
Image
After another trip through the event queue where the whole dataset gets rebuilt again, it works fine.

Here's the order I do things in:
1) Create a DefaultCategoryDataset
2) Create a pie chart with that dataset. Instantiate a ChartPanel with it. Return chartPanel
3) Populate the data in the dataset
4) Once the dataset is populated, I go through the whole dataset, and call setSectionPaint(). The code looks something like this:

Code: Select all

// Preconditions: mainPieChart is a jFreeChart
//          mainChartDataset is a DefaultCategoryDataset

MultiplePiePlot piePlot = (MultiplePiePlot) mainPieChart.getPlot();
JFreeChart chart = piePlot.getPieChart();
PiePlot mainPiePlot = (PiePlot) chart.getPlot();

for(String key : (List<String>)mainChartDataset.getRowKeys()) {
        int rowIndex = mainChartDataset.getRowIndex(key);
        if(rowIndex >= 0) {
            final Color col = # Some kind of thing that picks a color. I'm pulling it from a hashmap;
            mainPiePlot.setSectionPaint(rowIndex, col);
        }
}
This is only happening during application startup; after application startup, the callback begins at step 3 above, not step 1. Of possible note is that the panel is actually hidden right on startup, and only becomes visible after the "Pie" option is chosen from a dropdown.

I've tried triggering repaint on a whole bunch of different things near the end of the function, but nothing seems to change what you see here. For example, mainPieChart.repaint() seems the obvious candidate, but no effect.

This manifests with both jfreechart-1.0.13 and 1.0.14, I haven't tried earlier versions.

Any suggestions would be greatly appreciated,
Gary (-;

PS I bought the developer guide a while ago, but it doesn't seem to have an updated version for 1.0.14 available yet; is that in the pipeline?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Pie Rendering issues

Post by paradoxoff » Mon Nov 28, 2011 8:42 pm

Hi Gary,
all you need to do is to trigger a re-rendering of the JFreeChart after your loop in step 4 is complete.
A repaint of the ChartPanel won´t do it, as this will only lead to a repaint of the buffer image of the ChartPanel.
And a re-rendering of the JFreeChart is not automaticall triggered for the following reason:
you are modifying the section paints of the "inner PiePlot". The inner PiePlot fires a PlotChangeEvent that is captured by its parent JFreeChart which is the "inner JFreeChart" or "pieChart". The inner JFreeChart will then fire a ChartChangeEvent. However, the latter is not captured by the MultiplePiePlot as the MultiplePiePlot doesn´t register itself as a ChartChangeListener on the inner JFreeChart (this latter fact could easily be regarded as a bug).

To see the changes to the section paints, you have to force a re-rendering of the chart by an action such as:
- change the size of the ChartPanel
- call setNotify(true) on the MultiplePiePlot.

chunkyks
Posts: 8
Joined: Sat Sep 24, 2011 2:56 am
antibot: No, of course not.

Re: Pie Rendering issues

Post by chunkyks » Mon Nov 28, 2011 11:25 pm

Thanks for the quick response! Unfortunately...
I tried calling setNotify(true) on the MultiplePiePlot shortly after instantiating it, no dice. Resizing the window also has no effect at all.

After some messing around, I noticed that the version of setSectionPaint I was using is deprecated; I was using the version that took an index and a Paint. Now I'm using the version that takes a Comparable and a Paint, and it's working perfectly. ie, instead of:
mainPaiePlot.setSectionPaint(rowIndex,col)
I use
mainPaiePlot.setSectionPaint(key,col)

And now it's working fine

Thanks,
Gary

Locked