diff --git a/corefx/src/main/java/com/core/client/graph/CoreGraph.java b/corefx/src/main/java/com/core/client/graph/CoreGraph.java new file mode 100644 index 00000000..8b896e46 --- /dev/null +++ b/corefx/src/main/java/com/core/client/graph/CoreGraph.java @@ -0,0 +1,11 @@ +package com.core.client.graph; + +import lombok.Data; + +@Data +public class CoreGraph { + private String title; + private CoreGraphAxis xAxis; + private CoreGraphAxis yAxis; + private GraphType graphType; +} diff --git a/corefx/src/main/java/com/core/client/graph/CoreGraphAxis.java b/corefx/src/main/java/com/core/client/graph/CoreGraphAxis.java new file mode 100644 index 00000000..1ca7c49a --- /dev/null +++ b/corefx/src/main/java/com/core/client/graph/CoreGraphAxis.java @@ -0,0 +1,15 @@ +package com.core.client.graph; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CoreGraphAxis { + private String label; + private Double lower; + private Double upper; + private Double tick; +} diff --git a/corefx/src/main/java/com/core/client/graph/CoreGraphData.java b/corefx/src/main/java/com/core/client/graph/CoreGraphData.java new file mode 100644 index 00000000..e6444c12 --- /dev/null +++ b/corefx/src/main/java/com/core/client/graph/CoreGraphData.java @@ -0,0 +1,15 @@ +package com.core.client.graph; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CoreGraphData { + private String name; + private Double x; + private Double y; + private Double weight; +} diff --git a/corefx/src/main/java/com/core/client/graph/CoreGraphWrapper.java b/corefx/src/main/java/com/core/client/graph/CoreGraphWrapper.java new file mode 100644 index 00000000..e0e9c565 --- /dev/null +++ b/corefx/src/main/java/com/core/client/graph/CoreGraphWrapper.java @@ -0,0 +1,157 @@ +package com.core.client.graph; + +import javafx.scene.chart.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +public class CoreGraphWrapper { + private static final Logger logger = LogManager.getLogger(); + private final GraphType graphType; + private PieChart pieChart; + private final Map pieData = new HashMap<>(); + private BarChart barChart; + private final Map> barMap = new HashMap<>(); + private XYChart xyChart; + private final XYChart.Series series = new XYChart.Series<>(); + private final XYChart.Series barSeries = new XYChart.Series<>(); + private AtomicInteger timeValue = new AtomicInteger(0); + + public CoreGraphWrapper(CoreGraph coreGraph) { + graphType = coreGraph.getGraphType(); + createChart(coreGraph); + } + + public Chart getChart() { + switch (graphType) { + case PIE: + return pieChart; + case BAR: + return barChart; + default: + return xyChart; + } + } + + public void add(CoreGraphData coreGraphData) { + switch (graphType) { + case PIE: + case BAR: + add(coreGraphData.getName(), coreGraphData.getY()); + break; + case TIME: + add(coreGraphData.getY()); + break; + case BUBBLE: + add(coreGraphData.getX(), coreGraphData.getY(), coreGraphData.getWeight()); + break; + default: + add(coreGraphData.getX(), coreGraphData.getY()); + } + } + + public void add(String name, double value) { + if (GraphType.PIE == graphType) { + PieChart.Data data = pieData.computeIfAbsent(name, x -> { + PieChart.Data newData = new PieChart.Data(x, value); + pieChart.getData().add(newData); + return newData; + }); + data.setPieValue(value); + } else { + XYChart.Data data = barMap.computeIfAbsent(name, x -> { + XYChart.Data newData = new XYChart.Data<>(name, value); + barSeries.getData().add(newData); + return newData; + }); + data.setYValue(value); + } + } + + public void add(Number y) { + series.getData().add(new XYChart.Data<>(timeValue.getAndIncrement(), y)); + } + + public void add(Number x, Number y) { + series.getData().add(new XYChart.Data<>(x, y)); + } + + public void add(Number x, Number y, Number weight) { + series.getData().add(new XYChart.Data<>(x, y, weight)); + } + + private NumberAxis getAxis(CoreGraphAxis graphAxis) { + return new NumberAxis(graphAxis.getLabel(), graphAxis.getLower(), + graphAxis.getUpper(), graphAxis.getTick()); + } + + private void createChart(CoreGraph coreGraph) { + NumberAxis xAxis; + NumberAxis yAxis; + + switch (coreGraph.getGraphType()) { + case AREA: + xAxis = getAxis(coreGraph.getXAxis()); + yAxis = getAxis(coreGraph.getYAxis()); + xyChart = new AreaChart<>(xAxis, yAxis); + xyChart.setTitle(coreGraph.getTitle()); + xyChart.setLegendVisible(false); + xyChart.getData().add(series); + break; + case TIME: + xAxis = new NumberAxis(); + xAxis.setLabel(coreGraph.getXAxis().getLabel()); + xAxis.setTickUnit(1); + xAxis.setLowerBound(0); + yAxis = getAxis(coreGraph.getYAxis()); + xyChart = new LineChart<>(xAxis, yAxis); + xyChart.setTitle(coreGraph.getTitle()); + xyChart.setLegendVisible(false); + xyChart.getData().add(series); + break; + case LINE: + xAxis = getAxis(coreGraph.getXAxis()); + yAxis = getAxis(coreGraph.getYAxis()); + xyChart = new LineChart<>(xAxis, yAxis); + xyChart.setTitle(coreGraph.getTitle()); + xyChart.setLegendVisible(false); + xyChart.getData().add(series); + break; + case BUBBLE: + xAxis = getAxis(coreGraph.getXAxis()); + yAxis = getAxis(coreGraph.getYAxis()); + xyChart = new BubbleChart<>(xAxis, yAxis); + xyChart.setTitle(coreGraph.getTitle()); + xyChart.setLegendVisible(false); + xyChart.getData().add(series); + break; + case SCATTER: + xAxis = getAxis(coreGraph.getXAxis()); + yAxis = getAxis(coreGraph.getYAxis()); + xyChart = new ScatterChart<>(xAxis, yAxis); + xyChart.setTitle(coreGraph.getTitle()); + xyChart.setLegendVisible(false); + xyChart.getData().add(series); + break; + case PIE: + pieChart = new PieChart(); + pieChart.setTitle(coreGraph.getTitle()); + break; + case BAR: + CategoryAxis categoryAxis = new CategoryAxis(); + categoryAxis.setLabel(coreGraph.getXAxis().getLabel()); + yAxis = getAxis(coreGraph.getYAxis()); + barChart = new BarChart<>(categoryAxis, yAxis); + barChart.setLegendVisible(false); + barChart.setTitle(coreGraph.getTitle()); + barChart.getData().add(barSeries); + break; + default: + throw new IllegalArgumentException(String.format("unknown graph type: %s", + coreGraph.getGraphType())); + } + } +} diff --git a/corefx/src/main/java/com/core/client/graph/GraphType.java b/corefx/src/main/java/com/core/client/graph/GraphType.java new file mode 100644 index 00000000..8daba5ef --- /dev/null +++ b/corefx/src/main/java/com/core/client/graph/GraphType.java @@ -0,0 +1,11 @@ +package com.core.client.graph; + +public enum GraphType { + PIE, + LINE, + TIME, + AREA, + BAR, + SCATTER, + BUBBLE +} diff --git a/corefx/src/main/java/com/core/ui/ChartDialog.java b/corefx/src/main/java/com/core/ui/ChartDialog.java index 9a8e9744..eb5fa36e 100644 --- a/corefx/src/main/java/com/core/ui/ChartDialog.java +++ b/corefx/src/main/java/com/core/ui/ChartDialog.java @@ -1,16 +1,19 @@ package com.core.ui; import com.core.Controller; +import com.core.client.graph.*; 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.chart.Chart; import javafx.scene.layout.Pane; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.*; +import java.util.Arrays; +import java.util.List; +import java.util.Random; import java.util.concurrent.atomic.AtomicBoolean; public class ChartDialog extends StageDialog { @@ -28,11 +31,18 @@ public class ChartDialog extends StageDialog { @FXML private JFXButton stopButton; + private CoreGraph coreGraph; + public ChartDialog(Controller controller) { super(controller, "/fxml/chart_dialog.fxml"); addCancelButton(); - chartCombo.getItems().addAll("pie", "line", "area", "bar", "scatter", "bubble"); + coreGraph = new CoreGraph(); + coreGraph.setTitle("My Graph"); + coreGraph.setXAxis(new CoreGraphAxis("X Label", 0.0, 100.0, 1.0)); + coreGraph.setYAxis(new CoreGraphAxis("Y Label", 0.0, 100.0, 1.0)); + + chartCombo.getItems().addAll("pie", "line", "area", "bar", "scatter", "bubble", "time"); chartCombo.getSelectionModel().selectedItemProperty().addListener((ov, prev, curr) -> { if (curr == null) { return; @@ -58,6 +68,9 @@ public class ChartDialog extends StageDialog { case "bubble": bubbleChart(); break; + case "time": + timeChart(); + break; } }); @@ -65,26 +78,36 @@ public class ChartDialog extends StageDialog { 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); + private void timeChart() { + coreGraph.setGraphType(GraphType.TIME); + CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph); + setChart(graphWrapper.getChart()); + 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))); + double y = numbers.nextInt(100); + Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, null, y, null))); + Thread.sleep(1000); + } catch (Exception ex) { + logger.error("error adding data", ex); + } + } + }).start(); + } + + private void bubbleChart() { + coreGraph.setGraphType(GraphType.BUBBLE); + CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph); + setChart(graphWrapper.getChart()); + + new Thread(() -> { + while (running.get()) { + try { + double x = numbers.nextInt(100); + double y = numbers.nextInt(100); + double weight = numbers.nextInt(10); + Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, x, y, weight))); Thread.sleep(1000); } catch (Exception ex) { logger.error("error adding data", ex); @@ -94,24 +117,16 @@ public class ChartDialog extends StageDialog { } 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); + coreGraph.setGraphType(GraphType.SCATTER); + CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph); + setChart(graphWrapper.getChart()); + 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))); + double x = numbers.nextInt(100); + double y = numbers.nextInt(100); + Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, x, y, null))); Thread.sleep(1000); } catch (Exception ex) { logger.error("error adding data", ex); @@ -121,24 +136,16 @@ public class ChartDialog extends StageDialog { } 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); + coreGraph.setGraphType(GraphType.AREA); + CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph); + setChart(graphWrapper.getChart()); + 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))); + double x = numbers.nextInt(100); + double y = numbers.nextInt(100); + Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, x, y, null))); Thread.sleep(1000); } catch (Exception ex) { logger.error("error adding data", ex); @@ -147,25 +154,25 @@ public class ChartDialog extends StageDialog { }).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"); + private void setChart(Chart chart) { chart.prefHeightProperty().bind(chartPane.heightProperty()); chart.prefWidthProperty().bind(chartPane.widthProperty()); chartPane.getChildren().clear(); chartPane.getChildren().add(chart); running.set(true); + } + + private void lineChart() { + coreGraph.setGraphType(GraphType.LINE); + CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph); + setChart(graphWrapper.getChart()); + 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))); + double x = numbers.nextInt(100); + double y = numbers.nextInt(100); + Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, x, y, null))); Thread.sleep(1000); } catch (Exception ex) { logger.error("error adding data", ex); @@ -175,29 +182,15 @@ public class ChartDialog extends StageDialog { } 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<>(); + coreGraph.setGraphType(GraphType.PIE); + CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph); + setChart(graphWrapper.getChart()); 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); - } - }); + double y = numbers.nextInt(100); + Platform.runLater(() -> graphWrapper.add(new CoreGraphData(name, null, y, null))); Thread.sleep(1000); } catch (Exception ex) { logger.error("error adding data", ex); @@ -207,36 +200,15 @@ public class ChartDialog extends StageDialog { } 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<>(); + coreGraph.setGraphType(GraphType.BAR); + CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph); + setChart(graphWrapper.getChart()); 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); - } - }); + Platform.runLater(() -> graphWrapper.add(name, y)); Thread.sleep(1000); } catch (Exception ex) { logger.error("error adding data", ex);