Heartrate Simulator with visuals added
This commit is contained in:
parent
411033e6b8
commit
1bf4594a93
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMLController.java to edit this template
|
||||||
|
*/
|
||||||
|
package efi.projekt.gesundheitsassistent.controller;
|
||||||
|
|
||||||
|
import efi.projekt.gesundheitsassistent.viewmodel.HeartRateViewModel;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.fxml.Initializable;
|
||||||
|
import javafx.scene.chart.LineChart;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FXML Controller class
|
||||||
|
*
|
||||||
|
* @author naumueller
|
||||||
|
*/
|
||||||
|
public class HeartRateController implements Initializable {
|
||||||
|
|
||||||
|
@FXML private LineChart<String, Number> heartRateChart;
|
||||||
|
|
||||||
|
private HeartRateViewModel viewModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the controller class.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void initialize(URL url, ResourceBundle rb) {
|
||||||
|
viewModel = new HeartRateViewModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void startSimulation() {
|
||||||
|
heartRateChart.getData().add(viewModel.getHeartRateSeries());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||||
|
*/
|
||||||
|
package efi.projekt.gesundheitsassistent.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author naumueller
|
||||||
|
*/
|
||||||
|
public class HeartRate {
|
||||||
|
private List<Integer> heartRateData = new ArrayList<>();
|
||||||
|
private int time = 0;
|
||||||
|
private Random random = new Random();
|
||||||
|
|
||||||
|
public HeartRate() {}
|
||||||
|
|
||||||
|
/** Neue Herzfrequenz simulieren */
|
||||||
|
public int simulateHeartRate() {
|
||||||
|
int base = 70;
|
||||||
|
int fluctuation = random.nextInt(30) - 10; // -10 bis +20
|
||||||
|
int bpm = base + fluctuation;
|
||||||
|
addData(bpm);
|
||||||
|
return bpm;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addData(int bpm) {
|
||||||
|
heartRateData.add(bpm);
|
||||||
|
time++;
|
||||||
|
if (heartRateData.size() > 60) {
|
||||||
|
heartRateData.remove(0); // nur letzte 60 Werte behalten
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> getHeartRateData() {
|
||||||
|
return heartRateData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTime() {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||||
|
*/
|
||||||
|
package efi.projekt.gesundheitsassistent.viewmodel;
|
||||||
|
|
||||||
|
import efi.projekt.gesundheitsassistent.model.HeartRate;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
import javafx.application.Platform;
|
||||||
|
import javafx.scene.chart.XYChart;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author naumueller
|
||||||
|
*/
|
||||||
|
public class HeartRateViewModel {
|
||||||
|
private HeartRate heartRate;
|
||||||
|
private XYChart.Series<String, Number> heartRateSeries = new XYChart.Series<>();
|
||||||
|
private Timer timer = new Timer(true);
|
||||||
|
|
||||||
|
public HeartRateViewModel() {
|
||||||
|
this.heartRate = new HeartRate();
|
||||||
|
heartRateSeries.setName("Herzfrequenz");
|
||||||
|
startSimulation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public XYChart.Series<String, Number> getHeartRateSeries() { return heartRateSeries; }
|
||||||
|
|
||||||
|
private void startSimulation() {
|
||||||
|
timer.scheduleAtFixedRate(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int bpm = heartRate.simulateHeartRate();
|
||||||
|
int time = heartRate.getTime();
|
||||||
|
Platform.runLater(() -> addHeartRateData(time, bpm));
|
||||||
|
}
|
||||||
|
}, 0, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addHeartRateData(int time, int bpm) {
|
||||||
|
heartRateSeries.getData().add(new XYChart.Data<>(String.valueOf(time),bpm));
|
||||||
|
if (heartRateSeries.getData().size() > 60) {
|
||||||
|
heartRateSeries.getData().remove(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.chart.CategoryAxis?>
|
||||||
|
<?import javafx.scene.chart.LineChart?>
|
||||||
|
<?import javafx.scene.chart.NumberAxis?>
|
||||||
|
<?import javafx.scene.control.Button?>
|
||||||
|
<?import javafx.scene.control.Label?>
|
||||||
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
|
||||||
|
|
||||||
|
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" spacing="10.0" fx:controller="efi.projekt.gesundheitsassistent.controller.HeartRateController" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
|
||||||
|
<children>
|
||||||
|
<Label text="Herzfrequenz Dashboard">
|
||||||
|
<VBox.margin>
|
||||||
|
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
|
||||||
|
</VBox.margin>
|
||||||
|
</Label>
|
||||||
|
<LineChart fx:id="heartRateChart">
|
||||||
|
<xAxis>
|
||||||
|
<CategoryAxis label="Zeit in s" side="BOTTOM" />
|
||||||
|
</xAxis>
|
||||||
|
<yAxis>
|
||||||
|
<NumberAxis label="BPM" side="LEFT" />
|
||||||
|
</yAxis>
|
||||||
|
</LineChart>
|
||||||
|
<Button fx:id="startBtn" mnemonicParsing="false" onMouseClicked="#startSimulation" text="Button" />
|
||||||
|
</children>
|
||||||
|
</VBox>
|
||||||
Loading…
x
Reference in New Issue
Block a user