forked from farrellf/Telemetry-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeDomainChart.java
143 lines (97 loc) · 3.94 KB
/
TimeDomainChart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
@SuppressWarnings("serial")
public class TimeDomainChart extends PositionedChart {
Image chartImage;
public static ChartDescriptor getDescriptor() {
return new ChartDescriptor() {
@Override public String toString() { return "Time Domain Chart"; }
@Override public int getMinimumDuration() { return 10; }
@Override public int getDefaultDuration() { return 1000; }
@Override public int getMaximumDuration() { return Integer.MAX_VALUE; }
@Override public String[] getInputNames() { return null; }
@Override public PositionedChart createChart(int x1, int y1, int x2, int y2, int chartDuration, Dataset[] chartInputs) {
return new TimeDomainChart(x1, y1, x2, y2, chartDuration, chartInputs);
}
};
}
@Override public String toString() {
return "Time Domain Chart";
}
public TimeDomainChart(int x1, int y1, int x2, int y2, int chartDuration, Dataset[] chartInputs) {
super(x1, y1, x2, y2, chartDuration, chartInputs);
// spawn a thread that draws the chart
Thread thread = new Thread(new Runnable() {
@Override public void run() {
while(true) {
long startTime = System.currentTimeMillis();
// draw the chart
int chartWidth = getWidth();
int chartHeight = getHeight();
if(chartWidth < 1) chartWidth = 1;
if(chartHeight < 1) chartHeight = 1;
String chartTitle = "";
String xAxisTitle = "Sample Number";
String yAxisTitle = datasets[0].unit;
BufferedImage image = getTimeDomainChart(chartTitle, xAxisTitle, yAxisTitle, chartWidth, chartHeight);
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
// free resources of old image
if(chartImage != null)
chartImage.flush();
// paint new image
chartImage = image;
repaint();
}
});
// end this thread if we are no longer visible (the user clicked the "Reset" button)
if(!isShowing())
break;
// wait if needed before drawing a new frame
long timeToNextFrame = startTime + Controller.getTargetFramePeriod() - System.currentTimeMillis();
if(timeToNextFrame <= 0)
continue;
else
try{ Thread.sleep(timeToNextFrame); } catch(Exception e) {}
}
}
});
String inputNames = "";
for(int i = 0; i < datasets.length; i++)
inputNames += datasets[i].name + ", ";
thread.setName(String.format("TimeDomainChart of: %s", inputNames));
thread.start();
}
private BufferedImage getTimeDomainChart(String chartTitle, String xAxisTitle, String yAxisTitle, int chartWidth, int chartHeight) {
XYSeriesCollection seriesCollection = new XYSeriesCollection();
JFreeChart lineGraph = ChartFactory.createXYLineChart(chartTitle, xAxisTitle, yAxisTitle, seriesCollection);
// calculate domain
int sampleCount = Controller.getSamplesCount(datasets);
int maxX = sampleCount - 1;
int minX = maxX - duration;
if(minX < 0) minX = 0;
for(int i = 0; i < datasets.length; i++) {
XYSeries series = new XYSeries(datasets[i].name);
for(int x = minX; x <= maxX; x++) {
double y = datasets[i].get(x);
series.add(x, y);
}
seriesCollection.addSeries(series);
lineGraph.getXYPlot().getRenderer().setSeriesPaint(i, datasets[i].color);
}
lineGraph.getXYPlot().getDomainAxis().setRange(minX, maxX + 2); // +2 to prevent a range of (0,0)
lineGraph.getTitle().setFont(Model.chartTitleFont);
return lineGraph.createBufferedImage(chartWidth, chartHeight);
}
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(chartImage != null)
g.drawImage(chartImage, 0, 0, null);
}
}