c8b9c50594a2 — Quintillus tip 15 days ago
Fix a few bugs with the engine layout chart hiding the options, and always reverting to layout/numbers, if a new file was opened.  Now, it'll keep the filters when switching, making it easier to e.g. compare popular fuel types across games.
M src/main/java/com/ajtjp/gearcityuserinterface/controller/EngineLayoutController.java +49 -19
@@ 59,13 59,18 @@ public class EngineLayoutController impl
                 engineLayoutChart.getStylesheets().add("styles/styles.css");
                 engineLayoutChart.setPrefHeight(2400);
 
-                results = LayoutResult.getLayoutSummary();
+                getNewResults(cmbLayoutCylinders.getSelectionModel().getSelectedItem().toString());
 
-                setupNumberData();
+                if (cmbNumbersOrPercent.getSelectionModel().getSelectedItem().toString().equals("Numbers")) {
+                    setupNumberData();
+                }
+                else {
+                    setupPercentData();
+                }
 
                 Platform.runLater(() -> {
                     setupComboBox();
-                    vbox.getChildren().remove(vbox.getChildren().size() - 1);
+                    vbox.getChildren().removeIf((node) -> node instanceof ToggleableLineChart);
                     vbox.getChildren().add(engineLayoutChart);
                 });
             }

          
@@ 116,6 121,7 @@ public class EngineLayoutController impl
         String lastSeries = "";
         Series currentSeries = null;
         
+        int maxYear = 1900;
         for (LayoutResult result : results) {
             if (!result.getLayoutAndCylinders().equals(lastSeries)) {
                 if (currentSeries != null) {

          
@@ 126,8 132,23 @@ public class EngineLayoutController impl
                 lastSeries = result.getLayoutAndCylinders();
             }
             currentSeries.getData().add(new XYChart.Data(result.getYear(), result.getSales() * 100.0 / totalSalesByYear.get(result.getYear())));
+            if (result.getYear() > maxYear) {
+                maxYear = result.getYear();
+            }
         }
         engineLayoutChart.getData().add(currentSeries);
+        ((NumberAxis)engineLayoutChart.getXAxis()).setUpperBound(maxYear + 1);
+        ((NumberAxis)engineLayoutChart.getXAxis()).setTickLabelFormatter(new StringConverter<Number>() {
+            @Override
+            public String toString(Number object) {
+                return (object.intValue()) + "";
+            }
+
+            @Override
+            public Number fromString(String string) {
+                return 0;
+            }
+        });
         engineLayoutChart.initialize();
     }
 

          
@@ 159,22 180,7 @@ public class EngineLayoutController impl
         cmbLayoutCylinders.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
             @Override public void changed(ObservableValue<? extends String> selected, String oldSelection, String newSelection) {
             if (!newSelection.equals(oldSelection)) {
-                if (newSelection.equals("Layout")) {
-                    engineLayoutChart.setTitle("Engine Layout by Year");
-                    results = LayoutResult.getLayoutSummary();
-                }
-                else if (newSelection.equals("Layout and Cylinders")) {
-                    engineLayoutChart.setTitle("Engine Layout and Cylinder Count by Year");
-                    results = LayoutResult.getLayoutDetails();
-                }
-                else if (newSelection.equals("Cylinders")) {
-                    engineLayoutChart.setTitle("Cylinder Count by Year");
-                    results = LayoutResult.getCylinderDetails();
-                }
-                else if (newSelection.equals("Fuel Type")) {
-                    engineLayoutChart.setTitle("Fuel Type by Year");
-                    results = LayoutResult.getFuelTypeDetails();
-                }
+                getNewResults(newSelection);
                 if (cmbNumbersOrPercent.getSelectionModel().getSelectedItem().equals("Numbers")) {
                     setupNumberData();                    
                 }

          
@@ 185,4 191,28 @@ public class EngineLayoutController impl
           }
         });
     }
+
+    /**
+     * Fetches new results (and updates the title) based on the selection in the
+     * combo box.
+     * @param newSelection 
+     */
+    private void getNewResults(String newSelection) {
+        if (newSelection.equals("Layout")) {
+            engineLayoutChart.setTitle("Engine Layout by Year");
+            results = LayoutResult.getLayoutSummary();
+        }
+        else if (newSelection.equals("Layout and Cylinders")) {
+            engineLayoutChart.setTitle("Engine Layout and Cylinder Count by Year");
+            results = LayoutResult.getLayoutDetails();
+        }
+        else if (newSelection.equals("Cylinders")) {
+            engineLayoutChart.setTitle("Cylinder Count by Year");
+            results = LayoutResult.getCylinderDetails();
+        }
+        else if (newSelection.equals("Fuel Type")) {
+            engineLayoutChart.setTitle("Fuel Type by Year");
+            results = LayoutResult.getFuelTypeDetails();
+        }
+    }
 }