Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * FrequencyPlot.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.TraceDistribution;
29 import dr.inference.trace.TraceType;
30 import dr.stats.FrequencyCounter;
31 import dr.stats.Variate;
32 import dr.util.FrequencyDistribution;
33
34 import java.awt.*;
35 import java.util.List;
36 import java.util.Map;
37
38 public class ColumnPlot extends Plot.AbstractPlot {
39
40 private Paint barPaint = new Color(124, 164, 221);
41 private Paint intervalTailPaint = new Color(232, 114, 103);
42
43 private boolean hasIntervals = false;
44 private double upperInterval = 0.0;
45 private double lowerInterval = 0.0;
46
47 private double columnWidth = 0.9;
48 private boolean columnsOnTicks = true;
49
50 public ColumnPlot(FrequencyCounter<Integer> frequencyCounter, List<Integer> order, boolean showFrequency) {
51 super();
52 setData(frequencyCounter, order, showFrequency);
53 }
54
55 public ColumnPlot(List<Double> xData, List<Double> yData) {
56 super();
57 setData(xData, yData);
58 }
59
60 public ColumnPlot(Variate.D xData, Variate.D yData) {
61 super();
62 setData(xData, yData);
63 }
64
65 private void setData(FrequencyCounter<Integer> frequencyCounter, List<Integer> order, boolean showFrequency) {
66 Variate.D xd = new Variate.D();
67 Variate.D yd = new Variate.D();
68
69 List<Integer> values = order;
70 if (values == null) {
71 values = frequencyCounter.getUniqueValues();
72 }
73
74 int i = 0;
75 for (int value : values) {
76 if (order == null) {
77 xd.add((double) value);
78 } else {
79 xd.add((double) i);
80 i++;
81 }
82 yd.add(showFrequency ? frequencyCounter.getFrequency(value) : frequencyCounter.getProbability(value));
83 }
84
85 setData(xd, yd);
86 }
87
88 public void setColumnWidth(double columnWidth) {
89 this.columnWidth = columnWidth;
90 }
91
92 /**
93 * Set paints
94 */
95 public void setPaints(Paint barPaint, Paint intervalTailPaint) {
96 this.barPaint = barPaint;
97 this.intervalTailPaint = intervalTailPaint;
98 }
99
100 /**
101 * Set arbitrary intervals to use (0 for none).
102 */
103 public void setIntervals(double lower, double upper) {
104 hasIntervals = true;
105 upperInterval = upper;
106 lowerInterval = lower;
107 }
108
109 /**
110 * Paint data series
111 */
112 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
113
114 double x, x1, y1, x2, y2;
115 int n = xData.getCount();
116
117 double spaceWidth = 1.0 * columnWidth;
118 double spaceDelta = (1 - spaceWidth) / 2;
119 double width = spaceWidth / getChart().getPlotCount();
120 double barWidth = width * columnWidth;
121 double delta = (width - barWidth) / 2;
122
123 g2.setStroke(lineStroke);
124 for (int i = 0; i < n; i ++) {
125
126 x = (Double)xData.get(i) + (getPlotNumber() * width) + spaceDelta;
127
128 x1 = x + delta;
129 y1 = 0.0;
130 x2 = x + width - delta;
131 y2 = (Double) yData.get(i);
132
133 if (columnsOnTicks) {
134 x1 -= 0.5;
135 x2 -= 0.5;
136 }
137
138 if (y1 != y2) {
139 if (barPaint != null) {
140 if (hasIntervals && (x < lowerInterval || x > upperInterval)) {
141 g2.setPaint(intervalTailPaint);
142 } else {
143 g2.setPaint(barPaint);
144 }
145 fillRect(g2, x1, y1, x2, y2);
146 }
147
148 if (lineStroke != null && linePaint != null) {
149 g2.setStroke(lineStroke);
150 g2.setPaint(linePaint);
151 drawRect(g2, x1, y1, x2, y2);
152 }
153 }
154 }
155 }
156
157 }