1

I'm using JFreeChart for displaying charts in PDF with Apache PdfBox.

My problem is this: I have a Scatter Plot Chart (image attached) that has fixed lower and upper bound, so auto-calculate is not an option. The chart displays a blue dot with the result. However, if the value of dot is 0 or 2 (edge values), the dot is cut out, so I need to set up a margin in this case. I tried with xAxis.setUpperMargin, but with no luck.

This is part of the code:

NumberAxis xAxis = (NumberAxis) xyPlot.getDomainAxis();
double tickSize = maxValue > 10 ? 1 : 0.5;
xAxis.setTickUnit(new NumberTickUnit(tickSize));
xAxis.setRange(1, maxValue);

Chart Image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Why not pad the range, e.g. `setRange(1, maxValue + margin)`? – trashgod Oct 18 '19 at 03:08
  • @trashgod So, I was looking for solution for two day, made all combinations on axis, and failed, and it was this simple. This helped, if you could post it as answer, I would accept it, please. – DraganKocic Oct 18 '19 at 12:09

1 Answers1

1

As you have observed, a "margin is added only when the axis range is auto-calculated—if you set the axis range manually, the margin is ignored." Alternatively, you can add a suitable margin when manually setting the range. Starting from this example, the following change to adjustAxis() adds a 10% margin to each end of each axis, producing the result shown.

axis.setRange(-1.1, 1.1);

scatter plot with margin

trashgod
  • 203,806
  • 29
  • 246
  • 1,045