【问题标题】:Saving JavaFX chart to pdf does not show the x and y -axsis values将 JavaFX 图表保存为 pdf 不显示 x 和 y 轴值
【发布时间】:2018-08-27 20:47:16
【问题描述】:

所以我有这个散点图:

final NumberAxis xAxis = new NumberAxis(0, 10, 1);
final NumberAxis yAxis = new NumberAxis(-100, 500, 100);
final ScatterChart<Number, Number> sc = new ScatterChart<Number, Number>(xAxis, yAxis);
xAxis.setLabel("Age (years)");
yAxis.setLabel("Returns to date");
sc.setTitle("Investment Overview");

XYChart.Series series1 = new XYChart.Series();
series1.setName("Equities");
series1.getData().add(new XYChart.Data(4.2, 193.2));
series1.getData().add(new XYChart.Data(2.8, 33.6));
series1.getData().add(new XYChart.Data(6.2, 24.8));
series1.getData().add(new XYChart.Data(1, 14));
series1.getData().add(new XYChart.Data(1.2, 26.4));
series1.getData().add(new XYChart.Data(4.4, 114.4));
series1.getData().add(new XYChart.Data(8.5, 323));
series1.getData().add(new XYChart.Data(6.9, 289.8));
series1.getData().add(new XYChart.Data(9.9, 287.1));
series1.getData().add(new XYChart.Data(0.9, -9));
series1.getData().add(new XYChart.Data(3.2, 150.8));
series1.getData().add(new XYChart.Data(4.8, 20.8));
series1.getData().add(new XYChart.Data(7.3, -42.3));
series1.getData().add(new XYChart.Data(1.8, 81.4));
series1.getData().add(new XYChart.Data(7.3, 110.3));
series1.getData().add(new XYChart.Data(2.7, 41.2));

XYChart.Series series2 = new XYChart.Series();
series2.setName("Mutual funds");
series2.getData().add(new XYChart.Data(5.2, 229.2));
series2.getData().add(new XYChart.Data(2.4, 37.6));
series2.getData().add(new XYChart.Data(3.2, 49.8));
series2.getData().add(new XYChart.Data(1.8, 134));
series2.getData().add(new XYChart.Data(3.2, 236.2));
series2.getData().add(new XYChart.Data(7.4, 114.1));
series2.getData().add(new XYChart.Data(3.5, 323));
series2.getData().add(new XYChart.Data(9.3, 29.9));
series2.getData().add(new XYChart.Data(8.1, 287.4));

sc.getData().addAll(series1, series2);
Scene scene = new Scene(sc, 500, 400);
Stage stage = (Stage) this.bttn_LoadCSVFolder.getScene().getWindow();

Whitch 看起来像这样:

使用此代码,我设法将图表保存为 PDF:

WritableImage image = sc.snapshot(new SnapshotParameters(), null);
BufferedImage awtImage = SwingFXUtils.fromFXImage(image, null);
PDImageXObject pdImageXObject = LosslessFactory.createFromImage(doc, awtImage);
PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.drawImage(pdImageXObject, 100, 160, awtImage.getWidth() / 2, awtImage.getHeight() / 2);
contentStream.close();

但是当我将图表保存到我的 pdf 文件(甚至只是作为图像)时,不会显示轴的 x 和 y 值:

【问题讨论】:

  • 为什么你的图片尺寸是 1 x 1 ? new BufferedImage(1,1,1);
  • @TilmanHausherr - 这可能与问题无关,因为documentation 说“如果提供的对象为空、太小或图像像素的类型,将创建一个新的 BufferedImage不能轻易转换成”。只通过null 可能对性能和可读性都更好,但它不应该影响结果...
  • 好吧...另一个奇怪的事情是文本块中有一个绘图图像。我想知道这是否可以。其他要尝试的事情:将 BufferedImage 保存到带有ImageIO.write() 的文件中,图像在那里吗?如果不是,则不是 PDFBox 问题。顺便说一句,图表使用LosslessFactory,边缘锐利的图像不应该是Jpeg压缩的,这看起来不太好。 Jpeg 最适合拍照。
  • @TilmanHausherr 图像被创建

标签: java pdf javafx pdfbox scatter


【解决方案1】:

将此添加到您的代码中:

xAxis.setAnimated(false); 
yAxis.setAnimated(false);

原因:“这是因为标签是动画的(淡入),因此它们在第一帧(您捕获的)上不可见。” (Source)

如果您的图像看起来模糊,请参阅this answer

【讨论】:

  • 谢谢你解决了我的问题。唯一的问题是右图有点模糊/您可以看到像素。
  • 为此,您应该将原件放大很多(然后再放大),或者使用矢量图形来完成整个工作。
猜你喜欢
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多