The Plot
of the chart for pie charts can be cast to a PiePlot
which lets you set the paint color, outline color, shadow color ect.. of each piece of the pie. Heres a little sample of setting the colors for each piece of the pie. If the number of pieces is greater than the amount of colors defined in our array then it will reuse the colors.
The setSectionPaint
method takes any type of java.awt.Paint
so you can also pass in gradient paints among other things.
PieDataset dataset = ...
JFreeChart chart = ChartFactory.createPieChart("My Chart", dataset, false, true, false);
// all possible colors for each piece of the pie
Color[] colors = new Color[] { new Color(232, 124, 35),
new Color(51, 109, 178), new Color(182, 52, 49),
new Color(103, 131, 45), new Color(108, 77, 146),
new Color(46, 154, 183), new Color(151, 64, 64) };
PiePlot plot = (PiePlot) chart.getPlot();
// set each sections inside paint
int i = 0;
for (Object key : dataset.getKeys()) {
plot.setSectionPaint((Comparable) key, colors[i % colors.length]);
i++;
}