MOA 12.03
Real Time Analytics for Data Streams
GraphAxes.java
Go to the documentation of this file.
00001 /*
00002  *    GraphAxes.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen ([email protected])
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 
00021 package moa.gui.visualization;
00022 
00023 import java.awt.Color;
00024 import java.awt.Font;
00025 import java.awt.Graphics;
00026 import java.text.DecimalFormat;
00027 
00028 public class GraphAxes extends javax.swing.JPanel {
00029     private final int x_offset_left = 35;
00030     private final int x_offset_right = 5;
00031     private final int y_offset_bottom = 20;
00032     private final int y_offset_top = 20;
00033 
00034     private int height;
00035     private int width;
00036 
00037     private double x_resolution; //how many pixels per 1px
00038     private int processFrequency;
00039 
00040     private double min_value = 0;
00041     private double max_value = 1;
00042     private int max_x_value;
00043 
00045     public GraphAxes() {
00046         initComponents();
00047     }
00048 
00049     public void setXMaxValue(int max) {
00050         max_x_value = max;
00051     }
00052 
00053     public void setXResolution(double resolution){
00054         x_resolution = resolution;
00055     }
00056 
00057     public void setProcessFrequency(int frequency){
00058         processFrequency = frequency;
00059     }
00060 
00061     @Override
00062     protected void paintComponent(Graphics g) {
00063         super.paintComponent(g);
00064 
00065         //stream not started yet
00066         if(processFrequency == 0) return;
00067 
00068         height = getHeight()-y_offset_bottom-y_offset_top;
00069         width = getWidth()-x_offset_left-x_offset_right;
00070 
00071         //System.out.println(width);
00072 
00073         g.setColor(new Color(236,233,216));
00074         g.fillRect(0, 0, getWidth(), getHeight());
00075 
00076         //draw background
00077         g.setColor(Color.WHITE);
00078         g.fillRect(x_offset_left, y_offset_top, width, height);
00079 
00080         g.setFont(new Font("Tahoma", 0, 11));
00081         
00082 
00083         xAxis(g);
00084         yAxis(g);
00085     }
00086 
00087     private void xAxis(Graphics g){
00088         g.setColor(Color.BLACK);
00089         
00090         //x-achsis
00091         g.drawLine(x_offset_left, calcY(0), width+x_offset_left, calcY(0));
00092 
00093         //x achsis labels
00094         int w = 100;
00095         for (int i = 0; w*i < width-x_offset_right; i++) {
00096             g.drawLine(w*i+x_offset_left, height+y_offset_top, w*i+x_offset_left, height+y_offset_top+5);
00097 
00098             String label = Integer.toString((int)(w*i*processFrequency*x_resolution));
00099 
00100             int str_length = g.getFontMetrics().stringWidth(label);
00101             g.drawString(label,w*i+x_offset_left-str_length/2,height+y_offset_top+18);
00102         }
00103     }
00104 
00105 
00106     private void yAxis(Graphics g){
00107         //y-achsis
00108         g.setColor(Color.BLACK);
00109         g.drawLine(x_offset_left, calcY(0), x_offset_left, y_offset_top);
00110 
00111         //center horizontal line
00112         g.setColor(new Color(220,220,220));
00113         g.drawLine(x_offset_left, height/2+y_offset_top, getWidth(), height/2+y_offset_top);
00114 
00115         //3 y-achsis markers + labels
00116         g.setColor(Color.BLACK);
00117         DecimalFormat d = new DecimalFormat("0.00");
00118         int digits_y = (int)(Math.log10(max_value))-1;
00119         double upper = Math.ceil(max_value/Math.pow(10,digits_y));
00120         if(digits_y < 0) upper*=Math.pow(10,digits_y);
00121 
00122         if(Double.isNaN(upper)) upper =1.0;
00123 
00124         g.drawString(d.format(0.0), 3, height+y_offset_top+5);
00125         g.drawString(d.format(upper/2), 3, height/2+y_offset_top + 5);
00126         g.drawString(d.format(upper), 3, y_offset_top + 5);
00127         g.drawLine(x_offset_left-5, height+y_offset_top, x_offset_left,height+y_offset_top);
00128         g.drawLine(x_offset_left-5, height/2+y_offset_top, x_offset_left,height/2+y_offset_top);
00129         g.drawLine(x_offset_left-5, y_offset_top, x_offset_left,y_offset_top);
00130 
00131     }
00132 
00133     public void setYMinMaxValues(double min, double max){
00134         min_value = min;
00135         max_value = max;
00136     }
00137 
00138     public void setMaxXValue(int max){
00139         max_x_value = max;
00140     }
00141 
00142     private int calcY(double value){
00143         return (int)(height-(value/max_value)*height)+y_offset_top;
00144     }
00145 
00151     @SuppressWarnings("unchecked")
00152     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
00153     private void initComponents() {
00154 
00155         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
00156         this.setLayout(layout);
00157         layout.setHorizontalGroup(
00158             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00159             .addGap(0, 400, Short.MAX_VALUE)
00160         );
00161         layout.setVerticalGroup(
00162             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00163             .addGap(0, 300, Short.MAX_VALUE)
00164         );
00165     }// </editor-fold>//GEN-END:initComponents
00166 
00167 
00168 
00169 
00170     // Variables declaration - do not modify//GEN-BEGIN:variables
00171     // End of variables declaration//GEN-END:variables
00172 
00173 }
 All Classes Namespaces Files Functions Variables Enumerations