Debian Codegraph @

uk/ac/babraham/FastQC/Graphs/LineGraph.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Graphs;
21
22 import java.awt.BasicStroke;
23 import java.awt.Color;
24 import java.awt.Dimension;
25 import java.awt.Font;
26 import java.awt.Graphics;
27 import java.awt.Graphics2D;
28 import java.awt.RenderingHints;
29
30 import javax.swing.JPanel;
31
32 public class LineGraph extends JPanel {
33
34 private String [] xTitles;
35 private String xLabel;
36 private String [] xCategories;
37 private double [][] data;
38 private String graphTitle;
39 private double minY;
40 private double maxY;
41 private double yInterval;
42
43 // Colours taken from the Tol scheme at https://davidmathlogic.com/colorblind/
44 private static final Color [] COLOURS = new Color[] {
45 new Color(136,34,85),
46 new Color(51,34,136),
47 new Color(17,119,51),
48 new Color(221,204,119),
49 new Color(68,170,153),
50 new Color(170,68,153),
51 new Color(204,102,119),
52 new Color(136,204,238)
53 };
54
55
56 public LineGraph (double [] [] data, double minY, double maxY, String xLabel, String [] xTitles, int [] xCategories, String graphTitle) {
57 this(data,minY,maxY,xLabel,xTitles,new String[0],graphTitle);
58 this.xCategories = new String [xCategories.length];
59 for (int i=0;i<xCategories.length;i++) {
60 this.xCategories[i] = ""+xCategories[i];
61 }
62
63 }
64
65 public LineGraph (double [] [] data, double minY, double maxY, String xLabel, String [] xTitles, String [] xCategories, String graphTitle) {
66 this.data = data;
67 this.minY = minY;
68 this.maxY = maxY;
69 this.xTitles = xTitles;
70 this.xLabel = xLabel;
71 this.xCategories = xCategories;
72 this.graphTitle = graphTitle;
73 this.yInterval = findOptimalYInterval(maxY);
74 }
75
76 private double findOptimalYInterval(double max) {
77
78 int base = 1;
79 double [] divisions = new double [] {1,2,2.5,5};
80
81 while (true) {
82
83 for (int d=0;d<divisions.length;d++) {
84 double tester = base * divisions[d];
85 if (max / tester <= 10) {
86 return tester;
87 }
88 }
89
90 base *=10;
91
92 }
93
94
95
96 }
97
98 public Dimension getPreferredSize () {
99 return new Dimension(800,600);
100 }
101
102 public Dimension getMinimumSize () {
103 return new Dimension(100,200);
104 }
105
106 public void paint (Graphics g) {
107 super.paint(g);
108
109 g.setColor(Color.WHITE);
110 g.fillRect(0, 0, getWidth(), getHeight());
111 g.setColor(Color.BLACK);
112
113 int lastY = 0;
114
115 double yStart;
116
117 if (minY % yInterval == 0) {
118 yStart = minY;
119 }
120 else {
121 yStart = yInterval * (((int)minY/yInterval)+1);
122 }
123
124 int xOffset = 0;
125
126 for (double i=yStart;i<=maxY;i+=yInterval) {
127 String label = ""+i;
128 label = label.replaceAll(".0$", ""); // Don't leave trailing .0s where we don't need them.
129 int width = g.getFontMetrics().stringWidth(label);
130 if (width > xOffset) {
131 xOffset = width;
132 }
133
134 g.drawString(label, 2, getY(i)+(g.getFontMetrics().getAscent()/2));
135 }
136
137 // Give the x axis a bit of breathing space
138 xOffset += 5;
139
140 // Draw the graph title
141 int titleWidth = g.getFontMetrics().stringWidth(graphTitle);
142 g.drawString(graphTitle, (xOffset + ((getWidth()-(xOffset+10))/2)) - (titleWidth/2), 30);
143
144
145 // Now draw the axes
146 g.drawLine(xOffset, getHeight()-40, getWidth()-10,getHeight()-40);
147 g.drawLine(xOffset, getHeight()-40, xOffset, 40);
148
149 // Draw the xLabel under the xAxis
150 g.drawString(xLabel, (getWidth()/2) - (g.getFontMetrics().stringWidth(xLabel)/2), getHeight()-5);
151
152
153 // Now draw the data points
154 int baseWidth = (getWidth()-(xOffset+10))/Math.max(data[0].length,1); // Math.max is there in case we have no data (no sequences)
155 if (baseWidth<1) baseWidth=1;
156
157 // System.out.println("Base Width is "+baseWidth);
158
159 // First draw faint boxes over alternating bases so you can see which is which
160
161 // Let's find the longest label, and then work out how often we can draw labels
162
163 int lastXLabelEnd = 0;
164
165 for (int i=0;i<data[0].length;i++) {
166 if (i%2 != 0) {
167 g.setColor(new Color(230, 230, 230));
168 g.fillRect(xOffset+(baseWidth*i), 40, baseWidth, getHeight()-80);
169 }
170 g.setColor(Color.BLACK);
171 String baseNumber = ""+xCategories[i];
172 int baseNumberWidth = g.getFontMetrics().stringWidth(baseNumber);
173 int baseNumberPosition = (baseWidth/2)+xOffset+(baseWidth*i)-(baseNumberWidth/2);
174
175 if (baseNumberPosition > lastXLabelEnd) {
176 g.drawString(baseNumber,baseNumberPosition, getHeight()-25);
177 lastXLabelEnd = baseNumberPosition+baseNumberWidth+5;
178 }
179 }
180
181 // Now draw horizontal lines across from the y axis
182
183 g.setColor(new Color(180,180,180));
184 for (double i=yStart;i<=maxY;i+=yInterval) {
185 g.drawLine(xOffset, getY(i), getWidth()-10, getY(i));
186 }
187 g.setColor(Color.BLACK);
188
189 // Now draw the datasets
190
191 if (g instanceof Graphics2D) {
192 ((Graphics2D)g).setStroke(new BasicStroke(2));
193 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
194 }
195
196 for (int d=0;d<data.length;d++) {
197 g.setColor(COLOURS[d % COLOURS.length]);
198
199 if (data[d].length > 0)
200 lastY = getY(data[d][0]);
201 for (int i=1;i<data[d].length;i++) {
202 int thisY = getY(data[d][i]);
203 g.drawLine((baseWidth/2)+xOffset+(baseWidth*(i-1)), lastY, (baseWidth/2)+xOffset+(baseWidth*i), thisY);
204 lastY = thisY;
205 }
206
207 }
208
209 // Now draw the data legend
210
211 // Make the letters thicker for this so we can see the colours better
212 g.setFont(g.getFont().deriveFont(Font.BOLD));
213
214 if (g instanceof Graphics2D) {
215 ((Graphics2D)g).setStroke(new BasicStroke(1));
216 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
217 }
218
219
220 // First we need to find the widest label
221 int widestLabel = 0;
222 for (int t=0;t<xTitles.length;t++) {
223 int width = g.getFontMetrics().stringWidth(xTitles[t]);
224 if (width > widestLabel) widestLabel = width;
225 }
226
227 // Add 3px either side for a bit of space;
228 widestLabel += 6;
229
230 // First draw a box to put the legend in
231 g.setColor(Color.WHITE);
232 g.fillRect((getWidth()-10)-widestLabel, 40, widestLabel, 3+(20*xTitles.length));
233 g.setColor(Color.LIGHT_GRAY);
234 g.drawRect((getWidth()-10)-widestLabel, 40, widestLabel, 3+(20*xTitles.length));
235
236 // Now draw the actual labels
237 for (int t=0;t<xTitles.length;t++) {
238 g.setColor(COLOURS[t % COLOURS.length]);
239 g.drawString(xTitles[t], ((getWidth()-10)-widestLabel)+3, 35+(20*(t+1)));
240 }
241
242
243 // Put the font back how we found it
244 g.setFont(g.getFont().deriveFont(Font.PLAIN));
245
246
247 }
248
249 private int getY(double y) {
250 return (getHeight()-40) - (int)(((getHeight()-80)/(maxY-minY))*y);
251 }
252
253 }