I am in charge of creating a GUI in Java to monitor heart rates of multiple people over regions (modified for confidentiality reasons).
There are a total of 8 regions, and each region contains 8 people. These are indicated by a JTabbedPane, 1 tab for each region. I'm attempting to draw and create 8 real-time graphs using the XChart library. The data will be read from a text file and be passed as two double arrays. One of the arrays being time in milliseconds (x-axis) and the second array being the heart rate data for 1 person (y-axis). Each person will have their own individual graph within their corresponding regional tab.
This is similar to watching several graphs to monitor vitals in the hospital to get an overview of what this application is supposed to simulate.
I am currently using Model-View-Controller architecture.
TLDR; To break it down, I'm only attempting to add multiple real-time XYChart within a tab within a JPanel inside a JTabbedPane. But for the time being, just adding ONE real-time chart would be great.
View.java
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
tabRegion1 = new javax.swing.JPanel();
jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
tabRegion1.setLayout(new java.awt.GridLayout(4, 2));
// Populate the JPanel with empty static graphs
for(int i = 0; i < Global.MAX_GRAPHS; i++)
tabRegion1.add(Graph.getRandomStaticGraph());
jTabbedPane1.addTab("Region 1", tabRegion1);
}
Graph.java (Models)
// Testing purposes
public static JPanel getRandomStaticGraph() {
XYChart chart = getEmptyXYChart();
XYSeries series = chart.addSeries("HeartRate", null, getRandomWalk(200));
series.setMarker(SeriesMarkers.NONE);
return new XChartPanel(chart);
}
// Testing purposes
public static JPanel getRandomRealTimeGraph() throws InterruptedException {
XYChart chart = getEmptyXYChart();
// Show it
final SwingWrapper<XYChart> sw = new SwingWrapper<XYChart>(chart);
sw.displayChart();
double phase = 0;
double[][] initdata = getSineData(phase);
XYSeries series = chart.addSeries("HeartRate", null, getRandomWalk(200));
while(true) {
phase += 2 * Math.PI * 2 / 20.0;
Thread.sleep(100);
final double[][] data = getSineData(phase);
chart.updateXYSeries("sine", data[0], data[1], null);
sw.repaintChart();
}
}
// To be used to generate random static graphs (testing purposes)
private static double[] getRandomWalk(int numPoints) {
double[] y = new double[numPoints];
y[0] = 0;
for(int i = 1; i < y.length; i++)
y[i] = y[i-1] + Math.random() - .5;
return y;
}
// To be used to generate real time graphs (testing purposes)
private static double[][] getSineData(double phase) {
double[] xData = new double[100];
double[] yData = new double[100];
for(int i = 0; i < xData.length; i++) {
double radians = phase + (2 * Math.PI / xData.length * i);
xData[i] = radians;
yData[i] = Math.sin(radians);
}
return new double[][] {xData, yData};
}
Resources and Where I've Looked
http://knowm.org/how-to-make-real-time-charts-in-java/ : This site has helped me create a base class for a real-time graph, but no way to help integrate this within a JPanel or how to make it a "real-time" through repaint() or update(). Not that using repaint() or update() is necessary. I'm open to pretty much any suggestions.
Current Environment
- NetBeans IDE 8.2 (for the GUI generator)
- Java 1.8.0_111
- XChart JAR (http://knowm.org/open-source/xchart/)
Additional Notes
I'm just not able to call getRandomRealTimeGraph() to return a JPanel or even an XChartPanel so I can add that to the JTabbedPane. I would love any suggestions that would be helpful!
If this helps, the data already exists and is being parsed into 2 arrays of doubles. But for now, I don't really care what's being taken in as the x-data and the y-data. As long as I can substitute it out later, I'm happy with anything.
Thank you so much!