Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * NumericalDensityPlot.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.GeneralPath;
32
33 /**
34 * @author Andrew Rambaut
35 */
36 public class BoxPlot extends Plot.AbstractPlot { //Plot.AbstractPlot {
37
38 private final boolean isVertical;
39 private final double boxWidth;
40 private final boolean showTails;
41 private final double upper, lower, mean, lowerTail, upperTail;
42
43 protected Stroke meanLineStroke = new BasicStroke(1.5f);
44 protected Paint meanLinePaint = Color.black;
45
46 public BoxPlot(boolean isVertical, double boxWidth, double lower, double upper, double lowerTail, double upperTail, double mean) {
47 super();
48
49 this.isVertical = isVertical;
50 this.boxWidth = boxWidth;
51 this.showTails = true;
52
53 this.lower = lower;
54 this.upper = upper;
55 this.mean = mean;
56
57 this.lowerTail = lowerTail;
58 this.upperTail = upperTail;
59 }
60
61 /**
62 * Set up the axis with some data
63 */
64 public void setupAxis(Axis xAxis, Axis yAxis, Variate xData, Variate yData) {
65 Axis valueAxis = (isVertical ? yAxis : xAxis);
66 double minValue = lower;
67 double maxValue = upper;
68 if (showTails) {
69 minValue = lowerTail;
70 maxValue = upperTail;
71 }
72
73 valueAxis.addRange(minValue, maxValue);
74 }
75
76 /**
77 * Set line style
78 */
79 public void setMeanLineStyle(Stroke lineStroke, Paint linePaint) {
80 this.meanLineStroke = lineStroke;
81 this.meanLinePaint = linePaint;
82 }
83
84 protected boolean hasData() {
85 return true;
86 }
87
88 /**
89 * Paint data series
90 */
91 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
92
93 GeneralPath boxPath = new GeneralPath();
94 GeneralPath tailPath = new GeneralPath();
95 GeneralPath meanPath = new GeneralPath();
96
97 if (isVertical) {
98 // rotate -90
99 float y1 = (float) transformY(lower);
100 float x1 = (float) transformX(getPlotNumber() + 1 - (boxWidth / 2));
101 float y2 = (float) transformY(upper);
102 float x2 = (float) transformX(getPlotNumber() + 1 + (boxWidth / 2));
103
104 boxPath.moveTo(x1, y1);
105 boxPath.lineTo(x2, y1);
106 boxPath.lineTo(x2, y2);
107 boxPath.lineTo(x1, y2);
108 boxPath.lineTo(x1, y1);
109 boxPath.closePath();
110
111 if (showTails) {
112 float x = (float) transformX(getPlotNumber() + 1);
113 float y3 = (float) transformY(lowerTail);
114 float y4 = (float) transformY(upperTail);
115 tailPath.moveTo(x, y3);
116 tailPath.lineTo(x, y1);
117 tailPath.moveTo(x, y2);
118 tailPath.lineTo(x, y4);
119 }
120
121 float y = (float) transformY(mean);
122 meanPath.moveTo(x1, y);
123 meanPath.lineTo(x2, y);
124
125
126 } else {
127 float x1 = (float) transformX(lower);
128 float y1 = (float) transformY(getPlotNumber() + 1 - (boxWidth / 2));
129 float x2 = (float) transformX(upper);
130 float y2 = (float) transformY(getPlotNumber() + 1 + (boxWidth / 2));
131
132 boxPath.moveTo(x1, y1);
133 boxPath.lineTo(x2, y1);
134 boxPath.lineTo(x2, y2);
135 boxPath.lineTo(x1, y2);
136 boxPath.lineTo(x1, y1);
137 boxPath.closePath();
138
139 if (showTails) {
140 float y = (float) transformY(getPlotNumber() + 1);
141 float x3 = (float) transformX(lowerTail);
142 float x4 = (float) transformX(upperTail);
143 tailPath.moveTo(x3, y);
144 tailPath.lineTo(x1, y);
145 tailPath.moveTo(x2, y);
146 tailPath.lineTo(x4, y);
147 }
148
149 float x = (float) transformX(mean);
150 meanPath.moveTo(x, y1);
151 meanPath.lineTo(x, y2);
152
153
154 }
155
156 boolean solid = true;
157 if (solid) {
158 Paint fillPaint = new Color(
159 ((Color) linePaint).getRed(),
160 ((Color) linePaint).getGreen(),
161 ((Color) linePaint).getBlue(), 32);
162 g2.setPaint(fillPaint);
163
164 g2.fill(boxPath);
165 }
166
167 g2.setPaint(meanLinePaint);
168 g2.setStroke(meanLineStroke);
169 g2.draw(meanPath);
170
171 g2.setStroke(lineStroke);
172 g2.setPaint(linePaint);
173
174 if (showTails) {
175 g2.draw(tailPath);
176 }
177
178 g2.draw(boxPath);
179
180 }
181
182
183 }