1. AreaChart 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.AreaChart; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; public class JavaFxAreaChart extends Application { WritableImage getAreaChart(String title, int width, int height, List<XYChart.Series> data) { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); final AreaChart<String,Number> chart = new AreaChart<>(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> areaChartData = 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)); areaChartData.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)); areaChartData.add(series2); try { WritableImage image = getAreaChart("Annunal Trend",640,480,areaChartData); ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png" , new File("D:/area.png")); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } public static void main(String[] args) { launch(args); } } |
Result:
댓글 없음:
댓글 쓰기