jfreechart
jfreechart copied to clipboard
Removing space before and after chart
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.
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);`
My markup didn't format properly ;(
What happens if you adjust the lower and upper margins of your CategoryAxis
?
Unfortunately adding xAxis.setLowerMargin(0); xAxis.setUpperMargin(0);
results in the same result
NOTE: This is the same chart, just done a bunch of styling
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);
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 ;(
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
As far as I know, the axis margin is the only relevant, supported property, also cited here for reference.