1. PieChart Image Create
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package javafximage; import java.io.File; import java.io.IOException; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Scene; import javafx.scene.SnapshotParameters; import javafx.scene.chart.PieChart; import javafx.scene.chart.PieChart.Data; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; public class JavaFxPieChart extends Application { WritableImage getPieChart(String title, int width, int height, ObservableList<Data> data) { PieChart chart = new PieChart(data); Scene scene = new Scene(chart, width, height); chart.setTitle(title); return scene.snapshot(null); //or return chart.snapshot(new SnapshotParameters(), null); } @Override public void start(Stage stage) throws Exception { ObservableList<Data> pieChartData = FXCollections.observableArrayList( new javafx.scene.chart.PieChart.Data("Peachs", 13), new javafx.scene.chart.PieChart.Data("Oranges", 25), new javafx.scene.chart.PieChart.Data("Plums", 10), new javafx.scene.chart.PieChart.Data("Pears", 22), new javafx.scene.chart.PieChart.Data("Apples", 30)); try { WritableImage image = getPieChart("Fruits",640,480,pieChartData); ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png" , new File("D:/pie.png")); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } public static void main(String[] args) { launch(args); } } |
Result: (D:/pie.png)
2. Move Legend to Left Side
1 2 3 4 5 6 7 | WritableImage getPieChart(String title, int width, int height, ObservableList<Data> data) { PieChart chart = new PieChart(data); chart.setTitle(title); chart.setLegendSide(Side.LEFT); Scene scene = new Scene(chart, width, height); return scene.snapshot(null); } |
Result:
댓글 없음:
댓글 쓰기