Chart logic fixed and improved

This commit is contained in:
Niklas Aumueller 2026-03-05 12:40:18 +01:00
parent 9ccdd15adb
commit c90a4029e8
8 changed files with 74 additions and 25 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ target/
*.iws
*.iml
*.ipr
src/main/resources/scripts/.venv/
lib/
logs/
data/

View File

@ -4,6 +4,7 @@ import vassistent.model.AppState;
import vassistent.model.RatioPoint;
import vassistent.service.StatisticsService;
import vassistent.ui.DashboardView;
import vassistent.util.Logger;
import javax.swing.*;
import java.util.List;
@ -28,17 +29,19 @@ public class DashboardController {
private void onStateChanged(AppState state) {
List<RatioPoint> points =
statisticsService.getLastNAverages(20);
if (points.isEmpty())
return;
double ratio = points.get(points.size() - 1).getRatio();
statisticsService.getLastNAverages(120);
SwingUtilities.invokeLater(() -> {
Logger.info("STATISTICS",
"Entries loaded: " + points.size());
dashboardView.updateChart(points);
dashboardView.updateProblemLevel(ratio);
if (!points.isEmpty()) {
double ratio = points.get(points.size() - 1).getRatio();
dashboardView.updateProblemLevel(ratio);
}
});
}

View File

@ -18,7 +18,7 @@ public class AppState {
}
private void notifyListeners() {
listeners.forEach(l -> l.accept(this));
new ArrayList<>(listeners).forEach(l -> l.accept(this));
}
public ProblemLevel getProblemLevel() {

View File

@ -38,7 +38,7 @@ public class BinaryEventService {
if (lastId != null && lastId == id) {
Logger.warn("EVENT", "Payload ID bereits verarbeitet: " + id);
return;
//return;
}
lastId = id;

View File

@ -41,29 +41,37 @@ public class StatisticsService {
return 0.0;
}
public List<RatioPoint> getLastNAverages(int lastN) {
public List<RatioPoint> getLastNAverages(int windowSize) {
List<DatabaseEntry> entries = persistenceService.getLastEntries(30);
List<DatabaseEntry> entries = persistenceService.getLastEntries(windowSize + 1);
List<RatioPoint> result = new ArrayList<>();
if (entries.isEmpty())
return result;
for (int i = 10; i < entries.size(); i++) {
double sum = 0;
for (int i = 0; i < entries.size(); i++) {
int start = Math.max(0, i - windowSize);
for (int j = i - 10; j < i; j++) {
double sum = 0;
int count = 0;
for (int j = start; j < i; j++) {
sum += entries.get(j).getValue();
count++;
}
double avg = sum / 10.0;
double avg = count == 0 ? 0 : sum / count;
LocalDateTime timestamp = entries.get(i).getTimestamp();
result.add(new RatioPoint(timestamp, avg));
result.add(new RatioPoint(
entries.get(i).getTimestamp(),
avg
));
}
Logger.info("STATISTICS",
"Computed averages: " + result.size());
return result;
}
}

View File

@ -3,10 +3,13 @@ package vassistent.ui;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.ui.Layer;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.category.DefaultCategoryDataset;
@ -75,6 +78,24 @@ public class DashboardView extends JPanel {
XYPlot plot = chart.getXYPlot();
long tenMinutes = 10 * 60 * 1000L;
DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
domainAxis.setFixedAutoRange(tenMinutes);
domainAxis.setAutoRange(true);
XYLineAndShapeRenderer renderer =
(XYLineAndShapeRenderer) plot.getRenderer();
renderer.setDefaultShapesVisible(false);
renderer.setSeriesStroke(0,
new BasicStroke(
2.5f,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND
));
// Threshold Lines
addThresholdLine(plot, 0.5, "Warning");
addThresholdLine(plot, 0.8, "High");
@ -85,6 +106,7 @@ public class DashboardView extends JPanel {
rangeAxis.setTickUnit(new NumberTickUnit(0.1));
chart.setAntiAlias(true);
chart.setTextAntiAlias(true);
chart.getPlot().setBackgroundPaint(new Color(245,245,245));
chart.getPlot().setOutlineVisible(false);
@ -121,6 +143,9 @@ public class DashboardView extends JPanel {
}
public void updateChart(List<RatioPoint> points) {
if (points == null || points.isEmpty())
return;
series.clear();
for (RatioPoint point : points) {
@ -154,13 +179,25 @@ public class DashboardView extends JPanel {
ValueMarker marker = new ValueMarker(value);
marker.setPaint(Color.RED);
marker.setStroke(new BasicStroke(1.5f));
marker.setPaint(new Color(150,150,150,150));
float[] dash = {6.0f, 6.0f};
marker.setStroke(new BasicStroke(
1f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,
dash,
0.0f
));
marker.setLabel(label);
marker.setLabelFont(new Font("Segoe UI", Font.PLAIN, 11));
marker.setLabelAnchor(RectangleAnchor.LEFT);
marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
marker.setLabelPaint(new Color(160,160,160));
plot.addRangeMarker(marker);
marker.setLabelAnchor(RectangleAnchor.RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
plot.addRangeMarker(marker, Layer.BACKGROUND);
}
}

View File

@ -7,13 +7,13 @@ import time
import logging
# ===== KONFIGURATION =====
BROKER = "127.0.0.1"
BROKER = "141.75.223.13"
PORT = 1883
TOPIC = "PREDICTION"
USERNAME = None
PASSWORD = None
QOS = 0
INTERVAL_SECONDS = 10
INTERVAL_SECONDS = 5
# ==========================
# Logging konfigurieren (Console + Datei)