JFreeChart中PieChart的颜色

时间:2009-07-06 17:57:39

标签: java colors jfreechart

我想生成随机颜色,这在饼图中很有吸引力。当涉及到GUI时,我的意识很差。任何人都可以帮助编写一个功能,产生6种颜色可能看起来很好的馅饼以随机顺序排列图表。现在我已经硬编码了。但我不喜欢那些颜色。请帮助我。

plot.setSectionPaint("iPhone 2G", new Color(200, 255, 255));
plot.setSectionPaint("iPhone 3G", new Color(200, 200, 255));

2 个答案:

答案 0 :(得分:7)

我建议您使用 color charts 中的一组颜色,而不是尝试生成随机颜色。

您为graphs and charts制作了一些表格颜色。

alt text http://www.sapdesignguild.org/goodies/diagram_guidelines/PALETTES/PALETTE1.GIF

另见 Recommendations for Charts and Graphics

alt text
(来源:sapdesignguild.org

答案 1 :(得分:6)

我的建议是不要生成随机颜色有两个原因。首先,你必须采取额外的步骤,以确保两种或多种颜色不太相似。其次,颜色是情绪化的。你不希望你的“这个图表告诉我一切都很好”颜色要鲜红。红色是警示色;橙色和黄色也是如此。

图表中带颜色的一般建议是对一般数据使用不饱和颜色,为要引起注意的数据使用更亮的颜色。我不确定你的确切用例,但如果你想引起人们对特别高的iPhone 3GS销售的关注,你可能想要使用更亮的颜色,如果它超过一定的阈值。

作为入门手段,我会使用VonC's answer中的颜色图表来手工挑选5种颜色。你不应该在一张图表上显示太多不同的颜色,因为观众有效地收集的数据太多。如果你在图表上有超过7个左右的数据集,那么很有可能你没有显示正确的图表! (但那是另一个故事......)

确保您选择的颜色之间没有冲突,并将它们排列在一个数组中。现在,每次着色图表时都可以使用简单的列表。

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 ) );
        }
    }
}
相关问题