java 更改 JFreeChart 条形图中的条形颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12951505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:56:00  来源:igfitidea点击:

Change bar color in JFreeChart Bar Chart

javajfreechart

提问by Drag0

I'm making an application that takes some data and it needs to make a bar chart, but the color of a bar MUST be related to data that it is representing.

我正在制作一个需要一些数据的应用程序,它需要制作一个条形图,但条形图的颜色必须与它所代表的数据相关。

Imagine that I have this kind of data: BANANA 430 WATER MELLON 300

想象一下我有这样的数据:BANANA 430 WATER MELLON 300

Now I should make a bar chart, and I would like to paint the BANANA bar with yellow paint, and WATER MELLON bar with green paint. I'm using JFreeChart library in java. My research led me to making my custom renderer, but then if I make custom renderer the colors will appear randomly on bars. Any solution for this?

现在我应该制作一个条形图,我想用黄色颜料涂 BANANA 条,用绿色颜料涂 WATER MELLON 条。我在 Java 中使用 JFreeChart 库。我的研究使我制作了自定义渲染器,但是如果我制作自定义渲染器,颜色将随机出现在条形图上。有什么解决办法吗?

回答by MaVRoSCy

Maybe this http://www.java2s.com/Code/Java/Chart/JFreeChartBarChartDemo3differentcolorswithinaseries.htmcan be of help.

也许这个http://www.java2s.com/Code/Java/Chart/JFreeChartBarChartDemo3differentcolorswithinaseries.htm会有所帮助。

See how the code below is used:

看看下面的代码是如何使用的:

    final CategoryItemRenderer renderer = new CustomRenderer(
        new Paint[] {Color.red, Color.blue, Color.green,
            Color.yellow, Color.orange, Color.cyan,
            Color.magenta, Color.blue}
    );

回答by peshkira

Hereis a demo, where you can see how to do this. As in the comment above, you have to add the paints to the series as you introduced them (0 is the first, 1 is the second and so on).

是一个演示,您可以在其中了解如何执行此操作。正如上面的评论一样,您必须在介绍它们时将它们添加到系列中(0 是第一个,1 是第二个,依此类推)。