Debian Codegraph @

src/dr/app/gui/chart/NumericalDensityPlot.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.inference.trace.TraceType;
29 import dr.stats.Variate;
30 import dr.util.FrequencyDistribution;
31
32 import java.awt.*;
33 import java.awt.geom.GeneralPath;
34 import java.awt.geom.Rectangle2D;
35 import java.util.*;
36
37 public class NumericalDensityPlot extends HistogramPlot {
38
39 boolean relativeDensity = true;
40 boolean pointsOnly = false;
41 int minimumBinCount;
42
43 public boolean isSolid() {
44 return solid;
45 }
46
47 public void setSolid(boolean solid) {
48 this.solid = solid;
49 }
50
51 boolean solid = true;
52
53 public NumericalDensityPlot(java.util.List<Double> data, int minimumBinCount) {
54 super(data, minimumBinCount);
55 this.minimumBinCount = minimumBinCount;
56 }
57
58 public NumericalDensityPlot(Variate.D data, int minimumBinCount) {
59 super(data, minimumBinCount);
60 this.minimumBinCount = minimumBinCount;
61 }
62
63 public void setRelativeDensity(boolean relative) {
64 relativeDensity = relative;
65 setData((Variate.D)getRawData(), minimumBinCount);
66 }
67
68 public void setPointsOnly(boolean pointsOnly) {
69 this.pointsOnly = pointsOnly;
70 }
71
72 /**
73 * Set data
74 */
75 public void setData(Variate.D data, int minimumBinCount) {
76
77 setRawData(data);
78 FrequencyDistribution frequency = getFrequencyDistribution(data, minimumBinCount);
79
80 Variate.D xData = new Variate.D();
81 Variate.D yData = new Variate.D();
82
83 double x = frequency.getLowerBound() - frequency.getBinSize();
84 double maxDensity = 0.0;
85 for (int i = 0; i < frequency.getBinCount(); i++) {
86 double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getCount();
87 if (density > maxDensity) maxDensity = density;
88 }
89
90 xData.add(x + (frequency.getBinSize() / 2.0));
91 yData.add(0.0);
92 x += frequency.getBinSize();
93
94 for (int i = 0; i < frequency.getBinCount(); i++) {
95 xData.add(x + (frequency.getBinSize() / 2.0));
96 double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getCount();
97 if (relativeDensity) {
98 yData.add(density / maxDensity);
99 } else {
100 yData.add(density);
101 }
102 x += frequency.getBinSize();
103 }
104
105 xData.add(x + (frequency.getBinSize() / 2.0));
106 yData.add(0.0);
107
108 setData(xData, yData);
109 }
110
111 /**
112 * Set bar fill style. Use a barPaint of null to not fill bar.
113 * Bar outline style is set using setLineStyle
114 */
115 public void setBarFillStyle(Paint barPaint) {
116 throw new IllegalArgumentException();
117 }
118
119 /**
120 * Paint data series
121 */
122 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
123
124 int n = xData.getCount();
125
126 if (pointsOnly) {
127 setMarkStyle(Plot.CIRCLE_MARK, 3, new BasicStroke(0.5F), new Color(44, 44, 44), new Color(249, 202, 105));
128 Rectangle2D bounds = mark.getBounds2D();
129 float w = (float) bounds.getWidth();
130 float h = (float) bounds.getHeight();
131
132 for (int i = 0; i < n; i++) {
133 float x = (float) transformX(((Number)xData.get(i)).doubleValue());
134 float y = (float) transformY(((Number)yData.get(i)).doubleValue());
135
136 x = x - (w / 2);
137 y = y - (h / 2);
138
139 g2.translate(x, y);
140
141 if (markFillPaint != null) {
142 g2.setPaint(markFillPaint);
143 g2.fill(mark);
144 }
145
146 g2.setPaint(markPaint);
147 g2.setStroke(markStroke);
148 g2.draw(mark);
149
150 g2.translate(-x, -y);
151 }
152
153 } else {
154 float x = (float) transformX(((Number)xData.get(0)).doubleValue());
155 float y = (float) transformY(((Number)yData.get(0)).doubleValue());
156
157 GeneralPath path = new GeneralPath();
158 path.moveTo(x, y);
159
160 for (int i = 1; i < n; i++) {
161 x = (float) transformX(((Number)xData.get(i)).doubleValue());
162 y = (float) transformY(((Number)yData.get(i)).doubleValue());
163
164 path.lineTo(x, y);
165 }
166
167 if (solid) {
168 path.closePath();
169 Paint fillPaint = new Color(
170 ((Color) linePaint).getRed(),
171 ((Color) linePaint).getGreen(),
172 ((Color) linePaint).getBlue(), 32);
173 g2.setPaint(fillPaint);
174 g2.fill(path);
175 }
176
177 g2.setStroke(lineStroke);
178 g2.setPaint(linePaint);
179 g2.draw(path);
180 }
181 }
182 }