Search code examples
jfreecharttruncationaxis-labels

JFreeChart last X-axis label cut off


I've got a nice, pretty JFreeChart-generated Line chart, with one exception - the last value on my X axis is getting truncated, like so:


      |
      |
      |_________|________|__________|_________|_________|
    100 Hz    1 kHz    10 kHz    100 kHz    1 MHz    10 MH

(Note the z in MHz is cut off).

I've looked into the solution here:
JFreeChart tick label cut off

but because I'm manually specifying the range in a LogAxis it appears from the JavaDoc that setting the margin has no effect.
JavaDoc

This is the code I'm using to generate my X-axis:


      final LogAxis domainAxis = new LogAxis(frequencyAxisLabel);
      domainAxis.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.ENGLISH));
      domainAxis.setRange(100, 10000000); //100Hz to 10MHz
      domainAxis.setUpperMargin(.05);  //Has no effect
      domainAxis.setNumberFormatOverride(new UnitNumberFormat(UnitValue.HERTZ));
      plot.setDomainAxis(domainAxis);

If it helps, I'm also writing this to a .svg file using the following:


          // Get a DOMImplementation and create an XML document
      DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
      Document document = domImpl.createDocument(null, "svg", null);

      // Create an instance of the SVG Generator
      SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
      svgGenerator.setSVGCanvasSize(new Dimension(720, 470));

      // draw the chart in the SVG generator
      Rectangle bounds = new Rectangle(10, 10, 700, 450);
      chart.draw(svgGenerator, bounds);

      // Write svg file
      OutputStream outputStream = new FileOutputStream(new File("SomePath));
      Writer out = new OutputStreamWriter(outputStream, "UTF-8");
      svgGenerator.stream(out, true /* use css */);
      outputStream.flush();
      outputStream.close();

Given that the setUpperMargin() method is not going to have an effect, how would I go about making sure the label has enough space to fully print? I'm open to rotating the tick labels as well, though I haven't figured out how to do that either, only how to rotate the axis label.


Solution

  • Ok, found one solution that works.

    I used

    chart.setPadding(new RectangleInsets(10, 10, 10, 10));
    

    obviously your mileage may vary, but this seems to work for my purposes.