我有一个漂亮的 JFreeChart 生成的折线图,但有一个异常(exception) - X 轴上的最后一个值被截断,如下所示:
| | |_________|________|__________|_________|_________| 100 Hz 1 kHz 10 kHz 100 kHz 1 MHz 10 MH
(注意 MHz 中的 z 被截断)。
我在这里研究了解决方案:
JFreeChart tick label cut off
但是因为我在 LogAxis
中手动指定范围,从 JavaDoc 看来,设置边距没有效果。
JavaDoc
这是我用来生成 X 轴的代码:
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);
如果有帮助,我还会使用以下命令将其写入 .svg 文件:
// 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();
鉴于 setUpperMargin() 方法不会产生效果,我将如何确保标签有足够的空间来完全打印?我也愿意旋转刻度标签,尽管我也没有弄清楚如何做到这一点,只知道如何旋转轴标签。
最佳答案
好的,找到了一个有效的解决方案。
我用过
chart.setPadding(new RectangleInsets(10, 10, 10, 10));
显然,您的里程可能会有所不同,但这似乎适合我的目的。
关于JFreeChart 最后 X 轴标签被截掉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16091206/