Skip to content
Snippets Groups Projects
Commit 89c60967 authored by joalhelk's avatar joalhelk
Browse files

chart update

parent 9db3dc9e
No related branches found
No related tags found
No related merge requests found
......@@ -55,6 +55,8 @@ public class Node {
}
}
public List<NodeData> getData() { return data; }
// Gets the temperature reading of the most recent measurement
public Integer getRecentTemperature() {
NodeData data = findRecentData();
......
......@@ -7,36 +7,51 @@ import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import com.joelhelkala.watcherGui.Node.NodeData.NodeData;
import java.awt.BorderLayout;
import java.time.LocalDateTime;
import java.util.List;
public class LineChartPanel extends JPanel {
public class LineChartPanel extends JPanel {
private JFreeChart lineChart;
private ChartPanel chartPanel;
private CategoryPlot plot;
// Constructor
public LineChartPanel(String chartTitle) {
JFreeChart lineChart = ChartFactory.createLineChart(chartTitle, "DateTime","Temperature",
createDataset(),
public LineChartPanel(String chartTitle, List<NodeData> list) {
lineChart = ChartFactory.createLineChart(chartTitle, "DateTime","Temperature",
createDataset(list),
PlotOrientation.VERTICAL,
true,true,false);
ChartPanel chartPanel = new ChartPanel( lineChart );
chartPanel = new ChartPanel( lineChart );
chartPanel.setPreferredSize( new Dimension( 560 , 367 ) );
plot = (CategoryPlot) lineChart.getPlot();
this.setLayout(new BorderLayout());
this.add(chartPanel, BorderLayout.CENTER);
this.validate();
}
// Mockup dataset
private DefaultCategoryDataset createDataset( ) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
dataset.addValue( -17 , "temp" , LocalDateTime.now().minusDays(5) );
dataset.addValue( -15 , "temp" , LocalDateTime.now().minusDays(5).plusHours(2));
dataset.addValue( -16 , "temp" , LocalDateTime.now().minusDays(4).minusHours(2) );
dataset.addValue( -17 , "temp" , LocalDateTime.now().minusDays(1).minusHours(3) );
dataset.addValue( -10 , "temp" , LocalDateTime.now().minusDays(1) );
dataset.addValue( -3 , "temp" , LocalDateTime.now() );
return dataset;
}
// Create dataset from given nodedata list
private DefaultCategoryDataset createDataset(List<NodeData> list) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
list.sort((a,b) -> a.getTime().compareTo(b.getTime()));
for(NodeData data : list) {
dataset.addValue(data.getTemperature(), "temp", data.getTime());
}
return dataset;
}
// Update chart data
public void updateData(List<NodeData> list) {
plot.setDataset(createDataset(list));
}
}
......@@ -2,7 +2,6 @@ package com.joelhelkala.watcherGui.frames.subframes;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.joelhelkala.watcherGui.Node.Node;
......@@ -22,6 +21,7 @@ public class NodeDataFrame extends JPanel {
private TemperaturePanel tempCard;
private JPanel humidCard;
private LineChartPanel chartCard;
public NodeDataFrame(int x, int y, int width, int height) {
setLayout(null);
......@@ -49,23 +49,24 @@ public class NodeDataFrame extends JPanel {
card3.setBounds(cardWidth*2+45, 10, cardWidth, cardPanelHeight-20);
cardPanel.add(card3);
JPanel chartPanel = new JPanel();
chartPanel.setBackground(gray);
chartPanel.setBounds(0, cardPanelHeight, width, cardPanelHeight+10);
chartPanel.setLayout(null);
JPanel chartCardPanel = new JPanel();
chartCardPanel.setBackground(gray);
chartCardPanel.setBounds(0, cardPanelHeight, width, cardPanelHeight+10);
chartCardPanel.setLayout(null);
JPanel chart = new LineChartPanel("Temperature");
chart.setBackground(lightgray);
chart.setBounds(15, 10, width-30, cardPanelHeight - 20);
chartPanel.add(chart);
chartCard = new LineChartPanel("Temperature", node.getData());
chartCard.setBackground(lightgray);
chartCard.setBounds(15, 10, width-30, cardPanelHeight - 20);
chartCardPanel.add(chartCard);
add(cardPanel);
add(chartPanel);
add(chartCardPanel);
}
// Updates the frames data panels with given node information
public void updateData(Node node) {
tempCard.updateData(node);
chartCard.updateData(node.getData());
// TODO: perhaps change the humidity to normal label since the updating is hard
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment