protected void updateChartProperties() {
chart.setTitle(tfTitle.getText());
PiePlot plot = (PiePlot)chart.getPlot();
SourcePieDataset dst = (SourcePieDataset)plot.getDataset();
int n = dst.getItemCount();
// If some curves were deleted, remove them
if (n != pcbxcurves.getItemCount()) {
boolean[] toKeep = new boolean[n]; // boolean are initialized to false by JVM
for (int i=0; i<pcbxcurves.getItemCount(); ++i)
toKeep[((CbxEntry)pcbxcurves.getItemAt(i)).datasetIndex] = true;
// Now we can remove the deleted curves, and update the existing ones
// Start from the end, to remove the entries and keep the index OK
for (int i=n-1; i>=0; i--) {
if (!toKeep[i]) {
GenericMapper mapper = (GenericMapper)mappers.remove(i);
if (mapper!=null) mapper.removeListener(PieChartShape.this);
DataSource source = (DataSource)mapperSources.remove(i);
if (source!=null) source.removeListener(PieChartShape.this);
dst.removeSource(i);
}
}
}
// Now we match the data set and the list
int len = pcbxcurves.getItemCount();
// first compute max ratio => define JFreeChart 100% and compute 1.0 equivalent
double maxratio = Double.NEGATIVE_INFINITY;
for (int i=0; i<len; ++i) {
CbxEntry e = (CbxEntry)pcbxcurves.getItemAt(i);
maxratio = Math.max(e.ratio, maxratio);
}
double delta = maxratio - 1.0;
// now apply changes
for (int i=0; i<len; ++i) {
CbxEntry e = (CbxEntry)pcbxcurves.getItemAt(i);
dst.setName(i,e.name);
DataSource ds = (DataSource)mapperSources.get(i);
if ((e.source==null) && (ds!=null)) {
ds.removeListener(PieChartShape.this);
ds.removeEndNotificationListener(PieChartShape.this);
}
mapperSources.set(i,e.source);
if (e.source!=null) {
e.source.addListener(PieChartShape.this);
e.source.addEndNotificationListener(PieChartShape.this);
}
ColorMapper cm = (ColorMapper)mappers.get(i);
if ((e.mapper==null) && (cm!=null)) cm.removeListener(PieChartShape.this);
mappers.set(i,e.mapper);
if (e.mapper!=null) e.mapper.addListener(PieChartShape.this);
if ((e.mapper!=null) && (e.source!=null)) {
e.mapper.setDefaultPaint(e.color);
plot.setSectionPaint(i,e.mapper.getPaint(e.source));
}
else plot.setSectionPaint(i,e.color);
plot.setExplodePercent(i, (delta==0) ? 0.0 : (e.ratio-1.0) / delta );
}
plot.setRadius(1.0/maxratio);
}