functional/ControlsTests/src/javafx/scene/control/test/chart/apps/NewStackedBarChartApp.java

Print this page




  78         NumberAxis axis2;
  79         ObservableList<String> existingCategories;
  80         CategoryAxis axis1;
  81         TabPaneWithControl pane;
  82 
  83         public StackedBarChartScene() {
  84             super("StackedBarChart", 1300, 800);
  85         }
  86 
  87         @Override
  88         final protected void prepareScene() {
  89             Utils.addBrowser(this);
  90 
  91             existingCategories = FXCollections.observableArrayList();
  92             existingCategories.addAll("category1", "category2", "category3");
  93             axis1 = new CategoryAxis(existingCategories);
  94             axis2 = new NumberAxis(0, 100, 10);
  95             testedStackedBarChart = getNewChart();
  96             testedStackedBarChart.setId(TESTED_CHART_ID);
  97 
  98             Button hardResetButton = ButtonBuilder.create().id(HARD_RESET_BUTTON_ID).text("Hard reset").build();

  99             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
 100                 public void handle(ActionEvent t) {
 101                     HBox hb = (HBox) getRoot();
 102                     hb.getChildren().clear();
 103                     prepareMainSceneStructure();
 104                     prepareScene();
 105                 }
 106             });
 107 
 108             Button softResetButton = ButtonBuilder.create().id(SOFT_RESET_BUTTON_ID).text("Soft reset").build();

 109             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 110                 public void handle(ActionEvent t) {
 111                     //throw new UnsupportedOperationException("Not supported yet.");
 112                 }
 113             });
 114 
 115             HBox resetButtonsHBox = new HBox();
 116             resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton);
 117 
 118             VBox vb = new VBox(5);
 119             vb.getChildren().addAll(resetButtonsHBox, getAddItemHBox(), getRemoveDataDialog(), getAddCategoryDialog(), getRemoveCategoryDialog());
 120 
 121             pane = getPaneFor(testedStackedBarChart, CHART_TAB_NAME, axis1, AXIS_1_TAB_NAME, axis2, AXIS_2_TAB_NAME);
 122 
 123             setTestedControlContainerSize(500, 500);
 124             setTestedControl(testedStackedBarChart);
 125             setPropertiesContent(pane);
 126             setControllersContent(vb);
 127         }
 128 
 129         public HBox getRemoveDataDialog() {
 130             HBox hb = new HBox();
 131             Label lb = new Label("From position");
 132             final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).id(REMOVE_ITEM_POS_TEXT_FIELD_ID).build();
 133             Button bt = ButtonBuilder.create().text("Remove!").id(REMOVE_BUTTON_ID).build();



 134             bt.setOnAction(new EventHandler() {
 135                 public void handle(Event t) {
 136                     int index = Integer.parseInt(tf.getText());
 137                     testedStackedBarChart.getData().remove(index);
 138                 }
 139             });
 140             hb.getChildren().addAll(lb, tf, bt);
 141             return hb;
 142         }
 143 
 144         public HBox getRemoveCategoryDialog() {
 145             HBox hb = new HBox();
 146             Label lb = new Label("From index");
 147             final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).build();
 148             Button bt = ButtonBuilder.create().text("Remove category!").build();

 149             bt.setOnAction(new EventHandler() {
 150                 public void handle(Event t) {
 151                     int index = Integer.parseInt(tf.getText());
 152                     existingCategories.remove(index);
 153                     axis1.getCategories().remove(index);
 154                 }
 155             });
 156             hb.getChildren().addAll(lb, tf, bt);
 157             return hb;
 158         }
 159 
 160         public HBox getAddCategoryDialog() {
 161             HBox hb = new HBox();
 162             Label lb = new Label("Category");
 163             final TextField tf = TextFieldBuilder.create().text("").prefWidth(50).build();

 164 
 165             Label lind = new Label("to index");
 166             final TextField tfind = TextFieldBuilder.create().text("0").prefWidth(50).build();

 167 
 168             Button bt = ButtonBuilder.create().text("Add!").build();
 169             bt.setOnAction(new EventHandler() {
 170                 public void handle(Event t) {
 171                     int index = Integer.parseInt(tfind.getText());
 172                     existingCategories.add(index, tf.getText());
 173                     axis1.getCategories().add(index, tf.getText());
 174                 }
 175             });
 176             hb.getChildren().addAll(lb, tf, lind, tfind, bt);
 177             return hb;
 178         }
 179 
 180         public HBox getAddItemHBox() {
 181             HBox hb = new HBox();
 182             Label lb = new Label("Add series named ");
 183             final TextField tf = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_NAME_TEXTFIELD_ID).build();


 184 
 185             Label minLabel = new Label(" min ");
 186             final TextField minText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_MIN_VALUE_TEXTFIELD_ID).build();


 187 
 188             Label maxLabel = new Label(" max ");
 189             final TextField maxText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_MAX_VALUE_TEXTFIELD_ID).build();


 190 
 191             Label amountLabel = new Label(" amount ");
 192             final TextField amountText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_DOTS_COUNT_TEXTFIELD_ID).build();


 193 
 194             Button bt = ButtonBuilder.create().text("Add!").id(ADD_SERIES_COMMAND_BUTTON_ID).build();

 195             bt.setOnAction(new EventHandler() {
 196                 public void handle(Event t) {
 197                     String serieName = tf.getText();
 198                     double min = Double.parseDouble(minText.getText());
 199                     double max = Double.parseDouble(maxText.getText());
 200                     int amount = Integer.parseInt(amountText.getText());
 201 
 202                     ObservableList list = FXCollections.observableArrayList();
 203 
 204                     XYChart.Series serie = new XYChart.Series(serieName, list);
 205 
 206                     for (int i = 0; i < amount; i++) {
 207                         XYChart.Data newData = new XYChart.Data();
 208                         String category = existingCategories.get(new Random().nextInt(existingCategories.size()));
 209                         Double value = new Random().nextDouble() * (max - min) + min;
 210                         newData.setYValue(value);
 211                         newData.setXValue(category);
 212                         list.add(newData);
 213                     }
 214 




  78         NumberAxis axis2;
  79         ObservableList<String> existingCategories;
  80         CategoryAxis axis1;
  81         TabPaneWithControl pane;
  82 
  83         public StackedBarChartScene() {
  84             super("StackedBarChart", 1300, 800);
  85         }
  86 
  87         @Override
  88         final protected void prepareScene() {
  89             Utils.addBrowser(this);
  90 
  91             existingCategories = FXCollections.observableArrayList();
  92             existingCategories.addAll("category1", "category2", "category3");
  93             axis1 = new CategoryAxis(existingCategories);
  94             axis2 = new NumberAxis(0, 100, 10);
  95             testedStackedBarChart = getNewChart();
  96             testedStackedBarChart.setId(TESTED_CHART_ID);
  97 
  98             Button hardResetButton = new Button("Hard reset");

  99             hardResetButton.setId(HARD_RESET_BUTTON_ID);

 100             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
 101                 public void handle(ActionEvent t) {
 102                     HBox hb = (HBox) getRoot();
 103                     hb.getChildren().clear();
 104                     prepareMainSceneStructure();
 105                     prepareScene();
 106                 }
 107             });
 108 
 109             Button softResetButton = new Button("Soft reset");

 110             softResetButton.setId(SOFT_RESET_BUTTON_ID);

 111             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 112                 public void handle(ActionEvent t) {
 113                     //throw new UnsupportedOperationException("Not supported yet.");
 114                 }
 115             });
 116 
 117             HBox resetButtonsHBox = new HBox();
 118             resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton);
 119 
 120             VBox vb = new VBox(5);
 121             vb.getChildren().addAll(resetButtonsHBox, getAddItemHBox(), getRemoveDataDialog(), getAddCategoryDialog(), getRemoveCategoryDialog());
 122             
 123             pane = getPaneFor(testedStackedBarChart, CHART_TAB_NAME, axis1, AXIS_1_TAB_NAME, axis2, AXIS_2_TAB_NAME);
 124 
 125             setTestedControlContainerSize(500, 500);
 126             setTestedControl(testedStackedBarChart);
 127             setPropertiesContent(pane);
 128             setControllersContent(vb);
 129         }
 130 
 131         public HBox getRemoveDataDialog() {
 132             HBox hb = new HBox();
 133             Label lb = new Label("From position");
 134             final TextField tf = new TextField("0");

 135             tf.setPrefWidth(50);

 136             tf.setId(REMOVE_ITEM_POS_TEXT_FIELD_ID);

 137             Button bt = new Button("Remove!");

 138             bt.setId(REMOVE_BUTTON_ID);

 139             bt.setOnAction(new EventHandler() {
 140                 public void handle(Event t) {
 141                     int index = Integer.parseInt(tf.getText());
 142                     testedStackedBarChart.getData().remove(index);
 143                 }
 144             });
 145             hb.getChildren().addAll(lb, tf, bt);
 146             return hb;
 147         }
 148 
 149         public HBox getRemoveCategoryDialog() {
 150             HBox hb = new HBox();
 151             Label lb = new Label("From index");
 152             final TextField tf = new TextField("0");

 153             tf.setPrefWidth(50);

 154             Button bt = new Button("Remove category!");

 155             bt.setOnAction(new EventHandler() {
 156                 public void handle(Event t) {
 157                     int index = Integer.parseInt(tf.getText());
 158                     existingCategories.remove(index);
 159                     axis1.getCategories().remove(index);
 160                 }
 161             });
 162             hb.getChildren().addAll(lb, tf, bt);
 163             return hb;
 164         }
 165 
 166         public HBox getAddCategoryDialog() {
 167             HBox hb = new HBox();
 168             Label lb = new Label("Category");
 169             final TextField tf = new TextField("");

 170             tf.setPrefWidth(50);

 171 
 172             Label lind = new Label("to index");
 173             final TextField tfind = new TextField("0");

 174             tfind.setPrefWidth(50);

 175 
 176             Button bt = new Button("Add!");

 177             bt.setOnAction(new EventHandler() {
 178                 public void handle(Event t) {
 179                     int index = Integer.parseInt(tfind.getText());
 180                     existingCategories.add(index, tf.getText());
 181                     axis1.getCategories().add(index, tf.getText());
 182                 }
 183             });
 184             hb.getChildren().addAll(lb, tf, lind, tfind, bt);
 185             return hb;
 186         }
 187 
 188         public HBox getAddItemHBox() {
 189             HBox hb = new HBox();
 190             Label lb = new Label("Add series named ");
 191             final TextField tf = new TextField();

 192             tf.setPrefWidth(50);

 193             tf.setId(ADDED_SERIES_NAME_TEXTFIELD_ID);

 194 
 195             Label minLabel = new Label(" min ");
 196             final TextField minText = new TextField();

 197             minText.setPrefWidth(50);

 198             minText.setId(ADDED_SERIES_MIN_VALUE_TEXTFIELD_ID);

 199 
 200             Label maxLabel = new Label(" max ");
 201             final TextField maxText = new TextField();

 202             maxText.setPrefWidth(50);

 203             maxText.setId(ADDED_SERIES_MAX_VALUE_TEXTFIELD_ID);

 204 
 205             Label amountLabel = new Label(" amount ");
 206             final TextField amountText = new TextField();

 207             amountText.setPrefWidth(50);

 208             amountText.setId(ADDED_SERIES_DOTS_COUNT_TEXTFIELD_ID);

 209 
 210             Button bt = new Button("Add!");

 211             bt.setId(ADD_SERIES_COMMAND_BUTTON_ID);

 212             bt.setOnAction(new EventHandler() {
 213                 public void handle(Event t) {
 214                     String serieName = tf.getText();
 215                     double min = Double.parseDouble(minText.getText());
 216                     double max = Double.parseDouble(maxText.getText());
 217                     int amount = Integer.parseInt(amountText.getText());
 218 
 219                     ObservableList list = FXCollections.observableArrayList();
 220 
 221                     XYChart.Series serie = new XYChart.Series(serieName, list);
 222 
 223                     for (int i = 0; i < amount; i++) {
 224                         XYChart.Data newData = new XYChart.Data();
 225                         String category = existingCategories.get(new Random().nextInt(existingCategories.size()));
 226                         Double value = new Random().nextDouble() * (max - min) + min;
 227                         newData.setYValue(value);
 228                         newData.setXValue(category);
 229                         list.add(newData);
 230                     }
 231