User: juha    
  Date: 00/12/29 14:36:25

  Modified:    src/org/jboss/admin/monitor/graph DefaultGraphRenderer.java
  Log:
  scale the graphs to fit the view area
  
  Revision  Changes    Path
  1.2       +95 -6     
admin/src/org/jboss/admin/monitor/graph/DefaultGraphRenderer.java
  
  Index: DefaultGraphRenderer.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/admin/src/org/jboss/admin/monitor/graph/DefaultGraphRenderer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultGraphRenderer.java 2000/12/17 20:11:23     1.1
  +++ DefaultGraphRenderer.java 2000/12/29 22:36:25     1.2
  @@ -10,7 +10,10 @@
   
   import java.util.ArrayList;
   import java.util.Collections;
  +import java.util.Comparator;
   import java.util.Iterator;
  +import java.util.NoSuchElementException;
  +import java.io.Serializable;
   
   import javax.swing.JPanel;
   
  @@ -29,14 +32,25 @@
   
           // superclass JComponent implements Serializable
   
  +    /** Horizontal "step" on the grid between plot points, in pixels */
       private int horzPlotStep    = 4;
       
  +    /** Gap between the vertical grid lines, in pixels */
       private int gridGapWidth    = 10;
  +    /** Gap between the horizontal grid lines, in pixels */
       private int gridGapHeight   = 10;
  -
  +    
  +    /** The color used for drawing the background grid */
       private Color gridLineColor = new Color(0x007700);
  +    /** The color used for drawing the plot lines */
       private Color plotColor     = Color.green;
        
  +    /**
  +     * The current maximum value plotted on the grid. This value is the
  +     * non-scaled value of a plot point at the top of the grid.
  +     */
  +    private double max          = 1.0d;
  +
       private ArrayList points    = new ArrayList();
              
                   
  @@ -67,18 +81,36 @@
    */
   
       public Component getGraphRendererObject(GraphView graph) {
  -
  -        points = new ArrayList(graph.getModel().getPlotPoints());
   
  -        Collections.reverse(points);
  +        try {
  +            points = new ArrayList(graph.getModel().getPlotPoints());
  +            
  +            Point maxPoint = (Point)Collections.max(points, new PointComparator());
  +            
  +            if (!isWithinScale(maxPoint.getY()))
  +                rescale(maxPoint.getY());
  +            
  +            Collections.reverse(points);
  +        }
  +        catch (NoSuchElementException ignored) {
  +            // collection was empty, just ignore
  +        }
  +        catch (ClassCastException e) {
  +            System.err.println(e);
  +            e.printStackTrace();
  +        }
           
           return this;
       }
   
       public void valueAppended(GraphModelEvent evt) {   
   
  -        points.add(0, new Point(points.size(), (int)evt.getValue()));
  +        double value = evt.getValue();
  +        points.add(0, new Point(points.size(), (int)value));
   
  +        if (!isWithinScale(value))
  +            rescale(value);
  +            
           repaint();
       }
       
  @@ -90,6 +122,13 @@
    *************************************************************************
    */
    
  +    public void addNotify() {
  +        super.addNotify();
  +        
  +        //Insets insets = getInsets();
  +        //this.max = getHeight() - insets.top - insets.bottom;
  +    }
  +    
       public void paintComponent(Graphics g) {
           
           // draw background first
  @@ -200,9 +239,59 @@
           
           while ((it.hasNext()) && (x >= 0)) {
               Point p = (Point)it.next();
  -            g.drawLine(x, y, (x -= horzPlotStep), (y = height - p.y));
  +            g.drawLine(x, y, (x -= horzPlotStep), (y = height - (int)(p.y * height 
/ max)));
               --x;
           }
       }
  +    
  +    
  +    private boolean isWithinScale(Double value) {
  +        return isWithinScale(value.doubleValue());
  +    }
  +    
  +    private boolean isWithinScale(double value) {
  +    
  +        if (value < 0.9 * max)
  +            return true;
  +            
  +        return false;
  +    }
  +    
  +    private void rescale(double value) {
  +        
  +        max     = 1.4 * value;
  +        
  +    }
  +    
  +/*
  + *************************************************************************
  + *
  + *      INNER CLASSES
  + *
  + *************************************************************************
  + */
  + 
  +    private class PointComparator implements Comparator, Serializable {
  +     
  +        /**
  +         * ...
  +         *
  +         * @exception ClassCastException - if the arguments' types prevent them
  +         *            from being compared by this Comparator.
  +         */
  +        public int compare(Object a, Object b) {
  +            Point pa = (Point)a;
  +            Point pb = (Point)b;
  +
  +            if (pa.getY() == pb.getY())
  +                return 0;
  +                
  +            else if (pa.getY() < pb.getY())
  +                return -1;
  +                
  +            else return 1;
  +        }
  +    }
  +    
   }
   
  
  
  

Reply via email to