/** * Copyright Copyright 2010-17 Simon Andrews * * This file is part of FastQC. * * FastQC is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * FastQC is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with FastQC; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ package uk.ac.babraham.FastQC.Graphs; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JPanel; public class LineGraph extends JPanel { private String [] xTitles; private String xLabel; private String [] xCategories; private double [][] data; private String graphTitle; private double minY; private double maxY; private double yInterval; // Colours taken from the Tol scheme at https://davidmathlogic.com/colorblind/ private static final Color [] COLOURS = new Color[] { new Color(136,34,85), new Color(51,34,136), new Color(17,119,51), new Color(221,204,119), new Color(68,170,153), new Color(170,68,153), new Color(204,102,119), new Color(136,204,238) }; public LineGraph (double [] [] data, double minY, double maxY, String xLabel, String [] xTitles, int [] xCategories, String graphTitle) { this(data,minY,maxY,xLabel,xTitles,new String[0],graphTitle); this.xCategories = new String [xCategories.length]; for (int i=0;i xOffset) { xOffset = width; } g.drawString(label, 2, getY(i)+(g.getFontMetrics().getAscent()/2)); } // Give the x axis a bit of breathing space xOffset += 5; // Draw the graph title int titleWidth = g.getFontMetrics().stringWidth(graphTitle); g.drawString(graphTitle, (xOffset + ((getWidth()-(xOffset+10))/2)) - (titleWidth/2), 30); // Now draw the axes g.drawLine(xOffset, getHeight()-40, getWidth()-10,getHeight()-40); g.drawLine(xOffset, getHeight()-40, xOffset, 40); // Draw the xLabel under the xAxis g.drawString(xLabel, (getWidth()/2) - (g.getFontMetrics().stringWidth(xLabel)/2), getHeight()-5); // Now draw the data points int baseWidth = (getWidth()-(xOffset+10))/Math.max(data[0].length,1); // Math.max is there in case we have no data (no sequences) if (baseWidth<1) baseWidth=1; // System.out.println("Base Width is "+baseWidth); // First draw faint boxes over alternating bases so you can see which is which // Let's find the longest label, and then work out how often we can draw labels int lastXLabelEnd = 0; for (int i=0;i lastXLabelEnd) { g.drawString(baseNumber,baseNumberPosition, getHeight()-25); lastXLabelEnd = baseNumberPosition+baseNumberWidth+5; } } // Now draw horizontal lines across from the y axis g.setColor(new Color(180,180,180)); for (double i=yStart;i<=maxY;i+=yInterval) { g.drawLine(xOffset, getY(i), getWidth()-10, getY(i)); } g.setColor(Color.BLACK); // Now draw the datasets if (g instanceof Graphics2D) { ((Graphics2D)g).setStroke(new BasicStroke(2)); ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } for (int d=0;d 0) lastY = getY(data[d][0]); for (int i=1;i widestLabel) widestLabel = width; } // Add 3px either side for a bit of space; widestLabel += 6; // First draw a box to put the legend in g.setColor(Color.WHITE); g.fillRect((getWidth()-10)-widestLabel, 40, widestLabel, 3+(20*xTitles.length)); g.setColor(Color.LIGHT_GRAY); g.drawRect((getWidth()-10)-widestLabel, 40, widestLabel, 3+(20*xTitles.length)); // Now draw the actual labels for (int t=0;t