Debian Codegraph @

src/dr/app/gui/chart/IntegerViolinPlot.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.FrequencyCounter;
29 import dr.stats.Variate;
30
31 import java.awt.*;
32
33 /**
34 * @author Andrew Rambaut
35 */
36 public class IntegerViolinPlot extends Plot.AbstractPlot { //Plot.AbstractPlot {
37
38 private Paint barPaint = new Color(124, 164, 221);
39 private Paint intervalTailPaint = new Color(232, 114, 103);
40
41 private boolean hasIntervals = false;
42 private double upperInterval = 0.0;
43 private double lowerInterval = 0.0;
44 private final boolean isVertical;
45 private final boolean showTails;
46
47 private double violinWidth = 0.9;
48
49 public IntegerViolinPlot(boolean isVertical, double violinWidth, double lower, double upper, boolean showTails, FrequencyCounter<Integer> frequencyCounter) {
50 super();
51 this.isVertical = isVertical;
52 this.violinWidth = violinWidth;
53 this.showTails = showTails;
54
55 this.lowerInterval = lower;
56 this.upperInterval = upper;
57
58 setData(frequencyCounter);
59 }
60
61 private void setData(FrequencyCounter<Integer> frequencyCounter) {
62 double scale = (0.5 * violinWidth) / frequencyCounter.getMaxFrequency();
63 Variate.D xd = new Variate.D();
64 Variate.D yd = new Variate.D();
65
66 java.util.List<Integer> values = frequencyCounter.getUniqueValues();
67
68 int i = 0;
69 for (int value : values) {
70 xd.add((double) value);
71 yd.add((double)frequencyCounter.getFrequency(value) * scale);
72 }
73
74 setData(xd, yd);
75 }
76
77 /**
78 * Set up the axis with some data
79 */
80 public void setupAxis(Axis xAxis, Axis yAxis, Variate xData, Variate yData) {
81 Axis valueAxis = (isVertical ? yAxis : xAxis);
82 Axis densityAxis = (isVertical ? xAxis : yAxis);
83 Variate valueData = xData;
84 Variate densityData = yData;
85
86 if (valueData != null) {
87 if (valueAxis instanceof LogAxis) {
88 double minValue = java.lang.Double.POSITIVE_INFINITY;
89
90 for (int i = 0; i < valueData.getCount(); i++) {
91 double value = (Double) valueData.get(i);
92 if (value > 0.0 && value < minValue)
93 minValue = value;
94 }
95
96 valueAxis.addRange(minValue, (Double) valueData.getMax());
97 } else {
98 valueAxis.addRange((Double) valueData.getMin(), (Double) valueData.getMax());
99 }
100 }
101
102 if (densityData != null) {
103 if (densityAxis instanceof LogAxis) {
104 throw new IllegalArgumentException("The density axis shouldn't be log scale");
105 } else {
106 // do nothing - the range will be set up by the chart
107 }
108 }
109 }
110
111 /**
112 * Set paints
113 */
114 public void setPaints(Paint barPaint, Paint intervalTailPaint) {
115 this.barPaint = barPaint;
116 this.intervalTailPaint = intervalTailPaint;
117 }
118
119 /**
120 * Set arbitrary intervals to use (0 for none).
121 */
122 public void setIntervals(double lower, double upper) {
123 hasIntervals = true;
124 upperInterval = upper;
125 lowerInterval = lower;
126 }
127
128 /**
129 * Paint data series
130 */
131 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
132
133 double x, x1, y1, x2, y2;
134 int n = xData.getCount();
135
136 g2.setStroke(lineStroke);
137
138 for (int i = 0; i < n; i ++) {
139
140 x = (Double)xData.get(i);
141
142 x1 = x - 0.5;
143 y1 = getPlotNumber() + 1 - (Double) yData.get(i);
144 x2 = x + 0.5;
145 y2 = getPlotNumber() + 1 + (Double) yData.get(i);
146
147 if (barPaint != null) {
148 if (hasIntervals && (x < lowerInterval || x > upperInterval)) {
149 g2.setPaint(intervalTailPaint);
150 } else {
151 g2.setPaint(barPaint);
152 }
153 if (isVertical) {
154 fillRect(g2, y1, x1, y2, x2);
155 } else {
156 fillRect(g2, x1, y1, x2, y2);
157 }
158 }
159
160 if (lineStroke != null && linePaint != null) {
161 g2.setStroke(lineStroke);
162 g2.setPaint(linePaint);
163 if (isVertical) {
164 drawRect(g2, y1, x1, y2, x2);
165 } else {
166 drawRect(g2, x1, y1, x2, y2);
167 }
168 }
169 }
170 }
171
172 }