-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathECGView.java
More file actions
318 lines (283 loc) · 7.9 KB
/
Copy pathECGView.java
File metadata and controls
318 lines (283 loc) · 7.9 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.DefaultXYDataset;
/**
* class ECGView - a class that handles rendering of the charts
*
* @author Dakota Williams
*/
public class ECGView {
private ChartPanel panel;
private JFreeChart chart;
private XYPlot plot;
private NumberAxis xaxis;
private NumberAxis yaxis;
private XYLineAndShapeRenderer renderer;
private DefaultXYDataset dataset;
private final int defaultWidth = 200;
private final int defaultHeight = 200;
private final ECGViewHandler handler;
private int index;
private ECGDataSet data;
private String title;
private final ECGView thisView = this;
private boolean canPlace = false;
private boolean canRemove = false;
private boolean selected = false;
private boolean labels;
/**
* Constructor - initializes the view
*
* @param data the dataset to display
* @param title the title of the chart
* @param withLabels whether labels on the chart should be shown
*/
public ECGView(ECGViewHandler handler,
ECGDataSet data,
int index,
String title,
boolean withLabels) {
this.handler = handler;
this.title = title;
this.index = index;
this.data = data;
this.labels = withLabels;
dataset = new DefaultXYDataset();
dataset.addSeries(1, data.toArray());
this.xaxis = new NumberAxis(handler.getxAxisLabel());
if(!withLabels) {
this.xaxis.setVisible(false);
}
this.yaxis = new NumberAxis(handler.getyAxisLabel());
if(!withLabels) {
this.yaxis.setVisible(false);
}
this.renderer = new XYLineAndShapeRenderer(true, false);
this.plot = new XYPlot(dataset, xaxis, yaxis, renderer);
if(withLabels) {
this.plot.setDomainPannable(true);
this.plot.setRangePannable(true);
}
this.chart = new JFreeChart(title, plot);
this.chart.removeLegend();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.panel = new ChartPanel(
chart,
defaultWidth,
defaultHeight,
0,
0,
screenSize.width,
screenSize.height,
true, //buffer
false,//properties
false,//copy
false,//save
false,//print
true, //zoom
false //tooltip
);
if(!withLabels) {
this.panel.setPopupMenu(null);
}
this.setBackground(!this.isBad() ?
Settings.normalLead :
Settings.badLead);
this.redrawAnnotations();
if(withLabels) {
panel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseClicked(ChartMouseEvent event) {
ChartEntity ce = event.getEntity();
if(ce == null)
return;
Point2D p = panel.translateScreenToJava2D(event.getTrigger().getPoint());
double x = plot.getDomainAxis().java2DToValue(p.getX(),
panel.getScreenDataArea(),
plot.getDomainAxisEdge());
if(canPlace) {
thisView.handler.addAnnotation(
Settings.getSelectedAnnotationType(), x);
plot.addDomainMarker(new ValueMarker(x,
Settings.getSelectedAnnotationColor(),
new BasicStroke()));
}
if(canRemove) {
double xl = plot.getDomainAxis().java2DToValue(p.getX() - 10,
panel.getScreenDataArea(),
plot.getDomainAxisEdge());
double xr = plot.getDomainAxis().java2DToValue(p.getX() + 10,
panel.getScreenDataArea(),
plot.getDomainAxisEdge());
ArrayList<Annotation> annos = thisView.handler.getAnnotations();
for(int i = 0; i < annos.size(); i++) {
if(annos.get(i).getLoc() < xr && annos.get(i).getLoc() > xl) {
ValueMarker m = new ValueMarker(annos.get(i).getLoc());
thisView.handler.removeAnnotation(i);
plot.removeDomainMarker(m);
thisView.redrawAnnotations();
break;
}
}
}
}
public void chartMouseMoved(ChartMouseEvent event) {}
});
}
this.setViewingDomain(data.getAt(0)[0], data.getAt(data.size()-1)[0]);
// System.out.println(origData.toArray()[1][0]);
}
/**
* addDataset - adds a new plot to the graph
*
* @param d the dataset
*/
public void addDataset(ECGDataSet d) {
dataset.addSeries(dataset.getSeriesCount()+1, d.toArray());
}
/**
* setCanPlace - sets whether clicking the chart will place annotations
*
* @param b the value of the flag
*/
public void setCanPlace(boolean b) {
canPlace = b;
}
/**
* setCanRemove - sets whether clicking the chart will remove annotations
* @param b the new value
*/
public void setCanRemove(boolean b) {
canRemove = b;
}
public void setSelected(boolean b) {
selected = b;
}
public boolean isSelected() {
return selected;
}
/**
* clearAnnotations - clears annotations from the view
*/
public void clearAnnotations() {
plot.clearDomainMarkers();
this.handler.clearAnnotations();
}
/**
* redrawAnnotations - refreshes the displayed annotations
*/
public void redrawAnnotations() {
plot.clearDomainMarkers();
for(int i = 0; i < handler.getAnnotations().size(); i++) {
plot.addDomainMarker(
new ValueMarker(handler.getAnnotations().get(i).getLoc(),
Settings.getAnnotationColor(
handler.getAnnotations().get(i).getType()),
new BasicStroke()));
}
}
/**
* setBackground - sets the background color of the panel
*
* @param c the color to set the panel to
*/
public void setBackground(Color c) {
this.chart.setBackgroundPaint(c);
this.panel.revalidate();
}
/**
* setBad - sets whether the data should be marked as bad
*
* @param b the value of the flag
*/
public void setBad(boolean b) {
handler.setBad(index, b);
}
/**
* isBad - gets whether the data being displayed is marked as bad
*
* @return true if bad
*/
public boolean isBad() {
return handler.isBad(index);
}
/**
* getPanel - gets the Swing panel to display
*
* @return a JFreeChart ChartPanel
*/
public ChartPanel getPanel() {
return this.panel;
}
/**
* clone - copies the view
*
* @param withLabels should the new view have labels on the chart?
*/
public Object clone(boolean withLabels) {
return new ECGView(handler, data, index, title, withLabels);
}
/**
* revalidate - redraws the chart
*/
public void revalidate() {
DefaultXYDataset dxyd = new DefaultXYDataset();
dxyd.addSeries(1, data.toArray());
this.chart.getXYPlot().setDataset(dxyd);
this.chart.getXYPlot().setDataset(this.chart.getXYPlot().getDataset());
this.chart.fireChartChanged();
this.panel.revalidate();
}
/**
* update - updates data inside of the ECGView
*/
public void update() {
ECGView newData = handler.getView(this.index, this.labels);
this.data = newData.data;
this.revalidate();
}
/**
* setViewingDomain - scales the view between two x values
*
* @param start the left value
* @param end the right value
*/
public void setViewingDomain(double start, double end) {
this.plot.getDomainAxis().setAutoRange(true);
this.plot.getDomainAxis().setRange(start, end);
}
/**
* filter - filters the current view
*
* @param filterid the type of filter to apply
* @param params the arguments to the filter
*/
public void filter(int filterid, Number[] params) {
this.data = handler.shallowFilter(index, filterid, params, labels).data;
}
/**
* addLegend - enables a legend on the chart
*/
public void addLegend() {
this.chart.addLegend(new LegendTitle(plot));
}
}