Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: joist/java/org/joist/util/graph HTMLGraph.java

Project highlights: Architectural Overview

joist
Discussion topic

Back to topic list

CVS update: joist/java/org/joist/util/graph HTMLGraph.java

Author commitlogger at hocus dot collab dot net
Full name commitlogger at hocus dot collab dot net
Date 2000-04-02 21:14:55 PDT
Message User: dlr
  Date: 00/04/02 21:14:55

  Added: java/org/joist/util ColorScheme.java Orientation.java
               java/org/joist/util/graph HTMLGraph.java
  Log:
  First draft of an HTML-based graphing system.
  
  Revision Changes Path
  1.1 joist/java/org/joist​/util/ColorScheme.ja​va
  
  Index: ColorScheme.java
  ====================​====================​====================​=======
  package org.joist.util;
  
  import java.io.Serializable;
  import java.awt.Color;
  
  /**
   * Short Description (1 sentence).
   * Long Description (in HTML).
   * @author <a href="mailto:dlr@col​lab.net">Daniel Rall</a>
   * @version 1.0 (2000.04.2)
   */
  public class ColorScheme implements Serializable
  {
      /** Foreground color for this scheme. Defaults to black. */
      protected Color foreground;
  
      /** Background color for this scheme. Defaults to white. */
      protected Color background;
  
      /**
       * Default constructor.
       * Creates a new ColorScheme with default properties.
       */
      public ColorScheme ()
      {
      init(null, null);
      }
  
      /**
       * Constructor for specifying foreground and background colors.
       */
      public ColorScheme (Color foreground, Color background)
      {
      init(foreground, background);
      }
  
      /**
       * Properly initiliazes instance fields.
       */
      private void init (Color foreground, Color background)
      {
      this.foreground = (foreground == null ? Color.black : foreground);
      this.background = (background == null ? Color.white : background);
      }
  
      /**
       * Gets the foreground color as an object.
       *
       * @return The foreground color.
       */
      public Color getForeground () { return foreground; }
  
      /**
       * Gets the background color as an object.
       *
       * @return The background color.
       */
      public Color getBackground () { return background; }
  }
  
  
  
  
  1.1 joist/java/org/joist​/util/Orientation.ja​va
  
  Index: Orientation.java
  ====================​====================​====================​=======
  package org.joist.util;
  
  /**
   * Generic orientation description.
   *
   * @author Daniel L. Rall
   * @version 1.0 (2000.04. 2)
   */
  public class Orientation
  {
      public static final int DEFAULT = 1;
      public static final int VERTICAL = 1;
      public static final int HORIZONTAL = 2;
  
      /*
      public static final int DIM_DEF = 0;
      public static final int DIM_2D = 1;
      public static final int DIM_3D = 2;
      */
  
      /** The spacial orientation (i.e. VERTICAL, HORIZONTAL, etc.) */
      private int spacial;
  
      /**
       * Default constructor.
       * Creates a new Orientation with default properties.
       */
      public Orientation ()
      {
      spacial = DEFAULT;
      }
  
      /**
       * Default constructor.
       * Creates a new Orientation with default properties.
       */
      public Orientation (int spacial)
      {
      this.spacial = spacial;
      }
  
      /**
       * Gets the spacial orientation.
       *
       * @return The spacial orientation.
       */
      public int getSpacial () { return spacial; }
  }
  
  
  
  
  1.1 joist/java/org/joist​/util/graph/HTMLGrap​h.java
  
  Index: HTMLGraph.java
  ====================​====================​====================​=======
  /* HTMLGraph.java: -*- Java -*- */
  
  package org.joist.util.graph;
  
  import java.io.Serializable;
  import java.util.Hashtable;
  import java.util.Enumeration;
  
  import org.webmacro.util.*;
  
  import org.joist.util.Orientation;
  import org.joist.util.ColorScheme;
  
  /**
   * Generates HTML-based graphs.
   * Long Description (in HTML).
   * @author Daniel L. Rall
   * @version 1.0 (2000.04.2)
   */
  public class HTMLGraph implements Serializable
  {
      /** Default graph orientation. */
      protected static Orientation defaultOrientation = new Orientation();
  
      /** Default graph color scheme. */
      protected static ColorScheme defaultColorScheme = new ColorScheme();
  
      /** Logging mechanism. */
      private static Log log = new Log("htmGrph", "HTMLGraph");
  
      /**
       * Default constructor.
       * Creates a new HTMLGraph with default properties.
       */
      private HTMLGraph ()
      {
      }
  
      /**
       * Acts as a factory to create graphs as an HTML table.
       *
       * @param orientation
       * @param data
       * @return The graph as HTML.
       */
      public static String create (Hashtable data,
                   String title,
                   Orientation orientation,
                   ColorScheme colorScheme)
      {
      String graph = null;
  
      if (data != null)
      {
          StringBuffer buf = new StringBuffer();
  
          // Start the graph.
          buf.append("<table>");
  
          // Set the graph title.
          if (title != null && title.length() > 0)
          {
          buf.append("<tr><td align=\"center\" colspan=\"2\"><b>");
          buf.append(title);
          buf.append("</b​></td></tr​>");
          }
  
          // Make sure we have graph meta data.
          if (orientation == null) orientation = defaultOrientation;
          if (colorScheme == null) colorScheme = defaultColorScheme;
  
          // Iterate data set, adding header and data.
          for (Enumeration keys = data.keys(); keys.hasMoreElements(); )
          {
          Object key = keys.nextElement();
          Object value = data.get(key);
  
          buf.append("<tr>");
  
          // Crunch numbers.
          int numeric = 0;
          try { numeric = Integer.parseInt(val​ue.toString()); }
          catch (NumberFormatException e) { log.exception(e); }
          // TODO: Use a scale factor.
  
          // Generate graph layout based on orientation.
          switch (orientation.getSpacial())
          {
          case Orientation.VERTICAL:
              // TODO: Implement
              log.debug("create: VERTICAL orientation not yet " +
                    "implemented");
              break;
  
          case Orientation.HORIZONTAL:
              // Data header.
              buf.append("<td align=\"right\" valign=\"center\"​><b>");
              buf.append(key.toString());
              buf.append("</b​></td>");
  
              // Data display.
              buf.append("<td align=\"left\" valign=\"center\">");
              buf.append("<tabl​e><tr><t​d width=\"");
              buf.append(numeric).append("\" color=\"#");
              buf.append(getWebCol​or(colorScheme.getBa​ckground()));
              buf.append("\" bgcolor=\"#");
              buf.append(getWebCol​or(colorScheme.getFo​reground()));
              buf.append("\">​&nbsp;</td>​<td>&nbsp;​");
              buf.append(numeric).​append("</td>​</tr></table​>");
              buf.append("</td>");
              break;
  
          default:
              log.debug("create: Unhandled orientation");
              break;
          }
  
          buf.append("</tr>");
          }
  
          // End the graph.
          buf.append("</table>");
  
          graph = buf.toString();
      }
  
      return graph;
      }
  
      /**
       * Gets the red green blue integer representation of a color as hex.
       *
       * @param c Color to transform into hex.
       * @return Hex value of a color as a string.
       */
      protected static String getWebColor (java.awt.Color c)
      {
      // Integer.toHexString() returns an 8 digit number. Web browsers seem
      // to like 6 digits. Dropping the first two renders the desired color
      // in a browser.
      return Integer.toHexString(​c.getRGB()).substrin​g(2);
      }
  }

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

CVS update: joist/java/org/joist/util/graph HTMLGraph.java commitlogger at hocus dot collab dot net commitlogger at hocus dot collab dot net 2000-04-02 21:14:55 PDT
Messages per page: