jfreechart icon indicating copy to clipboard operation
jfreechart copied to clipboard

Removing space before and after chart

Open ibrahimatcha opened this issue 1 year ago • 8 comments

Hi,

I am using a CategoryDataset to produce a bar chart which is then being rendered using a LineAndShapeRenderer to appear as a line chart. I'm having an issue where I cannot find a way to remove the gaps before and after the chart! I want it so that the first value sits on the y-axis and the last value sits on the edge of the graph. Please advise how I can get rid of the space marked by the black squiggly lines.

Screenshot 2023-11-21 at 11 50 12

ibrahimatcha avatar Nov 21 '23 11:11 ibrahimatcha

CategoryDataset dataset = DatasetUtils.createCategoryDataset("Data", data); JFreeChart chart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.VERTICAL, false, false, false); LineAndShapeRenderer renderer = new LineAndShapeRenderer();

    CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
    categoryPlot.setRenderer(renderer);

    ValueAxis yAxis3 = categoryPlot.getRangeAxis();
    TickUnits tickUnits = new TickUnits();
    tickUnits.add(new NumberTickUnit(10000));
    yAxis3.setStandardTickUnits(tickUnits);

    yAxis3.setUpperBound(Math.round(yAxis3.getUpperBound()/10000) * 10000);`

ibrahimatcha avatar Nov 21 '23 11:11 ibrahimatcha

My markup didn't format properly ;(

ibrahimatcha avatar Nov 21 '23 11:11 ibrahimatcha

What happens if you adjust the lower and upper margins of your CategoryAxis?

trashgod avatar Nov 21 '23 12:11 trashgod

Unfortunately adding xAxis.setLowerMargin(0); xAxis.setUpperMargin(0); results in the same result Screenshot 2023-11-21 at 12 58 42

NOTE: This is the same chart, just done a bunch of styling

ibrahimatcha avatar Nov 21 '23 12:11 ibrahimatcha

What happens if you specify a negative value for the lower and upper margins of your CategoryAxis?

domainAxis.setLowerMargin(-0.05);
domainAxis.setUpperMargin(-0.05);

trashgod avatar Nov 21 '23 17:11 trashgod

Using a negative margin produced the desired effect for the most part. It has removed those gaps but it doesn't exactly sit on the y axis and the ends of the rounded stroke get cut off, it's progress for sure but unsure what other options there are ;( Screenshot 2023-11-22 at 08 59 45

ibrahimatcha avatar Nov 22 '23 08:11 ibrahimatcha

That's with negative values of -1.33 to try and get it as tight to the y axis as possible without cutting it off

ibrahimatcha avatar Nov 22 '23 09:11 ibrahimatcha

As far as I know, the axis margin is the only relevant, supported property, also cited here for reference.

trashgod avatar Nov 22 '23 19:11 trashgod