JFreeChart BarChart – >没有渐变
默认情况下,我的条形图始终使用渐变颜色绘制。 我只想要一个没有任何风格效果的简单颜色。
有人可以帮忙吗?
码:
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
谢谢
问题在于您正在使用的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
,但这确实需要很多额外的代码。
在ChartFactory中创建图表之前,您可以设置图表主题:
ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
默认值是添加渐变的JFreeTheme。 可以使用以下主题:
ChartFactory.setChartTheme(StandardChartTheme.createJFreeTheme()); ChartFactory.setChartTheme(StandardChartTheme.createDarknessTheme());
org.jfree.chart.demo.BarChartDemo1
的源代码显示了如何设置系列颜色。 只需指定普通颜色而不是渐变颜色。
renderer.setSeriesPaint(0, Color.red); renderer.setSeriesPaint(1, Color.green); renderer.setSeriesPaint(2, Color.blue);
更正:@ Jes的有用答案的关键可以在BarRenderer
的defaultBarPainter
初始化中BarRenderer
。