【问题标题】:JFreeChart BarChart -> NO gradientJFreeChart BarChart -> 无渐变
【发布时间】:2011-10-27 22:12:43
【问题描述】:

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

谁能帮忙?

代码:

   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

谢谢

【问题讨论】:

  • 粘贴您的代码。在大多数情况下,您应该添加一些代码以使其渐变,例如:GradientPaint
  • +1 以获得最真实的标题。 Web 2.0 再见!

标签: java colors jfreechart bar-chart


【解决方案1】:

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

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

应该可以的。

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

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

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

【讨论】:

  • 太棒了!找了很久的解决办法!
  • 对解决方案表示敬意。 “默认”(我认为)条形图具有这种非常廉价的外观。
【解决方案2】:

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

ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());

默认是添加渐变的 JFreeTheme。可以使用以下主题:

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

【讨论】:

  • 这个解决方案在使用 StackedBarChart 的情况下帮助了我,它不受在渲染器中更改 barPainter 的影响。也许我访问了错误的渲染器。尽管如此,这还是有帮助的。
【解决方案3】:

旧版org.jfree.chart.demo.BarChartDemo1source code 显示了如何设置系列颜色。只需指定纯色而不是渐变。

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

更正:@Jes 有用的answer 的关键可能在BarRenderer 中的defaultBarPainter 的初始化中找到。

【讨论】:

  • 也不行,一切都是用渐变绘制的。不幸的是我还不能上传图片:(此外,如果有超过三列,上面的解决方案会发生什么?
  • 图像会很好,但代码会更好。如果你什么都不做,你应该得到DefaultDrawingSupplier规定的颜色。
猜你喜欢
  • 2012-07-06
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多