views:

675

answers:

2

I want to generate random colours which can be attractive in pie charts.I have a very poor sense when it comes to GUI.Can anyone help in writing a piece of function which generates 6 good colors that may look good in a pie chart in a random order.Right now I have hardcoded.But I don't like those colors.Please help me.

plot.setSectionPaint("iPhone 2G", new Color(200, 255, 255));
plot.setSectionPaint("iPhone 3G", new Color(200, 200, 255));
+2  A: 
VonC
+1  A: 

My advice is to not generate random colours for two reasons. Firstly you'll have to take additional steps to ensure that two or more of the colours don't too closely resemble each other. Secondly, colour is emotive. You don't want your "this chart tells me everything is fine" colour to be bright red. Red is an alert colour; so are orange and yellow.

The general advice with colour in charts is to use unsaturated colours for general data, and brighter colours for data you want to draw attention to. I'm not sure of your exact use case, but if you wanted to draw attention to exceptionally high iPhone 3GS sales you might want to use a brighter colour if it goes over a certain threshold.

As a means of getting started I would use the colour charts in VonC's answer to hand-pick 5 or so colours. You shouldn't display too many different colours on a single chart as it will be too much data for viewers to effectively take in. If you have more than 7 or so datasets on a chart, then there's a good chance you're not displaying the right chart! (but that's another story...)

Ensure there is no clash amongst the colours you have chosen, and sequence them in an array. Now you can use a simple List each time your colouring a chart.

public class ChartColorSource {
    public static final Color[] COLORS;
    static {
        COLORS = new Color[ 6 ];
        COLORS[0] = new Color( ... );
        COLORS[1] = new Color( ... );
        COLORS[2] = new Color( ... );
        COLORS[3] = new Color( ... );
        COLORS[4] = new Color( ... );
        COLORS[5] = new Color( ... );
    }

    /**
     * Assign a color from the standard ones to each category
     */
    public static void colorChart( Plot plot, String[] categories ) {
        if ( categories.length > COLORS.length ) {
            // More categories than colors. Do something!
            return;
        }

        // Use the standard colors as a list so we can shuffle it and get 
        // a different order each time.
        List<Color> myColors = Arrays.asList( COLORS );
        Collections.shuffle( myColors );

        for ( int i = 0; i < categories.length; i++ ) {
            plot.setSectionPaint( categories[i], myColors.get( i ) );
        }
    }
}
banjollity