1. ScatterChart Image File Generation
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | package javafximage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.ScatterChart; import javafx.scene.chart.XYChart; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; public class JavaFxScatterChart extends Application { WritableImage getScatterChart(String title, int width, int height, List<XYChart.Series> data) { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); final ScatterChart<String,Number> chart = new ScatterChart<>(xAxis,yAxis); xAxis.setLabel("Number of Month"); xAxis.setAnimated(false); xAxis.setTickLabelsVisible(true); yAxis.setLabel("Values"); yAxis.setAnimated(false); yAxis.setTickLabelsVisible(true); chart.setTitle(title); chart.getData().addAll(data.toArray(new XYChart.Series[data.size()])); Scene scene = new Scene(chart, width, height); return scene.snapshot(null); } @Override public void start(Stage stage) throws Exception { List<XYChart.Series> data = new ArrayList<>(); XYChart.Series series1 = new XYChart.Series(); series1.setName("Data 1"); series1.getData().add(new XYChart.Data("Jan", 23)); series1.getData().add(new XYChart.Data("Feb", 14)); series1.getData().add(new XYChart.Data("Mar", 15)); series1.getData().add(new XYChart.Data("Apr", 24)); series1.getData().add(new XYChart.Data("May", 34)); series1.getData().add(new XYChart.Data("Jun", 36)); series1.getData().add(new XYChart.Data("Jul", 22)); series1.getData().add(new XYChart.Data("Aug", 45)); series1.getData().add(new XYChart.Data("Sep", 43)); series1.getData().add(new XYChart.Data("Oct", 17)); series1.getData().add(new XYChart.Data("Nov", 29)); series1.getData().add(new XYChart.Data("Dec", 25)); data.add(series1); XYChart.Series series2 = new XYChart.Series(); series2.setName("Data 2"); series2.getData().add(new XYChart.Data("Jan", 33)); series2.getData().add(new XYChart.Data("Feb", 34)); series2.getData().add(new XYChart.Data("Mar", 25)); series2.getData().add(new XYChart.Data("Apr", 44)); series2.getData().add(new XYChart.Data("May", 39)); series2.getData().add(new XYChart.Data("Jun", 16)); series2.getData().add(new XYChart.Data("Jul", 55)); series2.getData().add(new XYChart.Data("Aug", 54)); series2.getData().add(new XYChart.Data("Sep", 48)); series2.getData().add(new XYChart.Data("Oct", 27)); series2.getData().add(new XYChart.Data("Nov", 37)); series2.getData().add(new XYChart.Data("Dec", 29)); data.add(series2); try { WritableImage image = getScatterChart("Annunal Trend",640,480,data); ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png" , new File("D:/scatter.png")); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } public static void main(String[] args) { launch(args); } } |
Retult: (D:/scatter.png)
2. ScatterChart Example 2
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | package javafximage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.ScatterChart; import javafx.scene.chart.XYChart; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; public class JavaFxScatterChart2 extends Application { WritableImage getScatterChart2(String title, int width, int height, List<XYChart.Series> data) { final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); final ScatterChart<Number,Number> chart = new ScatterChart<>(xAxis,yAxis); xAxis.setLabel("Number of Month"); xAxis.setAnimated(false); xAxis.setTickLabelsVisible(true); yAxis.setLabel("Values"); yAxis.setAnimated(false); yAxis.setTickLabelsVisible(true); chart.setTitle(title); chart.getData().addAll(data.toArray(new XYChart.Series[data.size()])); Scene scene = new Scene(chart, width, height); return scene.snapshot(null); } @Override public void start(Stage stage) throws Exception { List<XYChart.Series> data = new ArrayList<>(); XYChart.Series series1 = new XYChart.Series(); series1.setName("Data 1"); 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)); data.add(series1); XYChart.Series series2 = new XYChart.Series(); series2.setName("Data 2"); 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)); data.add(series2); try { WritableImage image = getScatterChart2("Annunal Trend",640,480,data); ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png" , new File("D:/scatter2.png")); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } public static void main(String[] args) { launch(args); } } |
Result: (D:/scatter2.png)
댓글 없음:
댓글 쓰기