问题
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.
回答1:
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.
回答2:
Another solution may be found here on the jfreechart board
In short, add the following to your code where you generate the X-Axis :
plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(1.0f);
or, for your code above;
domainAxis.setMaximumCategoryLabelWidthRatio(1.0f);
This solved the problem for me using a horizontal bar chart, where long domain labels were truncated with the "..." suffix.
回答3:
Try this:
// change the margin at the top of the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setUpperMargin(0.15);
rangeAxis.setLowerMargin(0.15);
来源:https://stackoverflow.com/questions/16091206/jfreechart-last-x-axis-label-cut-off