| 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.FrequencyCounter; |
| 29 |
import dr.stats.Variate; |
| 30 |
|
| 31 |
import java.awt.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class IntegerViolinPlot extends 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 |
|
| 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 |
|
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public void setPaints(Paint barPaint, Paint intervalTailPaint) { |
| 115 |
this.barPaint = barPaint; |
| 116 |
this.intervalTailPaint = intervalTailPaint; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public void setIntervals(double lower, double upper) { |
| 123 |
hasIntervals = true; |
| 124 |
upperInterval = upper; |
| 125 |
lowerInterval = lower; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
} |