|
| 1 | +package xyz.elmot.clion.charttool; |
| 2 | + |
| 3 | +import javafx.application.Platform; |
| 4 | +import javafx.collections.FXCollections; |
| 5 | +import javafx.collections.ObservableList; |
| 6 | +import javafx.embed.swing.JFXPanel; |
| 7 | +import javafx.geometry.Insets; |
| 8 | +import javafx.scene.Scene; |
| 9 | +import javafx.scene.chart.LineChart; |
| 10 | +import javafx.scene.chart.NumberAxis; |
| 11 | +import javafx.scene.chart.XYChart; |
| 12 | +import javafx.scene.control.Button; |
| 13 | +import javafx.scene.layout.Priority; |
| 14 | +import javafx.scene.layout.VBox; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | +import xyz.elmot.clion.charttool.state.ChartExpr; |
| 17 | +import xyz.elmot.clion.charttool.state.ExpressionState; |
| 18 | + |
| 19 | +import java.util.*; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | +import java.util.concurrent.atomic.AtomicInteger; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.IntStream; |
| 24 | + |
| 25 | +public class ChartsPanel extends JFXPanel { |
| 26 | + private boolean initialized = false; |
| 27 | + public static final int MAX_SERIES = 50; |
| 28 | + |
| 29 | + private Button reset; |
| 30 | + private LineChart<Number, Number> lineChart; |
| 31 | + private Map<String, ChartExpressionData> seriesByName = new ConcurrentHashMap<>(); |
| 32 | + |
| 33 | + public ChartsPanel() { |
| 34 | + Platform.runLater(() -> { |
| 35 | + |
| 36 | + reset = new Button("Clear"); |
| 37 | + reset.setOnAction(e -> clear()); |
| 38 | + //defining the axes |
| 39 | + final NumberAxis xAxis = new NumberAxis(); |
| 40 | + final NumberAxis yAxis = new NumberAxis(); |
| 41 | + //creating the chart |
| 42 | + lineChart = new LineChart<>(xAxis, yAxis); |
| 43 | + lineChart.setCreateSymbols(false); |
| 44 | + |
| 45 | + VBox vBox = new VBox(10, lineChart, reset); |
| 46 | + vBox.setPadding(new Insets(10)); |
| 47 | + Scene scene = new Scene(vBox); |
| 48 | + |
| 49 | + lineChart.setAnimated(false); |
| 50 | + vBox.setFillWidth(true); |
| 51 | + VBox.setVgrow(lineChart, Priority.ALWAYS); |
| 52 | + lineChart.setScaleShape(true); |
| 53 | + setScene(scene); |
| 54 | + invalidate(); |
| 55 | + initialized = true; |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + public void clear() { |
| 60 | + seriesByName.clear(); |
| 61 | + Platform.runLater(lineChart.getData()::clear); |
| 62 | + } |
| 63 | + |
| 64 | + public void series(ChartExpr chartExpr, List<Number> numbers) { |
| 65 | + ChartExpressionData data = seriesByName |
| 66 | + .computeIfAbsent(chartExpr.getName(), a -> new ChartExpressionData()); |
| 67 | + String name; |
| 68 | + if (chartExpr.getState() == ExpressionState.ACCUMULATE) { |
| 69 | + int index = data.currentIndex.getAndUpdate(i -> (i + 1) % MAX_SERIES); |
| 70 | + if (data.data.size() <= index) { |
| 71 | + data.data.add(numbers); |
| 72 | + } else { |
| 73 | + data.data.set(index, numbers); |
| 74 | + } |
| 75 | + name = accChartName(chartExpr, index); |
| 76 | + } else { |
| 77 | + data.data.clear(); |
| 78 | + data.currentIndex.set(0); |
| 79 | + data.data.add(numbers); |
| 80 | + name = chartExpr.getName(); |
| 81 | + } |
| 82 | + |
| 83 | + if (initialized) { |
| 84 | + ObservableList<XYChart.Data<Number, Number>> lineData = calcLineData(chartExpr, numbers); |
| 85 | + Platform.runLater(() -> { |
| 86 | + ObservableList<XYChart.Series<Number, Number>> chartData = lineChart.getData(); |
| 87 | + Optional<XYChart.Series<Number, Number>> foundSeries = chartData |
| 88 | + .stream() |
| 89 | + .filter(series -> name.equals(series.getName())) |
| 90 | + .findFirst(); |
| 91 | + if (foundSeries.isPresent()) { |
| 92 | + foundSeries.get().setData(lineData); |
| 93 | + } else { |
| 94 | + chartData.add(new XYChart.Series<>(name, lineData)); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @NotNull |
| 101 | + protected String accChartName(ChartExpr chartExpr, int index) { |
| 102 | + return chartExpr.getName() + " #" + (index + 1); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + @NotNull |
| 107 | + protected ObservableList<XYChart.Data<Number, Number>> calcLineData(ChartExpr chartExpr, List<Number> numbers) { |
| 108 | + return FXCollections |
| 109 | + .observableArrayList(IntStream.range(0, numbers.size()).mapToObj( |
| 110 | + i -> { |
| 111 | + double x = chartExpr.getXBase() + chartExpr.getXScale() * i; |
| 112 | + double y = chartExpr.getYBase() + chartExpr.getYScale() * numbers.get(i).doubleValue(); |
| 113 | + return new XYChart.Data<>((Number) x, (Number) y); |
| 114 | + } |
| 115 | + ).collect(Collectors.toList())); |
| 116 | + } |
| 117 | + |
| 118 | + public void refreshData(Collection<ChartExpr> exprs) { |
| 119 | + if (!initialized) { |
| 120 | + return; |
| 121 | + } |
| 122 | + List<XYChart.Series<Number, Number>> chartData = new ArrayList<>(); |
| 123 | + for (ChartExpr expr : exprs) { |
| 124 | + String name = expr.getName(); |
| 125 | + ChartExpressionData chartExpressionData = seriesByName.get(name); |
| 126 | + if (chartExpressionData == null) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + if (expr.getState() == ExpressionState.ACCUMULATE) { |
| 130 | + for (int i = 0; i < chartExpressionData.data.size(); i++) { |
| 131 | + @NotNull ObservableList<XYChart.Data<Number, Number>> numberNumberSeries = |
| 132 | + calcLineData(expr, chartExpressionData.data.get(i)); |
| 133 | + chartData.add(new XYChart.Series<>(accChartName(expr, i), numberNumberSeries)); |
| 134 | + } |
| 135 | + } else if (!chartExpressionData.data.isEmpty()) { |
| 136 | + chartData.add(new XYChart.Series<>(name, calcLineData(expr, chartExpressionData.data.get(0)))); |
| 137 | + } |
| 138 | + } |
| 139 | + ObservableList<XYChart.Series<Number, Number>> observableChartData = FXCollections |
| 140 | + .observableArrayList(chartData); |
| 141 | + Platform.runLater(() -> lineChart.setData(observableChartData)); |
| 142 | + } |
| 143 | + |
| 144 | + public boolean isSampled(String name) { |
| 145 | + return seriesByName.containsKey(name); |
| 146 | + } |
| 147 | + |
| 148 | + private static class ChartExpressionData { |
| 149 | + private final List<List<Number>> data = new ArrayList<>(MAX_SERIES); |
| 150 | + private final AtomicInteger currentIndex = new AtomicInteger(); |
| 151 | + } |
| 152 | +} |
0 commit comments