1

I am using JFreeChart v1.5.0 and creating a waterfall chart with a sample dataset as shown in the code below. I was able to add space to the bars created using createBarChart() with the help of BarRenderer API, but when I created waterfall chart using createWaterFallChart() and tried adding space to the bars using the same BarRenderer API but I could not.

I used setItemMargin(double) API to achieve the output but nothing happened.

private CategoryDataset createDataset() {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    dataset.addValue(15.76, "Series 1", "Group A");
    dataset.addValue(-8.66, "Series 1", "Group B");
    dataset.addValue(4.71, "Series 1", "Group C");
    dataset.addValue(-3.51, "Series 1", "Group  D");
    dataset.addValue(32.64, "Series 1", "Group E");
    return dataset;
}

public JFreeChart createChart() {
    JFreeChart chart = ChartFactory.createWaterfallChart(
          "Waterfall Chart",
          "X-Axis",
          "Y-Axis",
          createDataset(),
          PlotOrientation.VERTICAL,
          true,
          true,
          false
      );

      CategoryPlot plot = (CategoryPlot) chart.getPlot();

      NumberAxis yAxis = (NumberAxis)plot.getRangeAxis();
      yAxis.setRange(0.0, 50.0);
      yAxis.setTickUnit(new NumberTickUnit(10.0));
      BarRenderer renderer = (BarRenderer) plot.getRenderer();
      renderer.setDrawBarOutline(false);

      //Spaces between bars - not working
      renderer.setItemMargin(0.5);

      return chart;
}

Output:

enter image description here

TheGaME
  • 443
  • 2
  • 8
  • 21
  • You may have to determine a satisfactory value empirically, as shown [here](https://stackoverflow.com/a/17342522/230513). – trashgod Sep 13 '19 at 23:18
  • This code renderer.setItemMargin(0.5) worked for BarRenderer when creating bar and stackedBar chart but not for waterfall chart. Does waterfall chart API account for item margin for bars? I am unable to figure it out. – TheGaME Sep 16 '19 at 15:17
  • I looks like the `WaterfallBarRenderer` ignores the margin in `drawItem()`. – trashgod Sep 16 '19 at 20:14

1 Answers1

1

Upon debugging I was able to see that WaterfallBarRenderer ignores the value set by the method renderer.setItemMargin(double).

I was able to add space between bars using the code below.

CategoryPlot plot = (CategoryPlot)chart.getPlot();
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setCategoryMargin(0.5);

enter image description here

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
TheGaME
  • 443
  • 2
  • 8
  • 21