Creating Pie Charts using JFreeChart with custom colors

I have attached a sample code snippet for creating a Pie Chart using JFreeChart with custom colors. There is a PieReneder (a static inner class) which can be used to achieve the same. The PieRenderer takes an array of java.awt.Color in its constructor.

package subin.rnd.chart;
import java.awt.Color; 
import java.util.List;
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.plot.PiePlot; 
public class PieChartWithCustomColours 
    public static void main(String[] args) 
        DefaultPieDataset dataset = new DefaultPieDataset(); 
        JFreeChart chart; 
        // Defining the dataset 
        dataset.setValue("Windows", 60); 
        dataset.setValue("Linux", 20); 
        dataset.setValue("Mac", 10); 
        dataset.setValue("Unix", 5); 
        dataset.setValue("DOS", 5); 
        chart = ChartFactory.createPieChart( 
                "OS Chart", dataset, false, false, false); 
        PiePlot plot = (PiePlot)chart.getPlot(); 
        // Specify the colors here 
        Color[] colors = {, Color.yellow,}; 
        PieRenderer renderer = new PieRenderer(colors); 
        renderer.setColor(plot, dataset); 
            // This will create a PNG image 
            ChartUtilities.saveChartAsPNG(new File("chart.png"), chart, 400, 
        catch (Exception e) 
            System.out.println("Exception while creating the chart"); 
     * A simple renderer for setting custom colors 
     * for a pie chart. 
    public static class PieRenderer 
        private Color[] color; 
        public PieRenderer(Color[] color) 
            this.color = color; 
        public void setColor(PiePlot plot, DefaultPieDataset dataset) 
            List <Comparable> keys = dataset.getKeys(); 
            int aInt; 
            for (int i = 0; i < keys.size(); i++) 
                aInt = i % this.color.length; 
                plot.setSectionPaint(keys.get(i), this.color[aInt]); 

Sample Output

Pie Chart With Custom Colors (JFreeChart)

Here the colors are repeating, right? Its b’ coz there is only three entries in the input color array to the PieRenderer.