updates to help defined classes to support displaying graph data when fleshed out

This commit is contained in:
Blake J. Harnden 2018-09-18 09:39:01 -07:00
parent 106d993b9d
commit c25003c693
6 changed files with 286 additions and 105 deletions

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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<String, PieChart.Data> pieData = new HashMap<>();
private BarChart<String, Number> barChart;
private final Map<String, XYChart.Data<String, Number>> barMap = new HashMap<>();
private XYChart<Number, Number> xyChart;
private final XYChart.Series<Number, Number> series = new XYChart.Series<>();
private final XYChart.Series<String, Number> 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<String, Number> data = barMap.computeIfAbsent(name, x -> {
XYChart.Data<String, Number> 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()));
}
}
}

View file

@ -0,0 +1,11 @@
package com.core.client.graph;
public enum GraphType {
PIE,
LINE,
TIME,
AREA,
BAR,
SCATTER,
BUBBLE
}

View file

@ -1,16 +1,19 @@
package com.core.ui; package com.core.ui;
import com.core.Controller; import com.core.Controller;
import com.core.client.graph.*;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox; import com.jfoenix.controls.JFXComboBox;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.chart.*; import javafx.scene.chart.Chart;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; 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; import java.util.concurrent.atomic.AtomicBoolean;
public class ChartDialog extends StageDialog { public class ChartDialog extends StageDialog {
@ -28,11 +31,18 @@ public class ChartDialog extends StageDialog {
@FXML @FXML
private JFXButton stopButton; private JFXButton stopButton;
private CoreGraph coreGraph;
public ChartDialog(Controller controller) { public ChartDialog(Controller controller) {
super(controller, "/fxml/chart_dialog.fxml"); super(controller, "/fxml/chart_dialog.fxml");
addCancelButton(); 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) -> { chartCombo.getSelectionModel().selectedItemProperty().addListener((ov, prev, curr) -> {
if (curr == null) { if (curr == null) {
return; return;
@ -58,6 +68,9 @@ public class ChartDialog extends StageDialog {
case "bubble": case "bubble":
bubbleChart(); bubbleChart();
break; break;
case "time":
timeChart();
break;
} }
}); });
@ -65,26 +78,36 @@ public class ChartDialog extends StageDialog {
chartCombo.getSelectionModel().selectFirst(); chartCombo.getSelectionModel().selectFirst();
} }
private void bubbleChart() { private void timeChart() {
NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1); coreGraph.setGraphType(GraphType.TIME);
NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph);
BubbleChart<Number, Number> chart = new BubbleChart<>(xAxis, yAxis); setChart(graphWrapper.getChart());
XYChart.Series<Number, Number> 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(() -> { new Thread(() -> {
while (running.get()) { while (running.get()) {
try { try {
Integer x = numbers.nextInt(100); double y = numbers.nextInt(100);
Integer y = numbers.nextInt(100); Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, null, y, null)));
Integer weight = numbers.nextInt(10); Thread.sleep(1000);
Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y, weight))); } 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); Thread.sleep(1000);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error adding data", ex); logger.error("error adding data", ex);
@ -94,24 +117,16 @@ public class ChartDialog extends StageDialog {
} }
private void scatterChart() { private void scatterChart() {
NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1); coreGraph.setGraphType(GraphType.SCATTER);
NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph);
ScatterChart<Number, Number> chart = new ScatterChart<>(xAxis, yAxis); setChart(graphWrapper.getChart());
XYChart.Series<Number, Number> 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(() -> { new Thread(() -> {
while (running.get()) { while (running.get()) {
try { try {
Integer x = numbers.nextInt(100); double x = numbers.nextInt(100);
Integer y = numbers.nextInt(100); double y = numbers.nextInt(100);
Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y))); Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, x, y, null)));
Thread.sleep(1000); Thread.sleep(1000);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error adding data", ex); logger.error("error adding data", ex);
@ -121,24 +136,16 @@ public class ChartDialog extends StageDialog {
} }
private void areaChart() { private void areaChart() {
NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1); coreGraph.setGraphType(GraphType.AREA);
NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1); CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph);
AreaChart<Number, Number> chart = new AreaChart<>(xAxis, yAxis); setChart(graphWrapper.getChart());
XYChart.Series<Number, Number> 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(() -> { new Thread(() -> {
while (running.get()) { while (running.get()) {
try { try {
Integer x = numbers.nextInt(100); double x = numbers.nextInt(100);
Integer y = numbers.nextInt(100); double y = numbers.nextInt(100);
Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y))); Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, x, y, null)));
Thread.sleep(1000); Thread.sleep(1000);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error adding data", ex); logger.error("error adding data", ex);
@ -147,25 +154,25 @@ public class ChartDialog extends StageDialog {
}).start(); }).start();
} }
private void lineChart() { private void setChart(Chart chart) {
NumberAxis xAxis = new NumberAxis("X Axis", 0, 100, 1);
NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1);
LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);
XYChart.Series<Number, Number> series = new XYChart.Series<>();
series.setName("Line Series Data");
chart.getData().add(series);
chart.setTitle("Line Chart");
chart.prefHeightProperty().bind(chartPane.heightProperty()); chart.prefHeightProperty().bind(chartPane.heightProperty());
chart.prefWidthProperty().bind(chartPane.widthProperty()); chart.prefWidthProperty().bind(chartPane.widthProperty());
chartPane.getChildren().clear(); chartPane.getChildren().clear();
chartPane.getChildren().add(chart); chartPane.getChildren().add(chart);
running.set(true); running.set(true);
}
private void lineChart() {
coreGraph.setGraphType(GraphType.LINE);
CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph);
setChart(graphWrapper.getChart());
new Thread(() -> { new Thread(() -> {
while (running.get()) { while (running.get()) {
try { try {
Integer x = numbers.nextInt(100); double x = numbers.nextInt(100);
Integer y = numbers.nextInt(100); double y = numbers.nextInt(100);
Platform.runLater(() -> series.getData().add(new XYChart.Data<>(x, y))); Platform.runLater(() -> graphWrapper.add(new CoreGraphData(null, x, y, null)));
Thread.sleep(1000); Thread.sleep(1000);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error adding data", ex); logger.error("error adding data", ex);
@ -175,29 +182,15 @@ public class ChartDialog extends StageDialog {
} }
private void pieChart() { private void pieChart() {
PieChart chart = new PieChart(); coreGraph.setGraphType(GraphType.PIE);
chart.setTitle("Pie Chart"); CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph);
chart.prefHeightProperty().bind(chartPane.heightProperty()); setChart(graphWrapper.getChart());
chart.prefWidthProperty().bind(chartPane.widthProperty());
chartPane.getChildren().clear();
chartPane.getChildren().add(chart);
running.set(true);
Map<String, PieChart.Data> pieMap = new HashMap<>();
new Thread(() -> { new Thread(() -> {
while (running.get()) { while (running.get()) {
try { try {
String name = chartNames.get(numbers.nextInt(chartNames.size())); String name = chartNames.get(numbers.nextInt(chartNames.size()));
Integer y = numbers.nextInt(100); double y = numbers.nextInt(100);
Platform.runLater(() -> { Platform.runLater(() -> graphWrapper.add(new CoreGraphData(name, null, y, null)));
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); Thread.sleep(1000);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error adding data", ex); logger.error("error adding data", ex);
@ -207,36 +200,15 @@ public class ChartDialog extends StageDialog {
} }
private void barChart() { private void barChart() {
CategoryAxis xAxis = new CategoryAxis(); coreGraph.setGraphType(GraphType.BAR);
xAxis.setLabel("X Axis"); CoreGraphWrapper graphWrapper = new CoreGraphWrapper(coreGraph);
xAxis.getCategories().add("My Ctageory"); setChart(graphWrapper.getChart());
NumberAxis yAxis = new NumberAxis("Y Axis", 0, 100, 1);
BarChart<String, Number> chart = new BarChart<>(xAxis, yAxis);
XYChart.Series<String, Number> 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<String, XYChart.Data<String, Number>> barMap = new HashMap<>();
new Thread(() -> { new Thread(() -> {
while (running.get()) { while (running.get()) {
try { try {
String name = chartNames.get(numbers.nextInt(chartNames.size())); String name = chartNames.get(numbers.nextInt(chartNames.size()));
Integer y = numbers.nextInt(100); Integer y = numbers.nextInt(100);
Platform.runLater(() -> { Platform.runLater(() -> graphWrapper.add(name, y));
XYChart.Data<String, Number> 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); Thread.sleep(1000);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error adding data", ex); logger.error("error adding data", ex);