jfree / jfreechart

A 2D chart library for Java applications (JavaFX, Swing or server-side).
http://www.jfree.org/jfreechart/
GNU Lesser General Public License v2.1
1.2k stars 459 forks source link

Removing space before and after chart #388

Open ibrahimatcha opened 10 months ago

ibrahimatcha commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

My markup didn't format properly ;(

trashgod commented 10 months ago

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

ibrahimatcha commented 10 months ago

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

trashgod commented 10 months ago

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);
ibrahimatcha commented 10 months ago

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 commented 10 months ago

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

trashgod commented 10 months ago

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