Debian Codegraph @

src/dr/app/gui/chart/Plot.java

raw symbols SCIP
1 /*
2 * Plot.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.chart;
27
28 import dr.stats.Variate;
29
30 import java.awt.*;
31 import java.awt.geom.*;
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36
37 /**
38 * Plot.java
39 * <p/>
40 * Description: Provides an interface for plots. Plots are elements of a
41 * chart that renders some data in a particular style. Multiple plots can
42 * be added to the same chart to get complex results.
43 *
44 * @author Andrew Rambaut
45 * @author Alexei Drummond
46 * @version $Id: Plot.java,v 1.23 2006/01/03 10:26:20 rambaut Exp $
47 */
48
49 public interface Plot {
50
51 // These constants are used for automatic scaling to select exactly
52 // where the axis starts and stops.
53 int NO_MARK = 0;
54 int POINT_MARK = 1;
55 int CROSS_MARK = 2;
56 int PLUS_MARK = 3;
57 int CIRCLE_MARK = 4;
58 int SQUARE_MARK = 5;
59 int DIAMOND_MARK = 6;
60
61 /**
62 * Set axes
63 */
64 void setAxes(Axis xAxis, Axis yAxis);
65
66 /**
67 * Resets axis ranges (if new data has been added)
68 */
69 void resetAxes();
70
71 /**
72 * Set data
73 */
74 void setData(List<Double> xData, List<Double> yData);
75
76 /**
77 * Set data
78 */
79 void setData(Variate.N xData, Variate.N yData);
80
81 /**
82 * Set line style
83 */
84 void setLineStyle(Stroke lineStroke, Paint linePaint);
85
86 /**
87 * Set line stroke
88 */
89 void setLineStroke(Stroke lineStroke);
90
91 /**
92 * Set line color
93 */
94 void setLineColor(Paint linePaint);
95
96 /**
97 * Get line color
98 */
99 Paint getLineColor();
100
101 /**
102 * Get line stroke
103 */
104 Stroke getLineStroke();
105
106 /**
107 * Set mark style
108 */
109 void setMarkStyle(int markType, double markSize, Stroke markStroke,
110 Paint markPaint, Paint markFillPaint);
111
112 /**
113 * Set mark style
114 */
115 void setMarkStyle(Shape mark, Stroke markStroke,
116 Paint markPaint, Paint markFillPaint);
117
118 /**
119 * Paint actual plot
120 */
121 void paintPlot(Graphics2D g2, double xScale, double yScale,
122 double xOffset, double yOffset, int plotNumber, int plotCount);
123
124 /**
125 * Set name
126 */
127 void setName(String name);
128
129 /**
130 * Get name
131 */
132 String getName();
133
134 int getPlotNumber();
135
136 void setPlotNumber(int plotNumber);
137
138 double getXLocation();
139
140 double getYLocation();
141
142 void setLocation(double x, double y);
143
144 /**
145 * A point on the plot has been clicked
146 */
147 void pointClicked(Point2D point, boolean isShiftDown);
148
149 void selectPoint(int index, boolean addToSelection);
150
151 void selectPoints(Rectangle2D dragRectangle, boolean addToSelection);
152
153 void clearSelection();
154
155 void setSelectedPoints(final Collection<Integer> selectedPoints);
156
157 Set<Integer> getSelectedPoints();
158
159 Variate getXData();
160
161 Variate getYData();
162
163 void setChart(JChart chart);
164
165 JChart getChart();
166
167 public interface Listener {
168
169 void pointClicked(double x, double y, boolean isShiftDown);
170
171 void markClicked(int index, double x, double y, boolean isShiftDown);
172
173 void selectionChanged(Set<Integer> selectedPoints);
174
175 void rangeXSelected(double lower, double upper);
176
177 void rangeYSelected(double lower, double upper);
178
179 void rangeXYSelected(double lowerX, double lowerY, double upperX, double upperY);
180
181 }
182
183 public class Adaptor implements Listener {
184 public void pointClicked(double x, double y, boolean isShiftDown) {
185 }
186
187 public void markClicked(int index, double x, double y, boolean isShiftDown) {
188 }
189
190 public void selectionChanged(final Set<Integer> selectedPoints) {
191 }
192
193 public void rangeXSelected(double lower, double upper) {
194 }
195
196 public void rangeYSelected(double lower, double upper) {
197 }
198
199 public void rangeXYSelected(double lowerX, double lowerY, double upperX, double upperY) {
200 }
201 }
202
203 /**
204 * AbstractPlot.java
205 * <p/>
206 * Description: An abstract base class for plots
207 */
208 abstract class AbstractPlot implements Plot {
209
210 private JChart chart = null;
211
212 protected Axis xAxis, yAxis;
213 protected Variate.N xData = null;
214 protected Variate.N yData = null;
215
216 protected List<Color> colours = null;
217
218 protected Shape mark;
219
220 protected Stroke lineStroke = new BasicStroke(1.5f);
221 protected Paint linePaint = Color.black;
222
223 protected Stroke markStroke = new BasicStroke(0.5f);
224 protected Paint markPaint = Color.black;
225 protected Paint markFillPaint = Color.black;
226
227 protected double xScale, yScale, xOffset, yOffset;
228
229 private String name;
230
231 private int plotNumber = -1, plotCount = -1;
232 private double xLocation = 0, yLocation = 0;
233
234 private Set<Integer> selectedPoints = new HashSet<Integer>();
235
236 /**
237 * Constructor
238 */
239 public AbstractPlot() {
240 }
241
242 /**
243 * Constructor
244 */
245 public AbstractPlot(Variate.N xData, Variate.N yData) {
246 setData(xData, yData);
247 }
248
249
250 /**
251 * Constructor
252 */
253 public AbstractPlot(List<Double> xData, List<Double> yData) {
254 setData(xData, yData);
255 }
256
257 public JChart getChart() {
258 return chart;
259 }
260
261 public void setChart(JChart chart) {
262 this.chart = chart;
263 }
264
265 /**
266 * Set data
267 */
268 public void setData(List<Double> xData, List<Double> yData) {
269 Variate.D xd = new Variate.D(xData);
270 Variate.D yd = new Variate.D(yData);
271
272 this.xData = xd;
273 this.yData = yd;
274 }
275
276 /**
277 * Set data
278 */
279 public void setData(Variate.N xData, Variate.N yData) {
280 this.xData = xData;
281 this.yData = yData;
282 }
283
284 /**
285 * Set data
286 */
287 public void setColours(List<Color> colours) {
288 this.colours = colours;
289 }
290
291 /**
292 * Set axes
293 */
294 public void setAxes(Axis xAxis, Axis yAxis) {
295 this.xAxis = xAxis;
296 this.yAxis = yAxis;
297 setupAxis(xAxis, yAxis, xData, yData);
298 }
299
300 /**
301 * Resets axis ranges (if new data has been added)
302 */
303 public void resetAxes() {
304 setupAxis(xAxis, yAxis, xData, yData);
305 }
306
307 public int getPlotNumber() {
308 return plotNumber;
309 }
310
311 public void setPlotNumber(int plotNumber) {
312 this.plotNumber = plotNumber;
313 }
314
315 public double getXLocation() {
316 return xLocation;
317 }
318
319 public double getYLocation() {
320 return yLocation;
321 }
322
323 @Override
324 public void setLocation(double x, double y) {
325 xLocation = x;
326 yLocation = y;
327 }
328
329 /**
330 * Set up the axis with some data
331 */
332 public void setupAxis(Axis xAxis, Axis yAxis, Variate xData, Variate yData) {
333 if (xData != null) {
334 if (xAxis instanceof LogAxis) {
335 double minValue = java.lang.Double.POSITIVE_INFINITY;
336
337 for (int i = 0; i < xData.getCount(); i++) {
338 double value = (Double) xData.get(i);
339 if (value > 0.0 && value < minValue)
340 minValue = value;
341 }
342
343 xAxis.addRange(minValue, (Double) xData.getMax());
344 } else {
345 xAxis.addRange((Double) xData.getMin(), (Double) xData.getMax());
346 }
347 }
348 if (yData != null) {
349 if (yAxis instanceof LogAxis) {
350 double minValue = java.lang.Double.POSITIVE_INFINITY;
351
352 for (int i = 0; i < yData.getCount(); i++) {
353 double value = (Double) yData.get(i);
354 if (value > 0.0 && value < minValue)
355 minValue = value;
356 }
357
358 yAxis.addRange(minValue, (Double) yData.getMax());
359 } else {
360 yAxis.addRange((Double) yData.getMin(), (Double) yData.getMax());
361 }
362 }
363 }
364
365 /**
366 * Set line style
367 */
368 public void setLineStyle(Stroke lineStroke, Paint linePaint) {
369 this.lineStroke = lineStroke;
370 this.linePaint = linePaint;
371 }
372
373 /**
374 * Set line stroke
375 */
376 public void setLineStroke(Stroke lineStroke) {
377 this.lineStroke = lineStroke;
378 }
379
380 /**
381 * Set line color
382 */
383 public void setLineColor(Paint linePaint) {
384 this.linePaint = linePaint;
385 }
386
387 public final Paint getLineColor() {
388 return linePaint;
389 }
390
391 public final Stroke getLineStroke() {
392 return lineStroke;
393 }
394
395 public final void setName(String name) {
396 this.name = name;
397 }
398
399 public final String getName() {
400 return name;
401 }
402
403 /**
404 * Set mark style
405 */
406 public void setMarkStyle(int markType, double markSize, Stroke markStroke,
407 Paint markPaint, Paint markFillPaint) {
408
409 float w = (float) (markSize / 2.0);
410
411 GeneralPath path;
412
413 switch (markType) {
414 case POINT_MARK:
415 path = new GeneralPath();
416 path.moveTo(0, 0);
417 path.lineTo(0, 0);
418 setMarkStyle(path, markStroke, markPaint, markFillPaint);
419 break;
420 case CROSS_MARK:
421 path = new GeneralPath();
422 path.moveTo(-w, -w);
423 path.lineTo(w, w);
424 path.moveTo(w, -w);
425 path.lineTo(-w, w);
426 setMarkStyle(path, markStroke, markPaint, markFillPaint);
427 break;
428 case PLUS_MARK:
429 path = new GeneralPath();
430 path.moveTo(-w, 0);
431 path.lineTo(w, 0);
432 path.moveTo(0, -w);
433 path.lineTo(0, w);
434 setMarkStyle(path, markStroke, markPaint, markFillPaint);
435 break;
436 case CIRCLE_MARK:
437 setMarkStyle(new Ellipse2D.Double(0.0, 0.0, markSize, markSize), markStroke, markPaint, markFillPaint);
438 break;
439 case SQUARE_MARK:
440 setMarkStyle(new Rectangle2D.Double(-w, -w, markSize, markSize), markStroke, markPaint, markFillPaint);
441 break;
442 case DIAMOND_MARK:
443 path = new GeneralPath();
444 path.moveTo(0, -w);
445 path.lineTo(w, 0);
446 path.lineTo(0, w);
447 path.lineTo(-w, 0);
448 path.closePath();
449 setMarkStyle(path, markStroke, markPaint, markFillPaint);
450 break;
451 }
452
453
454 }
455
456 /**
457 * Set mark style
458 */
459 public void setMarkStyle(Shape mark, Stroke markStroke,
460 Paint markPaint, Paint markFillPaint) {
461
462 this.mark = mark;
463 this.markStroke = markStroke;
464 this.markPaint = markPaint;
465 this.markFillPaint = markFillPaint;
466 }
467
468 public Variate getXData() {
469 return xData;
470 }
471
472 public Variate getYData() {
473 return yData;
474 }
475
476 /**
477 * Transform a chart co-ordinates into a drawing co-ordinates
478 */
479 protected double transformX(double value) {
480 double tx = xAxis.transform(value + xLocation);
481 if (Double.isNaN(tx) || tx == Double.NEGATIVE_INFINITY) {
482 return Double.NEGATIVE_INFINITY;
483 }
484 return ((tx - xAxis.transform(xAxis.getMinAxis())) * xScale) + xOffset;
485 }
486
487 /**
488 * Transform a chart co-ordinates into a drawing co-ordinates
489 */
490 protected double transformY(double value) {
491 double ty = yAxis.transform(value + yLocation);
492 if (Double.isNaN(ty) || ty == Double.NEGATIVE_INFINITY) {
493 return Double.NEGATIVE_INFINITY;
494 }
495 return ((ty - yAxis.transform(yAxis.getMinAxis())) * yScale) + yOffset;
496 }
497
498 /**
499 * Transform a drawing co-ordinate into a chart co-ordinate
500 */
501 protected double untransformX(double value) {
502
503 return xAxis.untransform(
504 xAxis.transform(xAxis.getMinAxis()) + ((value - xOffset) / xScale)) - xLocation;
505 }
506
507 /**
508 * Transform a drawing co-ordinate into a chart co-ordinate
509 */
510 protected double untransformY(double value) {
511
512 return yAxis.untransform(
513 yAxis.transform(yAxis.getMinAxis()) + ((value - yOffset) / yScale)) - yLocation;
514 }
515
516 /**
517 * Draw a line transforming co-ordinates to each axis
518 */
519 protected void drawLine(Graphics2D g2, double x1, double y1, double x2, double y2) {
520 Line2D line = new Line2D.Double(transformX(x1), transformY(y1),
521 transformX(x2), transformY(y2));
522 g2.draw(line);
523 }
524
525 /**
526 * Draw a rectangle directly (no transformation)
527 */
528 protected void drawRect(Graphics2D g2, float tx1, float ty1, float tx2, float ty2) {
529 GeneralPath path = new GeneralPath();
530 path.moveTo(tx1, ty1);
531 path.lineTo(tx1, ty2);
532 path.lineTo(tx2, ty2);
533 path.lineTo(tx2, ty1);
534 path.closePath();
535 // Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
536 g2.draw(path);
537 }
538
539 /**
540 * Fill a rectangle directly (no transformation)
541 */
542 protected void fillRect(Graphics2D g2, float tx1, float ty1, float tx2, float ty2) {
543
544 GeneralPath path = new GeneralPath();
545 path.moveTo(tx1, ty1);
546 path.lineTo(tx1, ty2);
547 path.lineTo(tx2, ty2);
548 path.lineTo(tx2, ty1);
549 path.closePath();
550 // Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
551 g2.fill(path);
552 }
553
554 /**
555 * Draw a rectangle transforming co-ordinates to each axis
556 */
557 protected void drawRect(Graphics2D g2, double x1, double y1, double x2, double y2) {
558
559 float tx1 = (float) transformX(x1);
560 float ty1 = (float) transformY(y1);
561 float tx2 = (float) transformX(x2);
562 float ty2 = (float) transformY(y2);
563 drawRect(g2, tx1, ty1, tx2, ty2);
564 }
565
566 /**
567 * Fill a rectangle transforming co-ordinates to each axis
568 */
569 protected void fillRect(Graphics2D g2, double x1, double y1, double x2, double y2) {
570
571 float tx1 = (float) transformX(x1);
572 float ty1 = (float) transformY(y1);
573 float tx2 = (float) transformX(x2);
574 float ty2 = (float) transformY(y2);
575 fillRect(g2, tx1, ty1, tx2, ty2);
576 }
577
578 /**
579 * Paint actual plot
580 */
581 @Override
582 public void paintPlot(Graphics2D g2, double xScale, double yScale,
583 double xOffset, double yOffset, int plotNumber, int plotCount) {
584 if (xAxis == null || yAxis == null)
585 return;
586
587 this.xScale = xScale;
588 this.yScale = yScale;
589 this.xOffset = xOffset;
590 this.yOffset = yOffset;
591
592 this.plotNumber = plotNumber;
593 this.plotCount = plotCount;
594
595 if (hasData()) {
596 paintData(g2, xData, yData);
597 }
598 }
599
600 /**
601 * Paint data series
602 */
603 abstract protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData);
604
605 protected boolean hasData() {
606 return (xData != null && yData != null && xData.getCount() > 0);
607 }
608
609 /**
610 * A point on the plot has been clicked
611 */
612 public void pointClicked(Point2D point, boolean isShiftDown) {
613
614 double x = untransformX(point.getX());
615 double y = untransformY(point.getY());
616
617 firePointClickedEvent(x, y, isShiftDown);
618 }
619
620 public void selectPoints(final Rectangle2D dragRectangle, final boolean addToSelection) {
621 if (dragRectangle == null) {
622 return;
623 }
624
625 if (!addToSelection) {
626 selectedPoints.clear();
627 }
628
629 double x0 = untransformX(dragRectangle.getX());
630 double y0 = untransformY(dragRectangle.getY() + dragRectangle.getHeight());
631
632 double x1 = untransformX(dragRectangle.getX() + dragRectangle.getWidth());
633 double y1 = untransformY(dragRectangle.getY());
634
635 for (int i = 0; i < xData.getCount(); i ++) {
636 double x = (Double) xData.get(i);
637 double y = (Double) yData.get(i);
638
639 if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
640 selectedPoints.add(i);
641 }
642 }
643
644 fireSelectionChanged();
645 }
646
647 public void selectPoint(final int index, final boolean addToSelection) {
648
649 if (!addToSelection) {
650 selectedPoints.clear();
651 }
652
653 selectedPoints.add(index);
654
655 fireSelectionChanged();
656 }
657
658 public void clearSelection() {
659 selectedPoints.clear();
660 fireSelectionChanged();
661 }
662
663 public void setSelectedPoints(final Collection<Integer> selectedPoints) {
664 this.selectedPoints.clear();
665 this.selectedPoints.addAll(selectedPoints);
666 }
667
668
669 public Set<Integer> getSelectedPoints() {
670 return selectedPoints;
671 }
672
673 // Listeners
674
675 private final java.util.Vector<Listener> listeners = new java.util.Vector<Listener>();
676
677 /**
678 * Add a plot listener
679 */
680 public void addListener(Listener listener) {
681 listeners.add(listener);
682 }
683
684 /**
685 * Tells plot listeners that a point has been clicked.
686 */
687 protected void firePointClickedEvent(double x, double y, boolean isShiftDown) {
688 for (int i = 0; i < listeners.size(); i++) {
689 final Listener listener = listeners.elementAt(i);
690 listener.pointClicked(x, y, isShiftDown);
691 }
692 }
693
694 /**
695 * Tells plot listeners that a point has been clicked.
696 */
697 protected void fireMarkClickedEvent(int index, double x, double y, boolean isShiftDown) {
698 for (int i=0; i < listeners.size(); i++) {
699 final Listener listener = listeners.elementAt(i);
700 listener.markClicked(index, x, y, isShiftDown);
701 }
702 }
703
704 protected void fireSelectionChanged() {
705 for (int i = 0; i < listeners.size(); i++) {
706 final Listener listener = listeners.elementAt(i);
707 listener.selectionChanged(selectedPoints);
708 }
709 }
710
711
712 }
713 }
714