Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * ScatterPlot.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.Point2D;
32 import java.awt.geom.Rectangle2D;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.Vector;
36
37 /**
38 * Description: A scatter plot.
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 * @version $Id: ScatterPlot.java,v 1.10 2005/05/24 20:25:59 rambaut Exp $
43 */
44
45 public class ScatterPlot extends Plot.AbstractPlot {
46
47 protected Vector<Rectangle2D> markBounds = null;
48
49 protected Stroke hilightedMarkStroke = new BasicStroke(0.5f);
50 protected Paint hilightedMarkPaint = Color.black;
51 protected Paint hilightedMarkFillPaint = Color.blue;
52
53 /**
54 * Constructor
55 */
56 public ScatterPlot(Variate.N xData, Variate.N yData) {
57 super(xData, yData);
58 setMarkStyle(CIRCLE_MARK, 5, new BasicStroke(1),
59 Color.black, Color.yellow);
60 setHilightedMarkStyle(new BasicStroke(1),
61 Color.black, Color.blue);
62 }
63
64 /**
65 * Constructor
66 */
67 public ScatterPlot(List<Double> xData, List<Double> yData) {
68 super(xData, yData);
69 setMarkStyle(CIRCLE_MARK, 5, new BasicStroke(1),
70 Color.black, Color.yellow);
71 setHilightedMarkStyle(new BasicStroke(1),
72 Color.black, Color.blue);
73 }
74
75 /**
76 * Constructor
77 */
78 public ScatterPlot(String name, List<Double> xData, List<Double> yData) {
79 super(xData, yData);
80 setName(name);
81 setMarkStyle(CIRCLE_MARK, 5, new BasicStroke(1),
82 Color.black, Color.yellow);
83 setHilightedMarkStyle(new BasicStroke(1),
84 Color.black, Color.blue);
85 }
86
87 /**
88 * Set mark style
89 */
90 public void setHilightedMarkStyle(Stroke hilightedMarkStroke,
91 Paint hilightedMarkPaint,
92 Paint hilightedMarkFillPaint) {
93
94 this.hilightedMarkStroke = hilightedMarkStroke;
95 this.hilightedMarkPaint = hilightedMarkPaint;
96 this.hilightedMarkFillPaint = hilightedMarkFillPaint;
97 }
98
99 /**
100 * Draw a mark transforming co-ordinates to each axis
101 */
102 protected void drawMark(Graphics2D g2, float x, float y, Color color) {
103
104 Rectangle2D bounds = mark.getBounds2D();
105 float w = (float) bounds.getWidth();
106 float h = (float) bounds.getHeight();
107 x = x - (w / 2);
108 y = y - (h / 2);
109
110 g2.translate(x, y);
111
112 if (color == null) {
113 if (markFillPaint != null) {
114 g2.setPaint(markFillPaint);
115 g2.fill(mark);
116 }
117 } else {
118 g2.setPaint(color);
119 g2.fill(mark);
120 }
121
122 g2.setPaint(markPaint);
123 g2.setStroke(markStroke);
124 g2.draw(mark);
125
126 g2.translate(-x, -y);
127
128 Rectangle2D rect = new Rectangle2D.Float(x, y, w, h);
129 markBounds.add(rect);
130 }
131
132 /**
133 * Draw a mark transforming co-ordinates to each axis
134 */
135 protected void drawMarkHilighted(Graphics2D g2, float x, float y) {
136
137 Rectangle2D bounds = mark.getBounds2D();
138 float w = (float) bounds.getWidth();
139 float h = (float) bounds.getHeight();
140 x = x - (w / 2);
141 y = y - (h / 2);
142
143 g2.translate(x, y);
144
145 if (hilightedMarkFillPaint != null) {
146 g2.setPaint(hilightedMarkFillPaint);
147 g2.fill(mark);
148 }
149
150 g2.setPaint(hilightedMarkPaint);
151 g2.setStroke(hilightedMarkStroke);
152 g2.draw(mark);
153
154 g2.translate(-x, -y);
155
156 Rectangle2D rect = new Rectangle2D.Float(x, y, w, h);
157 markBounds.add(rect);
158 }
159
160 /**
161 * Paint data series
162 */
163 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
164
165 float x, y;
166
167 markBounds = new java.util.Vector<Rectangle2D>();
168
169 Set<Integer> selectedPoints = getSelectedPoints();
170
171 int n = xData.getCount();
172 if (colours != null && colours.size() == n) {
173 for (int i = 0; i < n; i++) {
174 x = (float) transformX(((Number) xData.get(i)).doubleValue());
175 y = (float) transformY(((Number) yData.get(i)).doubleValue());
176
177 if (selectedPoints.contains(i)) {
178 drawMarkHilighted(g2, x, y);
179 } else {
180 drawMark(g2, x, y, colours.get(i));
181 }
182 }
183 } else {
184 for (int i = 0; i < n; i++) {
185 x = (float) transformX(((Number) xData.get(i)).doubleValue());
186 y = (float) transformY(((Number) yData.get(i)).doubleValue());
187
188 if (selectedPoints.contains(i)) {
189 drawMarkHilighted(g2, x, y);
190 } else {
191 drawMark(g2, x, y, null);
192 }
193 }
194 }
195
196 }
197
198 /**
199 * A point on the plot has been clicked
200 */
201 public void pointClicked(Point2D point, boolean isShiftDown) {
202
203 double x = untransformX(point.getX());
204 double y = untransformY(point.getY());
205
206 if (markBounds != null) {
207 int mark = -1;
208
209 for (int i = 0; i < markBounds.size(); i++) {
210 if ((markBounds.get(i)).contains(point)) {
211 mark = i;
212 break;
213 }
214 }
215 fireMarkClickedEvent(mark, x, y, isShiftDown);
216 }
217
218 firePointClickedEvent(x, y, isShiftDown);
219 }
220
221 }
222