diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index 6b19101c..a7e001ab 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -75,6 +75,7 @@ public class Controller implements Initializable { private ConfigDialog configDialog = new ConfigDialog(this); private HooksDialog hooksDialog = new HooksDialog(this); private MobilityDialog mobilityDialog = new MobilityDialog(this); + private ChartDialog chartDialog = new ChartDialog(this); public Controller() { // load configuration @@ -228,6 +229,11 @@ public class Controller implements Initializable { } } + @FXML + private void onTestMenuCharts(ActionEvent event) { + chartDialog.show(); + } + @Override public void initialize(URL location, ResourceBundle resources) { logger.info("controller initialize"); diff --git a/corefx/src/main/java/com/core/ui/ChartDialog.java b/corefx/src/main/java/com/core/ui/ChartDialog.java new file mode 100644 index 00000000..9a8e9744 --- /dev/null +++ b/corefx/src/main/java/com/core/ui/ChartDialog.java @@ -0,0 +1,252 @@ +package com.core.ui; + +import com.core.Controller; +import com.jfoenix.controls.JFXButton; +import com.jfoenix.controls.JFXComboBox; +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.chart.*; +import javafx.scene.layout.Pane; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; + +public class ChartDialog extends StageDialog { + private static final Logger logger = LogManager.getLogger(); + private final AtomicBoolean running = new AtomicBoolean(false); + private final Random numbers = new Random(); + private final List chartNames = Arrays.asList("Name 1", "Name 2", "Name 3", "Name 4", "Name 5"); + + @FXML + private JFXComboBox chartCombo; + + @FXML + private Pane chartPane; + + @FXML + private JFXButton stopButton; + + public ChartDialog(Controller controller) { + super(controller, "/fxml/chart_dialog.fxml"); + addCancelButton(); + + chartCombo.getItems().addAll("pie", "line", "area", "bar", "scatter", "bubble"); + chartCombo.getSelectionModel().selectedItemProperty().addListener((ov, prev, curr) -> { + if (curr == null) { + return; + } + + running.set(false); + switch (curr) { + case "pie": + pieChart(); + break; + case "line": + lineChart(); + break; + case "area": + areaChart(); + break; + case "bar": + barChart(); + break; + case "scatter": + scatterChart(); + break; + case "bubble": + bubbleChart(); + break; + } + }); + + stopButton.setOnAction(event -> running.set(false)); + chartCombo.getSelectionModel().selectFirst(); + } + + private void bubbleChart() { + NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1); + NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); + BubbleChart chart = new BubbleChart<>(xAxis, yAxis); + XYChart.Series series = new XYChart.Series<>(); + series.setName("Bubble Series Data"); + chart.getData().add(series); + chart.setTitle("Bubble Chart"); + chart.prefHeightProperty().bind(chartPane.heightProperty()); + chart.prefWidthProperty().bind(chartPane.widthProperty()); + chartPane.getChildren().clear(); + chartPane.getChildren().add(chart); + running.set(true); + new Thread(() -> { + while (running.get()) { + try { + Integer x = numbers.nextInt(100); + Integer y = numbers.nextInt(100); + Integer weight = numbers.nextInt(10); + Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y, weight))); + Thread.sleep(1000); + } catch (Exception ex) { + logger.error("error adding data", ex); + } + } + }).start(); + } + + private void scatterChart() { + NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1); + NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); + ScatterChart chart = new ScatterChart<>(xAxis, yAxis); + XYChart.Series series = new XYChart.Series<>(); + series.setName("Scatter Series Data"); + chart.getData().add(series); + chart.setTitle("Scatter Chart"); + chart.prefHeightProperty().bind(chartPane.heightProperty()); + chart.prefWidthProperty().bind(chartPane.widthProperty()); + chartPane.getChildren().clear(); + chartPane.getChildren().add(chart); + running.set(true); + new Thread(() -> { + while (running.get()) { + try { + Integer x = numbers.nextInt(100); + Integer y = numbers.nextInt(100); + Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y))); + Thread.sleep(1000); + } catch (Exception ex) { + logger.error("error adding data", ex); + } + } + }).start(); + } + + private void areaChart() { + NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1); + NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); + AreaChart chart = new AreaChart<>(xAxis, yAxis); + XYChart.Series series = new XYChart.Series<>(); + series.setName("Area Series Data"); + chart.getData().add(series); + chart.setTitle("Area Chart"); + chart.prefHeightProperty().bind(chartPane.heightProperty()); + chart.prefWidthProperty().bind(chartPane.widthProperty()); + chartPane.getChildren().clear(); + chartPane.getChildren().add(chart); + running.set(true); + new Thread(() -> { + while (running.get()) { + try { + Integer x = numbers.nextInt(100); + Integer y = numbers.nextInt(100); + Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y))); + Thread.sleep(1000); + } catch (Exception ex) { + logger.error("error adding data", ex); + } + } + }).start(); + } + + private void lineChart() { + NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1); + NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); + LineChart chart = new LineChart<>(xAxis, yAxis); + XYChart.Series series = new XYChart.Series<>(); + series.setName("Line Series Data"); + chart.getData().add(series); + chart.setTitle("Line Chart"); + chart.prefHeightProperty().bind(chartPane.heightProperty()); + chart.prefWidthProperty().bind(chartPane.widthProperty()); + chartPane.getChildren().clear(); + chartPane.getChildren().add(chart); + running.set(true); + new Thread(() -> { + while (running.get()) { + try { + Integer x = numbers.nextInt(100); + Integer y = numbers.nextInt(100); + Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y))); + Thread.sleep(1000); + } catch (Exception ex) { + logger.error("error adding data", ex); + } + } + }).start(); + } + + private void pieChart() { + PieChart chart = new PieChart(); + chart.setTitle("Pie Chart"); + chart.prefHeightProperty().bind(chartPane.heightProperty()); + chart.prefWidthProperty().bind(chartPane.widthProperty()); + chartPane.getChildren().clear(); + chartPane.getChildren().add(chart); + running.set(true); + Map pieMap = new HashMap<>(); + new Thread(() -> { + while (running.get()) { + try { + String name = chartNames.get(numbers.nextInt(chartNames.size())); + Integer y = numbers.nextInt(100); + Platform.runLater(() -> { + PieChart.Data data = pieMap.get(name); + if (data != null) { + data.setPieValue(y); + } else { + data = new PieChart.Data(name, y); + chart.getData().add(data); + pieMap.put(name, data); + } + }); + Thread.sleep(1000); + } catch (Exception ex) { + logger.error("error adding data", ex); + } + } + }).start(); + } + + private void barChart() { + CategoryAxis xAxis = new CategoryAxis(); + xAxis.setLabel("X Axis"); + xAxis.getCategories().add("My Ctageory"); + NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); + BarChart chart = new BarChart<>(xAxis, yAxis); + XYChart.Series series = new XYChart.Series<>(); + series.setName("Bar Chart Series"); + chart.getData().add(series); + chart.setTitle("Bar Chart"); + chart.prefHeightProperty().bind(chartPane.heightProperty()); + chart.prefWidthProperty().bind(chartPane.widthProperty()); + chartPane.getChildren().clear(); + chartPane.getChildren().add(chart); + running.set(true); + Map> barMap = new HashMap<>(); + new Thread(() -> { + while (running.get()) { + try { + String name = chartNames.get(numbers.nextInt(chartNames.size())); + Integer y = numbers.nextInt(100); + Platform.runLater(() -> { + XYChart.Data data = barMap.get(name); + if (data != null) { + data.setYValue(y); + } else { + data = new XYChart.Data<>(name, y); + series.getData().add(data); + barMap.put(name, data); + } + }); + Thread.sleep(1000); + } catch (Exception ex) { + logger.error("error adding data", ex); + } + } + }).start(); + } + + + public void showDialog() { + chartCombo.getSelectionModel().selectFirst(); + } +} diff --git a/corefx/src/main/resources/fxml/chart_dialog.fxml b/corefx/src/main/resources/fxml/chart_dialog.fxml new file mode 100644 index 00000000..1f76bfe2 --- /dev/null +++ b/corefx/src/main/resources/fxml/chart_dialog.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/corefx/src/main/resources/fxml/main.fxml b/corefx/src/main/resources/fxml/main.fxml index 71c93660..392b8f4a 100644 --- a/corefx/src/main/resources/fxml/main.fxml +++ b/corefx/src/main/resources/fxml/main.fxml @@ -37,6 +37,11 @@ + + + + +