import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Side; import javafx.scene.Scene; import javafx.scene.chart.*; import javafx.scene.control.*; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class EmptyChartSample extends Application { XYChart testedChart; ToggleButton legendVisible; Slider prefWidth; Slider prefHeight; ChoiceBox legendSide; ChoiceBox titleSide; ChoiceBox charts; Stage myStage; { charts = new ChoiceBox(); charts.getItems().addAll( new AreaChart(new NumberAxis(0, 100, 10), new NumberAxis(0, 100, 10)) // new BarChart(new NumberAxis(0, 100, 10), new NumberAxis(0, 100, 10) ,new BubbleChart(new NumberAxis(0, 100, 10), new NumberAxis(0, 100, 10)) // new LineChart(new NumberAxis(0, 100, 10), new NumberAxis(0, 100, 10)) // new ScatterChart(new NumberAxis(0, 100, 10), new NumberAxis(0, 100, 10)) ); } @Override public void start(Stage stage) { myStage = stage; stage.setTitle("Area Chart Sample"); testedChart = charts.getItems().get(0); charts.setValue(testedChart); initFields(); initBindings(); setUpScene(); } private void setUpScene() { Pane pane = new Pane(); pane.setPrefSize(600, 600); pane.getChildren().add(testedChart); HBox root = new HBox(5); root.getChildren().add(pane); root.getChildren().add(new VBox( charts, legendVisible, new HBox(new Label("Pref width"), prefWidth), new HBox(new Label("Pref height"), prefHeight), new HBox(new Label("Legend side"), legendSide), new HBox(new Label("Title side"), titleSide))); Scene scene = new Scene(root,800,600); myStage.setScene(scene); myStage.show(); } private void initBindings() { testedChart.legendVisibleProperty().bind(legendVisible.selectedProperty()); testedChart.prefWidthProperty().bind(prefWidth.valueProperty()); testedChart.prefHeightProperty().bind(prefHeight.valueProperty()); legendSide.setValue(Side.TOP); testedChart.legendSideProperty().bind(legendSide.valueProperty()); titleSide.setValue(Side.TOP); testedChart.titleSideProperty().bind(titleSide.valueProperty()); charts.valueProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, XYChart oldVal, XYChart newVal) { if (newVal != testedChart) { testedChart = newVal; initFields(); initBindings(); setUpScene(); } } }); } private void initFields() { legendVisible = new ToggleButton("Legend visible"); prefWidth = new Slider(); prefWidth.setMin(100); prefWidth.setMax(600); prefWidth.setValue(500); prefWidth.setMajorTickUnit(50); prefWidth.setShowTickLabels(true); prefHeight = new Slider(); prefHeight.setMin(100); prefHeight.setMax(600); prefHeight.setValue(500); prefHeight.setMajorTickUnit(50); prefHeight.setShowTickLabels(true); legendSide = new ChoiceBox(); legendSide.getItems().setAll(Side.values()); titleSide = new ChoiceBox(); titleSide.getItems().setAll(Side.values()); } public static void main(String[] args) { launch(args); } }