B 图表示例
本附录列出了以下JavaFX示例的代码:
有关这些源文件的描述,请参见使用JavaFX图表。
PieChartSample.java
/* * 版权所有 (c) 2011, 2014, Oracle及其附属公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和许可: * * 源代码的重新分发和使用,无论是否进行修改,只要满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 以二进制形式重新分发时,必须在文档和/或其他提供的材料中复制上述版权声明、此条件列表和以下免责声明。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用性的暗示保证。 * 在任何情况下,版权所有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用、数据或利润损失;或业务中断)承担任何责任,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他)。 */ package piechartsample; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.chart.*; import javafx.scene.Group; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; public class PieChartSample extends Application { @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("进口水果"); stage.setWidth(500); stage.setHeight(500); ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( new PieChart.Data("葡萄柚", 13), new PieChart.Data("橙子", 25), new PieChart.Data("李子", 10), new PieChart.Data("梨子", 22), new PieChart.Data("苹果", 30)); final PieChart chart = new PieChart(pieChartData); chart.setTitle("进口水果"); final Label caption = new Label(""); caption.setTextFill(Color.DARKORANGE); caption.setStyle("-fx-font: 24 arial;"); chart.getData().stream().forEach((data) ->{ data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> { caption.setTranslateX(e.getSceneX()); caption.setTranslateY(e.getSceneY()); caption.setText(String.valueOf(data.getPieValue()) + "%"); }); }); ((Group) scene.getRoot()).getChildren().addAll(chart, caption); stage.setScene(scene); //scene.getStylesheets().add("piechartsample/Chart.css"); stage.show(); } public static void main(String[] args) { launch(args); } }
LineChartSample.java
/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package linechartsample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.stage.Stage; public class LineChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); xAxis.setLabel("Month"); final LineChart<String,Number> lineChart = new LineChart<>(xAxis,yAxis); lineChart.setTitle("Stock Monitoring, 2010"); lineChart.setCreateSymbols(false); lineChart.setAlternativeRowFillVisible(false); XYChart.Series series1 = new XYChart.Series(); series1.setName("Portfolio 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)); XYChart.Series series2 = new XYChart.Series(); series2.setName("Portfolio 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)); XYChart.Series series3 = new XYChart.Series(); series3.setName("Portfolio 3"); series3.getData().add(new XYChart.Data("Jan", 44)); series3.getData().add(new XYChart.Data("Feb", 35)); series3.getData().add(new XYChart.Data("Mar", 36)); series3.getData().add(new XYChart.Data("Apr", 33)); series3.getData().add(new XYChart.Data("May", 31)); series3.getData().add(new XYChart.Data("Jun", 26)); series3.getData().add(new XYChart.Data("Jul", 22)); series3.getData().add(new XYChart.Data("Aug", 25)); series3.getData().add(new XYChart.Data("Sep", 43)); series3.getData().add(new XYChart.Data("Oct", 44)); series3.getData().add(new XYChart.Data("Nov", 45)); series3.getData().add(new XYChart.Data("Dec", 44)); Scene scene = new Scene(lineChart); lineChart.getData().addAll(series1, series2, series3); //scene.getStylesheets().add("linechartsample/Chart.css"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
AreaChartSample.java
/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package areachartsample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.AreaChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.stage.Stage; public class AreaChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Area Chart Sample"); final NumberAxis xAxis = new NumberAxis(1, 30, 1); final NumberAxis yAxis = new NumberAxis(-5, 27, 5); final AreaChart<Number,Number> ac = new AreaChart<>(xAxis,yAxis); xAxis.setForceZeroInRange(true); ac.setTitle("Temperature Monitoring (in Degrees C)"); XYChart.Series series1 = new XYChart.Series(); series1.setName("March"); series1.getData().add(new XYChart.Data(0, -2)); series1.getData().add(new XYChart.Data(3, -4)); series1.getData().add(new XYChart.Data(6, 0)); series1.getData().add(new XYChart.Data(9, 5)); series1.getData().add(new XYChart.Data(12, -4)); series1.getData().add(new XYChart.Data(15, 6)); series1.getData().add(new XYChart.Data(18, 8)); series1.getData().add(new XYChart.Data(21, 14)); series1.getData().add(new XYChart.Data(24, 4)); series1.getData().add(new XYChart.Data(27, 6)); series1.getData().add(new XYChart.Data(30, 6)); XYChart.Series series2 = new XYChart.Series(); series2.setName("April"); series2.getData().add(new XYChart.Data(0, 4)); series2.getData().add(new XYChart.Data(3, 10)); series2.getData().add(new XYChart.Data(6, 15)); series2.getData().add(new XYChart.Data(9, 8)); series2.getData().add(new XYChart.Data(12, 5)); series2.getData().add(new XYChart.Data(15, 18)); series2.getData().add(new XYChart.Data(18, 15)); series2.getData().add(new XYChart.Data(21, 13)); series2.getData().add(new XYChart.Data(24, 19)); series2.getData().add(new XYChart.Data(27, 21)); series2.getData().add(new XYChart.Data(30, 21)); XYChart.Series series3 = new XYChart.Series(); series3.setName("May"); series3.getData().add(new XYChart.Data(0, 20)); series3.getData().add(new XYChart.Data(3, 15)); series3.getData().add(new XYChart.Data(6, 13)); series3.getData().add(new XYChart.Data(9, 12)); series3.getData().add(new XYChart.Data(12, 14)); series3.getData().add(new XYChart.Data(15, 18)); series3.getData().add(new XYChart.Data(18, 25)); series3.getData().add(new XYChart.Data(21, 25)); series3.getData().add(new XYChart.Data(24, 23)); series3.getData().add(new XYChart.Data(27, 26)); series3.getData().add(new XYChart.Data(30, 26)); Scene scene = new Scene(ac,800,600); //scene.getStylesheets().add("areachartsample/Chart.css"); ac.setHorizontalZeroLineVisible(true); ac.getData().addAll(series1, series2, series3); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
BubbleChartSample.java
/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package bubblechartsample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.BubbleChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.paint.Color; import javafx.stage.Stage; public class BubbleChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Bubble Chart Sample"); final NumberAxis xAxis = new NumberAxis(1, 53, 4); final NumberAxis yAxis = new NumberAxis(0, 80, 10); final BubbleChart<Number,Number> blc = new BubbleChart<>(xAxis,yAxis); xAxis.setLabel("Week"); xAxis.setTickLabelFill(Color.CHOCOLATE); xAxis.setMinorTickCount(4); yAxis.setLabel("Product Budget"); yAxis.setTickLabelFill(Color.CHOCOLATE); yAxis.setTickLabelGap(10); yAxis.setTickLabelFormatter( new NumberAxis.DefaultFormatter(yAxis,"$ ",null) ); blc.setTitle("Budget Monitoring"); XYChart.Series series1 = new XYChart.Series(); series1.setName("Product 1"); series1.getData().add(new XYChart.Data(3, 35, 2)); series1.getData().add(new XYChart.Data(12, 60, 1.8)); series1.getData().add(new XYChart.Data(15, 15, 7)); series1.getData().add(new XYChart.Data(22, 30, 2.5)); series1.getData().add(new XYChart.Data(28, 20, 1)); series1.getData().add(new XYChart.Data(35, 41, 5.5)); series1.getData().add(new XYChart.Data(42, 17, 9)); series1.getData().add(new XYChart.Data(49, 30, 1.8)); XYChart.Series series2 = new XYChart.Series(); series2.setName("Product 2"); series2.getData().add(new XYChart.Data(8, 15, 2)); series2.getData().add(new XYChart.Data(13, 23, 1)); series2.getData().add(new XYChart.Data(15, 45, 3)); series2.getData().add(new XYChart.Data(24, 30, 4.5)); series2.getData().add(new XYChart.Data(38, 78, 1)); series2.getData().add(new XYChart.Data(40, 41, 7.5)); series2.getData().add(new XYChart.Data(45, 57, 2)); series2.getData().add(new XYChart.Data(47, 23, 3.8)); Scene scene = new Scene(blc); blc.getData().addAll(series1, series2); //scene.getStylesheets().add("bubblechartsample/Chart.css"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
ScatterChartSample.java
/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package scatterchartsample; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.ScatterChart; import javafx.scene.chart.XYChart; import javafx.scene.control.Button; import javafx.scene.effect.DropShadow; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ScatterChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Scatter Chart Sample"); final NumberAxis xAxis = new NumberAxis(0, 10, 1); final NumberAxis yAxis = new NumberAxis(-100, 500, 100); final ScatterChart<Number,Number> sc = new ScatterChart<>(xAxis,yAxis); xAxis.setLabel("Age (years)"); yAxis.setLabel("Returns to date"); sc.setTitle("Investment Overview"); XYChart.Series series1 = new XYChart.Series(); series1.setName("Option 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)); sc.setPrefSize(500, 400); sc.getData().addAll(series1); Scene scene = new Scene(new Group()); final VBox vbox = new VBox(); final HBox hbox = new HBox(); final Button add = new Button("Add Series"); add.setOnAction((ActionEvent e) -> { if (sc.getData() == null) sc.setData(FXCollections.<XYChart.Series<Number, Numbe>>observableArrayList()); ScatterChart.Series<Number, Number> series = new ScatterChart.Series<>(); series.setName("Option "+(sc.getData().size()+1)); for (int i=0; i<100; i++) series.getData().add( new ScatterChart.Data<>(Math.random()*100, Math.random()*500)); sc.getData().add(series); }); final Button remove = new Button("Remove Series"); remove.setOnAction((ActionEvent e) -> { if (!sc.getData().isEmpty()) sc.getData().remove((int)( Math.random()*(sc.getData().size()-1))); }); final DropShadow shadow = new DropShadow(); shadow.setOffsetX(2); shadow.setColor(Color.GREY); sc.setEffect(shadow); hbox.setSpacing(10); hbox.getChildren().addAll(add, remove); vbox.getChildren().addAll(sc, hbox); hbox.setPadding(new Insets(10, 10, 10, 50)); ((Group)scene.getRoot()).getChildren().add(vbox); //scene.getStylesheets().add("scatterchartsample/Chart.css"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
BarChartSample.java
/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package barchartsample; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; 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.stage.Stage; import javafx.util.Duration; public class BarChartSample extends Application { final static String austria = "Austria"; final static String brazil = "Brazil"; final static String france = "France"; final static String italy = "Italy"; final static String usa = "USA"; @Override public void start(Stage stage) { stage.setTitle("Bar Chart Sample"); final NumberAxis xAxis = new NumberAxis(); final CategoryAxis yAxis = new CategoryAxis(); final BarChart<Number, String> bc = new BarChart<>(xAxis, yAxis); bc.setTitle("Country Summary"); xAxis.setLabel("Value"); xAxis.setTickLabelRotation(90); yAxis.setLabel("Country"); XYChart.Series series1 = new XYChart.Series(); series1.setName("2003"); series1.getData().add(new XYChart.Data(25601.34, austria)); series1.getData().add(new XYChart.Data(20148.82, brazil)); series1.getData().add(new XYChart.Data(10000, france)); series1.getData().add(new XYChart.Data(35407.15, italy)); series1.getData().add(new XYChart.Data(12000, usa)); XYChart.Series series2 = new XYChart.Series(); series2.setName("2004"); series2.getData().add(new XYChart.Data(57401.85, austria)); series2.getData().add(new XYChart.Data(41941.19, brazil)); series2.getData().add(new XYChart.Data(45263.37, france)); series2.getData().add(new XYChart.Data(117320.16, italy)); series2.getData().add(new XYChart.Data(14845.27, usa)); XYChart.Series series3 = new XYChart.Series(); series3.setName("2005"); series3.getData().add(new XYChart.Data(45000.65, austria)); series3.getData().add(new XYChart.Data(44835.76, brazil)); series3.getData().add(new XYChart.Data(18722.18, france)); series3.getData().add(new XYChart.Data(17557.31, italy)); series3.getData().add(new XYChart.Data(92633.68, usa)); Timeline tl = new Timeline(); tl.getKeyFrames().add(new KeyFrame(Duration.millis(500), (ActionEvent actionEvent) -> { bc.getData().stream().forEach((series) -> { series.getData().stream().forEach((data) -> { data.setXValue(Math.random() * 1000); }); }); })); tl.setCycleCount(Animation.INDEFINITE); tl.setAutoReverse(true); tl.play(); Scene scene = new Scene(bc, 800, 600); bc.getData().addAll(series1, series2, series3); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }