Fix Bar Chart Width and spacing between bars in JFreeChart

I have a summary histogram in which the number of columns is dynamic, can vary from 1 to n columns. I want the distance between the graphs and the bandwidth to be consistent. How to fix it. Please suggest solutions / ideas.

+3
source share
2 answers

StackedBarRenderermakes certain efforts to ensure that "the distance between the [strips] and the bandwidth is consistent." It is not clear what you want it to do differently as the number of columns varies. Corresponding geometry is determined by the parent BarRendererin ways such as calculateBarWidth()that can be redefined as desired. Also, make sure that each series has a value for each category.

+2
source

In a bar graph, you can change the spacing between lines using

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

Code 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;
}
+4
source

Source: https://habr.com/ru/post/1744879/


All Articles