| 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 |
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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|