Reputation: 453
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;
}
Upvotes: 1
Views: 300
Reputation: 453
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);
Upvotes: 1