1. BarChart 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.BarChart; 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 JavaFxBarChart extends Application { WritableImage getBarChart(String title, int width, int height, List<XYChart.Series> data) { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); final BarChart<String,Number> chart = new BarChart<String,Number>(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 = getBarChart("Annunal Trend",640,480,data); ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png" , new File("D:/bar.png")); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } public static void main(String[] args) { launch(args); } } |
Result: (D:/bar.png)
2. Horizontal BarChart 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 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.BarChart; 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 JavaFxBarHorizontalChart extends Application { WritableImage getBarHorizontalChart(String title, int width, int height, List<XYChart.Series> data) { final NumberAxis xAxis = new NumberAxis(); final CategoryAxis yAxis = new CategoryAxis(); final BarChart<Number,String> chart = new BarChart<>(xAxis,yAxis); xAxis.setLabel("Number of Month"); xAxis.setAnimated(false); xAxis.setTickLabelsVisible(true); xAxis.setTickLabelRotation(90); 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(23, "Jan")); series1.getData().add(new XYChart.Data(14, "Feb")); series1.getData().add(new XYChart.Data(15, "Mar")); series1.getData().add(new XYChart.Data(24, "Apr")); series1.getData().add(new XYChart.Data(34, "May")); series1.getData().add(new XYChart.Data(36, "Jun")); series1.getData().add(new XYChart.Data(22, "Jul")); series1.getData().add(new XYChart.Data(45, "Aug")); series1.getData().add(new XYChart.Data(43, "Sep")); series1.getData().add(new XYChart.Data(17, "Oct")); series1.getData().add(new XYChart.Data(29, "Nov")); series1.getData().add(new XYChart.Data(25, "Dec")); data.add(series1); XYChart.Series series2 = new XYChart.Series(); series2.setName("Data 2"); series2.getData().add(new XYChart.Data(33, "Jan")); series2.getData().add(new XYChart.Data(34, "Feb")); series2.getData().add(new XYChart.Data(25, "Mar")); series2.getData().add(new XYChart.Data(44, "Apr")); series2.getData().add(new XYChart.Data(39, "May")); series2.getData().add(new XYChart.Data(16, "Jun")); series2.getData().add(new XYChart.Data(55, "Jul")); series2.getData().add(new XYChart.Data(54, "Aug")); series2.getData().add(new XYChart.Data(48, "Sep")); series2.getData().add(new XYChart.Data(27, "Oct")); series2.getData().add(new XYChart.Data(37, "Nov")); series2.getData().add(new XYChart.Data(29, "Dec")); data.add(series2); try { WritableImage image = getBarHorizontalChart("Annunal Trend",640,480,data); ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png" , new File("D:/barhori.png")); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } public static void main(String[] args) { launch(args); } } |
Result: (D:/barhori.png)
3. Stacked BarChart 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 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.BarChart; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.StackedBarChart; import javafx.scene.chart.XYChart; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; public class JavaFxBarStackedChart extends Application { WritableImage getStackedBarChart(String title, int width, int height, List<XYChart.Series> data) { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); final StackedBarChart<String,Number> chart = new StackedBarChart<>(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 = getStackedBarChart("Annunal Trend",640,480,data); ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png" , new File("D:/barstack.png")); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } public static void main(String[] args) { launch(args); } } |
Result: (D:/barstack.png)
댓글 없음:
댓글 쓰기