java JFreeChart BarChart -> 无渐变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7076305/
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-30 18:33:00  来源:igfitidea点击:

JFreeChart BarChart -> NO gradient

javacolorsjfreechartbar-chart

提问by shorty

my bar chart is always drawn with a gradient color by default. I just want a simple color without any styled effects.

默认情况下,我的条形图始终使用渐变色绘制。我只想要一个没有任何样式效果的简单颜色。

Can anyone help ?

任何人都可以帮忙吗?

Code:

代码:

   final JFreeChart chart = ChartFactory.createBarChart(
        "",         // chart title
        xLabel,               // domain axis label
        yLabel,                  // range axis label
        dataset,                  // data
        PlotOrientation.VERTICAL, // orientation
        true,                     // include legend
        false,                     // tooltips?
        false                     // URLs?
    );

  final CategoryPlot plot = chart.getCategoryPlot();
  // SOMETHING HAS TO BE DONE HERE

  showChart(chart); // Simply shows the chart in a new window

Thanks

谢谢

回答by Jes

The problem lies in the BarPainteryou are using. The JFreeChart version 1.0.13 default is to use GradientBarPainterwhich adds a metallic-ish look to the bar. If you want the "old" look the solution is to use the StandardBarPainter.

问题在于BarPainter您正在使用的。JFreeChart 版本 1.0.13 默认使用GradientBarPainter它为条形添加金属外观。如果您想要“旧”外观,解决方案是使用StandardBarPainter.

final CategoryPlot plot = chart.getCategoryPlot();
((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());

That should do it.

那应该这样做。

Alternatively, if you want use JFreeChart's BarRenderer, you could force it to use the StandardBarPainterby calling the static method setDefaultBarPainter()before initializing your renderer.

或者,如果您想使用 JFreeChart 的BarRenderer,您可以StandardBarPainter通过setDefaultBarPainter()在初始化渲染器之前调用静态方法来强制它使用。

final CategoryPlot plot = chart.getCategoryPlot();
BarRenderer.setDefaultBarPainter(new StandardBarPainter());
((BarRenderer) plot.getRenderer()).setBarPainter(new BarPainter());

If you want more control of the chart you can always build it from the ground up instead of using ChartFactory, but that does require a lot extra code.

如果您想要更多地控制图表,您可以随时从头开始构建它,而不是使用ChartFactory,但这确实需要很多额外的代码。

回答by Kazi Islam

Before you create the chart from ChartFactory you can set the chart theme:

在从 ChartFactory 创建图表之前,您可以设置图表主题:

ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());

The default is the JFreeTheme which adds the gradient. The following themes are available:

默认是 JFreeTheme,它添加了渐变。以下主题可用:

ChartFactory.setChartTheme(StandardChartTheme.createJFreeTheme());
ChartFactory.setChartTheme(StandardChartTheme.createDarknessTheme());

回答by trashgod

The source codefor an older version of org.jfree.chart.demo.BarChartDemo1shows how to set the series colors. Just specify plain colors instead of gradients.

旧版本的源代码org.jfree.chart.demo.BarChartDemo1显示了如何设置系列颜色。只需指定纯色而不是渐变。

renderer.setSeriesPaint(0, Color.red);
renderer.setSeriesPaint(1, Color.green);
renderer.setSeriesPaint(2, Color.blue);

Correction: The key to @Jes's helpful answermay be found in the initialization of defaultBarPainterin BarRenderer.

更正:关键@赫苏斯乐于助人的答案可以在初始化中找到defaultBarPainterBarRenderer