mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/pca/PlotViewer.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2004, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: PlotViewer.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.7 $
2 26 Feb 07 jari 8  * $Date: 2006/03/24 15:51:05 $
2 26 Feb 07 jari 9  * $Author: eleanorahowe $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.microarray.mev.cluster.gui.impl.pca;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import java.awt.Color;
2 26 Feb 07 jari 15 import java.awt.Font;
2 26 Feb 07 jari 16 import java.awt.FontMetrics;
2 26 Feb 07 jari 17 import java.awt.Graphics;
2 26 Feb 07 jari 18 import java.awt.Graphics2D;
2 26 Feb 07 jari 19 import java.beans.Expression;
2 26 Feb 07 jari 20
2 26 Feb 07 jari 21 import javax.swing.JComponent;
2 26 Feb 07 jari 22 import javax.swing.JPanel;
2 26 Feb 07 jari 23 import javax.swing.JTextArea;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 import org.tigr.microarray.mev.cluster.gui.impl.ViewerAdapter;
2 26 Feb 07 jari 26 import org.tigr.util.FloatMatrix;
2 26 Feb 07 jari 27
2 26 Feb 07 jari 28 public class PlotViewer extends ViewerAdapter implements java.io.Serializable {
2 26 Feb 07 jari 29     
2 26 Feb 07 jari 30     private JComponent content;
2 26 Feb 07 jari 31     FloatMatrix S;
2 26 Feb 07 jari 32     /**
2 26 Feb 07 jari 33      * Constructs a <code>PlotViewer</code> for specified S-matrix.
2 26 Feb 07 jari 34      */
2 26 Feb 07 jari 35     public PlotViewer(FloatMatrix S) {
2 26 Feb 07 jari 36   content = createContent(S);
2 26 Feb 07 jari 37       this.S = S;
2 26 Feb 07 jari 38     }
2 26 Feb 07 jari 39     /**
2 26 Feb 07 jari 40      * @inheritDoc
2 26 Feb 07 jari 41      */
2 26 Feb 07 jari 42     public Expression getExpression(){
2 26 Feb 07 jari 43       return new Expression(this, this.getClass(), "new", 
2 26 Feb 07 jari 44           new Object[]{S});
2 26 Feb 07 jari 45     }
2 26 Feb 07 jari 46     
2 26 Feb 07 jari 47     /**
2 26 Feb 07 jari 48      * Returns the viewer content.
2 26 Feb 07 jari 49      */
2 26 Feb 07 jari 50     public JComponent getContentComponent() {
2 26 Feb 07 jari 51   return content;
2 26 Feb 07 jari 52     }
2 26 Feb 07 jari 53     
2 26 Feb 07 jari 54     /**
2 26 Feb 07 jari 55      * Creates the viewer content.
2 26 Feb 07 jari 56      */
2 26 Feb 07 jari 57     private JComponent createContent(FloatMatrix S) {
2 26 Feb 07 jari 58   return new Plot(S);
2 26 Feb 07 jari 59     }
2 26 Feb 07 jari 60     
2 26 Feb 07 jari 61     /** Returns a component to be inserted into the scroll pane row header
2 26 Feb 07 jari 62      */
2 26 Feb 07 jari 63     public JComponent getRowHeaderComponent() {
2 26 Feb 07 jari 64         return null;
2 26 Feb 07 jari 65     }
2 26 Feb 07 jari 66     
2 26 Feb 07 jari 67     /**
2 26 Feb 07 jari 68      * The class to draw the viewer content.
2 26 Feb 07 jari 69      */
2 26 Feb 07 jari 70     private class Plot extends JPanel {
2 26 Feb 07 jari 71   
2 26 Feb 07 jari 72   private static final int left = 40;
2 26 Feb 07 jari 73   private static final int top  = 40;
2 26 Feb 07 jari 74   private FloatMatrix S;
2 26 Feb 07 jari 75   private Font font = new Font("monospaced", Font.BOLD, 10);
2 26 Feb 07 jari 76   
2 26 Feb 07 jari 77   /**
2 26 Feb 07 jari 78    * Constructs a <code>Plot</code> with specified S-matrix.
2 26 Feb 07 jari 79    */
2 26 Feb 07 jari 80   public Plot(FloatMatrix S) {
2 26 Feb 07 jari 81       setBackground(Color.white);
2 26 Feb 07 jari 82       setFont(font);
2 26 Feb 07 jari 83       this.S = S;
2 26 Feb 07 jari 84   }
2 26 Feb 07 jari 85   
2 26 Feb 07 jari 86   /**
2 26 Feb 07 jari 87    * Paints the viewer content into specified graphics.
2 26 Feb 07 jari 88    */
2 26 Feb 07 jari 89   public void paint(Graphics g) {
2 26 Feb 07 jari 90       super.paint(g);
2 26 Feb 07 jari 91       // drawing rectangle
2 26 Feb 07 jari 92       int plotWidth  = getWidth()  - 80;
2 26 Feb 07 jari 93       int plotHeight = getHeight() - 80;
2 26 Feb 07 jari 94       if (plotWidth < 5 || plotHeight < 5) {
2 26 Feb 07 jari 95     return;
2 26 Feb 07 jari 96       }
2 26 Feb 07 jari 97       g.setColor(Color.black);
2 26 Feb 07 jari 98       g.drawRect(left, top, plotWidth, plotHeight);
2 26 Feb 07 jari 99       
2 26 Feb 07 jari 100       double maxValue = S.get(0,0);
2 26 Feb 07 jari 101       if (Double.isNaN(maxValue)) {
2 26 Feb 07 jari 102     return;
2 26 Feb 07 jari 103       }
2 26 Feb 07 jari 104       int counter = 1;
2 26 Feb 07 jari 105       while (maxValue >= 10) {
2 26 Feb 07 jari 106     maxValue /= 10.0;
2 26 Feb 07 jari 107     counter *= 10;
2 26 Feb 07 jari 108       }
2 26 Feb 07 jari 109       int scale = ((int)Math.round(maxValue+0.5))*counter;
2 26 Feb 07 jari 110       int steps = ((int)Math.round(maxValue+0.5));
2 26 Feb 07 jari 111       // drawing left marks
2 26 Feb 07 jari 112       double stepY = plotHeight/(double)(steps);
2 26 Feb 07 jari 113       for (int i=1; i<steps; i++) {
2 26 Feb 07 jari 114     g.drawLine(left, top + (int)Math.round(i*stepY), left+5, top+(int)Math.round(i*stepY));
2 26 Feb 07 jari 115       }
2 26 Feb 07 jari 116       // drawing right marks
2 26 Feb 07 jari 117       double stepX = plotWidth/(S.getColumnDimension()-1.0);
2 26 Feb 07 jari 118       for (int i=1; i<S.getColumnDimension(); i++) {
2 26 Feb 07 jari 119     g.drawLine(left+(int)Math.round(i*stepX), top+plotHeight-5, left+(int)Math.round(i*stepX), top+plotHeight);
2 26 Feb 07 jari 120       }
2 26 Feb 07 jari 121       g.setColor(Color.magenta);
2 26 Feb 07 jari 122       double factor = (double)plotHeight/(double)(scale);
2 26 Feb 07 jari 123       int prevValue = -(int)Math.round(S.get(0,0)*factor);
2 26 Feb 07 jari 124       int curValue;
2 26 Feb 07 jari 125       int zeroValue = top+plotHeight;
2 26 Feb 07 jari 126       // draw chart
2 26 Feb 07 jari 127       for (int i=1; i<S.getColumnDimension(); i++) {
2 26 Feb 07 jari 128     curValue = -(int)Math.round(S.get(i, i)*factor);
2 26 Feb 07 jari 129     g.drawLine(left+(int)Math.round((i-1)*stepX), zeroValue+prevValue, left+(int)Math.round(i*stepX), zeroValue+curValue);
2 26 Feb 07 jari 130     prevValue = curValue;
2 26 Feb 07 jari 131       }
2 26 Feb 07 jari 132       // draw points
2 26 Feb 07 jari 133       g.setColor(new Color(0,0,128));
2 26 Feb 07 jari 134       for (int i=0; i<S.getColumnDimension(); i++) {
2 26 Feb 07 jari 135     curValue=-(int)Math.round(S.get(i,i)*factor);
2 26 Feb 07 jari 136     g.fillOval(left+(int)Math.round(i*stepX)-3, zeroValue+curValue-3, 6, 6);
2 26 Feb 07 jari 137       }
2 26 Feb 07 jari 138       // draw labels
2 26 Feb 07 jari 139       int width;
2 26 Feb 07 jari 140       String str;
2 26 Feb 07 jari 141       FontMetrics metrics = g.getFontMetrics();
2 26 Feb 07 jari 142       for (int i=0; i <= steps; i++) {
2 26 Feb 07 jari 143     str = String.valueOf(scale-counter*i);
2 26 Feb 07 jari 144     width  = metrics.stringWidth(str);
2 26 Feb 07 jari 145     g.drawString(str, left-10-width, top+(int)Math.round(i*stepY)+5);
2 26 Feb 07 jari 146       }
2 26 Feb 07 jari 147       ((Graphics2D)g).rotate(-Math.PI/2.0);
2 26 Feb 07 jari 148       for (int i=0; i<S.getColumnDimension(); i++) {
2 26 Feb 07 jari 149     str = String.valueOf(i+1);
2 26 Feb 07 jari 150     width  = metrics.stringWidth(str);
2 26 Feb 07 jari 151     g.drawString(str,-top-plotHeight-10-width, left+5+(int)Math.round(i*stepX));
2 26 Feb 07 jari 152       }
2 26 Feb 07 jari 153       ((Graphics2D)g).rotate(Math.PI/2.0);
2 26 Feb 07 jari 154   }
2 26 Feb 07 jari 155     }
2 26 Feb 07 jari 156 }