我正在尝试获取图表节点的屏幕截图。因此,即使将轴值添加到图表中,它们也不会显示在屏幕截图中。
public void saveProfitLineChartToFile(ObservableList<MonthlyRecord> monthlyRecords,String path) throws IOException {
CategoryAxis monthAxis = new CategoryAxis();
monthAxis.setLabel("Month");
NumberAxis profitAxis = new NumberAxis();
profitAxis.setLabel("Profit (Rs)");
LineChart profitLineChart = new LineChart(monthAxis, profitAxis);
XYChart.Series series1 = new XYChart.Series();
int[] profitValues = new int[monthlyRecords.size()];
int[] monthNo = new int[monthlyRecords.size()];
for (int i = 0; i < monthlyRecords.size(); i++) {
profitValues[i] = Integer.parseInt(monthlyRecords.get(i).getProfit());
monthNo[i] = Integer.parseInt(monthlyRecords.get(i).getMonth());
series1.getData().add(new XYChart.Data(monthName[monthNo[i]-1], profitValues[i]));
}
profitLineChart.getData().addAll(series1);
profitLineChart.setLegendVisible(false);
Scene sceneForChart = new Scene(profitLineChart, 800, 600);
WritableImage image = profitLineChart.snapshot(new SnapshotParameters(), null);
File file = new File(path);
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}