Tom
Tom

Reputation: 1385

JFreeChart align BarChart width across subplots

I'm building multiple stacked bar charts (subplots) that are combined through a CombinedRangeCategoryPlot.

As the subplots datasets do not have the same number of items and since JFreeChart decides to allocate the same space for each subplot, I have different widths of bars.

Is there any way I can align their width (even if it means that the subplots have different widths)?

Please see below for the result and the code I have so far.

Many thanks, Thomas

enter image description here

//Builds commong range axis
NumberAxis rangeAxis = new NumberAxis("%");
rangeAxis.setRange(0, 1.0);
rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());

//Builds common data set
CombinedRangeCategoryPlot combinedPlots = new CombinedRangeCategoryPlot(rangeAxis);
for (int groupIndex=0; groupIndex<LeakGroups.values().length; ++groupIndex){
    //Builds category axis
    CategoryAxis categoryAxis = new CategoryAxis(GuiConstants.LEAK_GROUPS_LABELS[groupIndex]);
    //Sets margins between bars
    categoryAxis.setCategoryMargin(0.5f);

    //Builds bar renderer
    StackedBarRenderer barRenderer = new StackedBarRenderer();
    barRenderer.setRenderAsPercentages(true);


    //Builds dot/level renderer
    LineAndShapeRenderer dotRenderer = new LineAndShapeRenderer();
    //dotRenderer.setSeriesLinesVisible(0, false);
    //dotRenderer.setSeriesShapesVisible(0, false);
    //dotRenderer.setSeriesLinesVisible(1, false);
    //Defines level shape height (depends on chart size): nominal values are for a height of 1000px 
    int shapeHeightPx = (int) Math.round(20 * (this.getHeight() / 1000.0));
    dotRenderer.setSeriesShape(1, new Rectangle(-1, -shapeHeightPx/2, 2, shapeHeightPx));

    //Builds plot
    CategoryPlot plot = new CategoryPlot();
    plot.setDomainAxis(categoryAxis);
    plot.setDataset(0, data[groupIndex].bars);
    plot.setRenderer(0, barRenderer);
    plot.setDataset(1, data[groupIndex].dots);
    plot.setRenderer(1, dotRenderer);

    //Adds to combined
    combinedPlots.add(plot);
}
combinedPlots.setOrientation(PlotOrientation.HORIZONTAL);
//Puts range axis at the bottom
combinedPlots.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
//Changes plot render sequence so that bars are in the background and shapes in front
combinedPlots.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
//Shows gridlines for categories and not for values
combinedPlots.setDomainGridlinesVisible(true);
combinedPlots.setRangeGridlinesVisible(false);

//Creates chart
JFreeChart chart = new JFreeChart("Leaks", combinedPlots);

//Sets a margin right to allow space for last catergory label ("100%")
chart.setPadding(new RectangleInsets(0, 0, 0, 20));

return chart;

Upvotes: 1

Views: 929

Answers (2)

trashgod
trashgod

Reputation: 205785

For some reason, the weight gets reset to value 1 when adding the plot.

By way of explanation,

Upvotes: 1

Tom
Tom

Reputation: 1385

After a few hours of search, found the solution: use plot.setWeight().

For some reason, the weight gets reset to value 1 when adding the plot to the CombinedRangeCategoryPlot, hence it has to be set after.

Hope this helps.

Upvotes: 1

Related Questions