修复 JFreeChart 中条形图的宽度和条形之间的间距

发布于 2024-09-01 13:06:40 字数 79 浏览 3 评论 0原文

我有堆积条形图,其中列数是动态的,可以从 1 列更改为 n 列。我希望图表之间的间距和条形的宽度保持一致。我该如何解决它。请提出解决方案/想法。

I have stacked bar chart in which the number of columns is dynamic, can change from 1 to n columns. I want the spacing between the charts and width of the bar to be consistent. How do I fix it. Please suggest solutions / ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˉ厌 2024-09-08 13:06:40

在堆叠条形图中,您可以使用

  • CategoryAxis.setLowerMargin
  • 更改条形之间的间距CategoryAxis.setMargin 和
  • CategoryAxis.setUpperMargin

代码如下

protected JFreeChart generateGraph() {

  CategoryAxis categoryAxis = new CategoryAxis("Categories");
  categoryAxis.setLowerMargin(.01);
  categoryAxis.setCategoryMargin(.01);
  categoryAxis.setUpperMargin(.01);      
  categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

  ValueAxis valueAxis = new NumberAxis("Values");

  StackedBarRenderer renderer = new StackedBarRenderer();
  renderer.setBarPainter(new StandardBarPainter());
  renderer.setDrawBarOutline(false);
  renderer.setShadowVisible(false);
  renderer.setBaseItemLabelsVisible(true);
  renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

  CategoryPlot plot = new CategoryPlot( _dataset,
                                        categoryAxis,
                                        valueAxis,
                                        renderer);

  plot.setOrientation(PlotOrientation.VERTICAL);

  JFreeChart chart = new JFreeChart( "Title",
                          JFreeChart.DEFAULT_TITLE_FONT,
                          plot,
                          true);
  //ChartFactory.getChartTheme().apply(_chart);
  return chart;
}

In a Stacked Bar chart, you can change the spacing between bars using

  • CategoryAxis.setLowerMargin
  • CategoryAxis.setMargin and
  • CategoryAxis.setUpperMargin

Code is below

protected JFreeChart generateGraph() {

  CategoryAxis categoryAxis = new CategoryAxis("Categories");
  categoryAxis.setLowerMargin(.01);
  categoryAxis.setCategoryMargin(.01);
  categoryAxis.setUpperMargin(.01);      
  categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

  ValueAxis valueAxis = new NumberAxis("Values");

  StackedBarRenderer renderer = new StackedBarRenderer();
  renderer.setBarPainter(new StandardBarPainter());
  renderer.setDrawBarOutline(false);
  renderer.setShadowVisible(false);
  renderer.setBaseItemLabelsVisible(true);
  renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

  CategoryPlot plot = new CategoryPlot( _dataset,
                                        categoryAxis,
                                        valueAxis,
                                        renderer);

  plot.setOrientation(PlotOrientation.VERTICAL);

  JFreeChart chart = new JFreeChart( "Title",
                          JFreeChart.DEFAULT_TITLE_FONT,
                          plot,
                          true);
  //ChartFactory.getChartTheme().apply(_chart);
  return chart;
}
萌能量女王 2024-09-08 13:06:40

StackedBarRenderer 致力于使“[条形]之间的间距和条形宽度保持一致”。当列数发生变化时,尚不清楚您希望它做什么不同的事情。相关几何图形由父级 确定CalculateBarWidth() 等方法中的 BarRenderer ,可以根据需要重写。另外,验证每个系列中的每个类别都有一个值。

StackedBarRenderer devotes some effort to making the "spacing between the [bars] and width of the bar to be consistent." It's not clear what you want it to do differently as the number of columns changes. The relevant geometry is determined by the parent BarRenderer in such methods as calculateBarWidth(), which can be overridden as desired. Also, verify that there is a value for each category in each series.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文