home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Webroot / classes / CFGraphs / linechart.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  9.1 KB  |  508 lines

  1. import allaire.dcf.recordset.AppletParamRecordset;
  2. import allaire.dcf.recordset.Query;
  3. import allaire.dcf.recordset.Recordset;
  4. import allaire.util.Debug;
  5. import java.applet.Applet;
  6. import java.awt.Color;
  7. import java.awt.Component;
  8. import java.awt.Event;
  9. import java.awt.Font;
  10. import java.awt.FontMetrics;
  11. import java.awt.Graphics;
  12. import java.util.Date;
  13. import java.util.Vector;
  14.  
  15. public class LineChart extends Applet implements Runnable {
  16.    static final int PARAMS = 1;
  17.    static final int TEMPLATE = 2;
  18.    static final Color[] defaultColors;
  19.    Thread runner;
  20.    Query query;
  21.    int refreshTime;
  22.    boolean debugInfoEnabled;
  23.    String title;
  24.    String titleFontName;
  25.    Font titleFont;
  26.    FontMetrics titleFontMetrics;
  27.    int titleFontHeight;
  28.    String legendFontName;
  29.    Font legendFont;
  30.    FontMetrics legendFontMetrics;
  31.    int legendFontHeight;
  32.    int legendHeight;
  33.    int legendWidth;
  34.    boolean legendEnabled;
  35.    int dotSize;
  36.    boolean barBorderEnabled;
  37.    boolean showDateTime;
  38.    boolean isCumulative;
  39.    String currentDateTime;
  40.    Color backgroundColor;
  41.    Color fontColor;
  42.    Color lineColor;
  43.    Vector items;
  44.    Vector pointerToItems;
  45.    Recordset chartData;
  46.    int rowCount;
  47.    int groupCount;
  48.    int itemCount;
  49.    int maxItemLabelWidth;
  50.    int maxGroupLabelWidth;
  51.    double maxValue;
  52.    double minValue;
  53.  
  54.    public void start() {
  55.       if (this.runner == null) {
  56.          this.runner = new Thread(this);
  57.          this.runner.start();
  58.       }
  59.  
  60.    }
  61.  
  62.    public void stop() {
  63.       if (this.runner != null) {
  64.          this.runner.stop();
  65.          this.runner = null;
  66.       }
  67.  
  68.    }
  69.  
  70.    private void getData(int var1) {
  71.       try {
  72.          switch (var1) {
  73.             case 1:
  74.                this.chartData = new AppletParamRecordset(this, "ChartData");
  75.                break;
  76.             case 2:
  77.                this.query.execute();
  78.                this.chartData = this.query.getRecordset();
  79.          }
  80.  
  81.          if (this.refreshTime == 0) {
  82.             ((Applet)this).getAppletContext().showStatus("Done");
  83.          } else {
  84.             ((Applet)this).getAppletContext().showStatus("...");
  85.          }
  86.  
  87.          this.rowCount = this.chartData.getRowCount();
  88.          if (this.rowCount != 0) {
  89.             for(int var2 = 0; var2 < this.rowCount; ++var2) {
  90.                double var5;
  91.                try {
  92.                   var5 = new Double(this.chartData.getData(var2 + 1, "Values"));
  93.                } catch (Exception var9) {
  94.                   var5 = (double)0.0F;
  95.                }
  96.  
  97.                if (var2 == 0) {
  98.                   this.groupCount = this.itemCount = 1;
  99.                   this.pointerToItems.setSize(this.rowCount);
  100.                   this.pointerToItems.setElementAt(new Integer(this.itemCount - 1), var2);
  101.                   this.items.setSize(this.itemCount);
  102.                   this.items.setElementAt(new Item(this.chartData.getData(var2 + 1, "Items")), this.itemCount - 1);
  103.                   ((Item)this.items.elementAt(this.itemCount - 1)).maxValue = var5;
  104.                   ((Item)this.items.elementAt(this.itemCount - 1)).minValue = var5;
  105.                   ((Item)this.items.elementAt(this.itemCount - 1)).value = var5;
  106.                   this.maxItemLabelWidth = this.legendFontMetrics.stringWidth(this.chartData.getData(var2 + 1, "Items"));
  107.                   this.maxGroupLabelWidth = this.legendFontMetrics.stringWidth(this.chartData.getData(var2 + 1, "Groups"));
  108.                } else {
  109.                   if (!this.chartData.getData(var2 + 1, "Groups").equals(this.chartData.getData(var2, "Groups"))) {
  110.                      this.maxGroupLabelWidth = Math.max(this.legendFontMetrics.stringWidth(this.chartData.getData(var2 + 1, "Groups")), this.maxGroupLabelWidth);
  111.                      ++this.groupCount;
  112.                   }
  113.  
  114.                   boolean var4 = false;
  115.  
  116.                   for(int var3 = 0; var3 < var2; ++var3) {
  117.                      if (this.chartData.getData(var2 + 1, "Items").equals(this.chartData.getData(var3 + 1, "Items"))) {
  118.                         var4 = true;
  119.                         this.pointerToItems.setElementAt(this.pointerToItems.elementAt(var3), var2);
  120.                         int var7 = (Integer)this.pointerToItems.elementAt(var3);
  121.                         if (!this.isCumulative) {
  122.                            ((Item)this.items.elementAt(var7)).maxValue = Math.max(var5, ((Item)this.items.elementAt(var7)).maxValue);
  123.                            ((Item)this.items.elementAt(var7)).minValue = Math.min(var5, ((Item)this.items.elementAt(var7)).minValue);
  124.                         } else {
  125.                            Item var10000 = (Item)this.items.elementAt(var7);
  126.                            var10000.value += var5;
  127.                            ((Item)this.items.elementAt(var7)).maxValue = Math.max(((Item)this.items.elementAt(var7)).maxValue, ((Item)this.items.elementAt(var7)).value);
  128.                            ((Item)this.items.elementAt(var7)).minValue = Math.min(((Item)this.items.elementAt(var7)).minValue, ((Item)this.items.elementAt(var7)).value);
  129.                         }
  130.                         break;
  131.                      }
  132.                   }
  133.  
  134.                   if (!var4) {
  135.                      ++this.itemCount;
  136.                      this.pointerToItems.setElementAt(new Integer(this.itemCount - 1), var2);
  137.                      this.items.setSize(this.itemCount);
  138.                      this.items.setElementAt(new Item(this.chartData.getData(var2 + 1, "Items")), this.itemCount - 1);
  139.                      ((Item)this.items.elementAt(this.itemCount - 1)).maxValue = var5;
  140.                      ((Item)this.items.elementAt(this.itemCount - 1)).minValue = var5;
  141.                      ((Item)this.items.elementAt(this.itemCount - 1)).value = var5;
  142.                      this.maxItemLabelWidth = Math.max(this.legendFontMetrics.stringWidth(this.chartData.getData(var2 + 1, "Items")), this.maxItemLabelWidth);
  143.                   }
  144.                }
  145.             }
  146.  
  147.             this.maxValue = (double)0.0F;
  148.             this.minValue = (double)0.0F;
  149.  
  150.             for(int var12 = 0; var12 < this.itemCount; ++var12) {
  151.                this.maxValue = Math.max(((Item)this.items.elementAt(var12)).maxValue, this.maxValue);
  152.                this.minValue = Math.min(((Item)this.items.elementAt(var12)).minValue, this.minValue);
  153.             }
  154.  
  155.             for(int var11 = 0; var11 < this.itemCount; ++var11) {
  156.                if (var11 < 9) {
  157.                   ((Item)this.items.elementAt(var11)).color = defaultColors[var11];
  158.                } else if (var11 < 18) {
  159.                   ((Item)this.items.elementAt(var11)).color = defaultColors[var11 - 9].darker();
  160.                } else if (var11 < 27) {
  161.                   ((Item)this.items.elementAt(var11)).color = defaultColors[var11 - 18].darker().darker();
  162.                } else {
  163.                   ((Item)this.items.elementAt(var11)).color = Color.black;
  164.                }
  165.             }
  166.          }
  167.  
  168.          this.currentDateTime = (new Date()).toLocaleString();
  169.       } catch (Exception var10) {
  170.          if (this.debugInfoEnabled) {
  171.             Debug.write(((Throwable)var10).getMessage());
  172.          }
  173.  
  174.       }
  175.    }
  176.  
  177.    public boolean mouseExit(Event var1, int var2, int var3) {
  178.       if (var1.id == 505) {
  179.          ((Applet)this).getAppletContext().showStatus("");
  180.          return true;
  181.       } else {
  182.          return false;
  183.       }
  184.    }
  185.  
  186.    static {
  187.       defaultColors = new Color[]{Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.cyan, Color.orange, Color.pink, Color.darkGray};
  188.    }
  189.  
  190.    private void pause(int var1) {
  191.       try {
  192.          Thread.sleep((long)var1);
  193.       } catch (InterruptedException var4) {
  194.          if (this.debugInfoEnabled) {
  195.             Debug.write(((Throwable)var4).getMessage());
  196.          }
  197.  
  198.       }
  199.    }
  200.  
  201.    public boolean mouseEnter(Event var1, int var2, int var3) {
  202.       if (var1.id == 504) {
  203.          ((Applet)this).getAppletContext().showStatus("(c)1996 Allaire Corp.");
  204.          return true;
  205.       } else {
  206.          return false;
  207.       }
  208.    }
  209.  
  210.    public void run() {
  211.       while(this.refreshTime != 0) {
  212.          this.pause(this.refreshTime * 1000);
  213.          this.getData(2);
  214.          ((Component)this).repaint();
  215.       }
  216.  
  217.       this.stop();
  218.    }
  219.  
  220.    public synchronized void init() {
  221.       String var1 = ((Applet)this).getParameter("RefreshTime");
  222.       if (var1 == null) {
  223.          this.refreshTime = 0;
  224.       } else {
  225.          this.refreshTime = Integer.parseInt(var1);
  226.          var1 = ((Applet)this).getParameter("RefreshDataFromURL");
  227.          if (var1 == null) {
  228.             this.refreshTime = 0;
  229.          } else {
  230.             if (var1.indexOf(63) == -1) {
  231.                var1 = var1 + '?';
  232.             }
  233.  
  234.             this.query = new Query(var1);
  235.             this.query.addParam("RefreshTime", String.valueOf(this.refreshTime));
  236.          }
  237.       }
  238.  
  239.       var1 = ((Applet)this).getParameter("DebugInfoEnabled");
  240.       if (var1 == null) {
  241.          this.debugInfoEnabled = false;
  242.       } else if (var1.toLowerCase().equals("yes")) {
  243.          this.debugInfoEnabled = true;
  244.       } else {
  245.          this.debugInfoEnabled = false;
  246.       }
  247.  
  248.       this.title = ((Applet)this).getParameter("Title");
  249.       if (this.title == null) {
  250.          this.title = "Chart";
  251.       }
  252.  
  253.       this.titleFontName = ((Applet)this).getParameter("TitleFontName");
  254.       if (this.titleFontName == null) {
  255.          this.titleFontName = "TimesRoman";
  256.       }
  257.  
  258.       var1 = ((Applet)this).getParameter("TitleFontHeight");
  259.       if (var1 == null) {
  260.          this.titleFontHeight = 12;
  261.       } else {
  262.          this.titleFontHeight = Integer.parseInt(var1);
  263.       }
  264.  
  265.       this.legendFontName = ((Applet)this).getParameter("LegendFontName");
  266.       if (this.legendFontName == null) {
  267.          this.legendFontName = "TimesRoman";
  268.       }
  269.  
  270.       var1 = ((Applet)this).getParameter("ShowLegend");
  271.       if (var1 == null) {
  272.          this.legendEnabled = true;
  273.       } else if (var1.toLowerCase().equals("yes")) {
  274.          this.legendEnabled = true;
  275.       } else {
  276.          this.legendEnabled = false;
  277.       }
  278.  
  279.       var1 = ((Applet)this).getParameter("LegendFontHeight");
  280.       if (var1 == null) {
  281.          this.legendFontHeight = 10;
  282.       } else {
  283.          this.legendFontHeight = Integer.parseInt(var1);
  284.       }
  285.  
  286.       var1 = ((Applet)this).getParameter("DrawBorders");
  287.       if (var1 == null) {
  288.          this.barBorderEnabled = true;
  289.       } else if (var1.toLowerCase().equals("yes")) {
  290.          this.barBorderEnabled = true;
  291.       } else {
  292.          this.barBorderEnabled = false;
  293.       }
  294.  
  295.       var1 = ((Applet)this).getParameter("ShowDateTime");
  296.       if (var1 == null) {
  297.          this.showDateTime = true;
  298.       } else if (var1.toLowerCase().equals("yes")) {
  299.          this.showDateTime = true;
  300.       } else {
  301.          this.showDateTime = false;
  302.       }
  303.  
  304.       var1 = ((Applet)this).getParameter("Cumulative");
  305.       if (var1 == null) {
  306.          this.isCumulative = false;
  307.       } else if (var1.toLowerCase().equals("yes")) {
  308.          this.isCumulative = true;
  309.       } else {
  310.          this.isCumulative = false;
  311.       }
  312.  
  313.       var1 = ((Applet)this).getParameter("DotSize");
  314.       if (var1 == null) {
  315.          this.dotSize = 2;
  316.       } else {
  317.          this.dotSize = Integer.parseInt(var1);
  318.       }
  319.  
  320.       var1 = ((Applet)this).getParameter("BackgroundColor");
  321.       if (var1 == null) {
  322.          this.backgroundColor = ((Component)this).getBackground();
  323.       } else {
  324.          this.backgroundColor = new Color(Integer.valueOf(var1, 16));
  325.       }
  326.  
  327.       var1 = ((Applet)this).getParameter("FontColor");
  328.       if (var1 == null) {
  329.          this.fontColor = Color.black;
  330.       } else {
  331.          this.fontColor = new Color(Integer.valueOf(var1, 16));
  332.       }
  333.  
  334.       var1 = ((Applet)this).getParameter("GridLineColor");
  335.       if (var1 == null) {
  336.          this.lineColor = Color.gray;
  337.       } else {
  338.          this.lineColor = new Color(Integer.valueOf(var1, 16));
  339.       }
  340.  
  341.       ((Component)this).setBackground(this.backgroundColor);
  342.       this.titleFont = new Font(this.titleFontName, 1, this.titleFontHeight);
  343.       this.titleFontMetrics = ((Component)this).getFontMetrics(this.titleFont);
  344.       this.legendFont = new Font(this.legendFontName, 0, this.legendFontHeight);
  345.       this.legendFontMetrics = ((Component)this).getFontMetrics(this.legendFont);
  346.       this.pointerToItems = new Vector();
  347.       this.items = new Vector();
  348.       this.getData(1);
  349.    }
  350.  
  351.    public synchronized void paint(Graphics var1) {
  352.       if (this.itemCount > 0) {
  353.          if (this.maxValue == (double)0.0F && this.minValue == (double)0.0F) {
  354.             this.maxValue = (double)1.0F;
  355.          } else if (this.maxValue < (double)0.0F) {
  356.             this.maxValue = (double)0.0F;
  357.          } else if (this.minValue > (double)0.0F) {
  358.             this.minValue = (double)0.0F;
  359.          }
  360.  
  361.          int var4 = ((Component)this).size().width / 15;
  362.          int var5 = ((Component)this).size().height / 15;
  363.          double var12 = Math.pow((double)10.0F, Math.floor(Math.log(this.maxValue - this.minValue) / Math.log((double)10.0F)));
  364.          if ((this.maxValue - this.minValue) / var12 < (double)2.0F) {
  365.             var12 /= (double)5.0F;
  366.          }
  367.  
  368.          if ((this.maxValue - this.minValue) / var12 < (double)5.0F) {
  369.             var12 /= (double)2.0F;
  370.          }
  371.  
  372.          var1.setColor(this.fontColor);
  373.          var1.setFont(this.titleFont);
  374.          var1.drawString(this.title, Math.max((((Component)this).size().width - this.titleFontMetrics.stringWidth(this.title)) / 2, 0), ((Component)this).size().height - var5);
  375.          var1.setFont(this.legendFont);
  376.          if (this.showDateTime) {
  377.             var1.drawString("(as of " + this.currentDateTime + ")", Math.max((((Component)this).size().width - this.legendFontMetrics.stringWidth("(as of " + this.currentDateTime + ")")) / 2, 0), ((Component)this).size().height - var5 + this.legendFontHeight * 3 / 2);
  378.          }
  379.  
  380.          int var25;
  381.          if (this.legendEnabled) {
  382.             int var43 = Math.min((((Component)this).size().height - this.titleFontHeight - 3 * var5) / this.legendFontHeight, this.itemCount);
  383.             int var44 = (((Component)this).size().height - this.titleFontHeight - 3 * var5) / var43;
  384.             if (var44 > 2 * this.titleFontHeight) {
  385.                var44 = 2 * this.titleFontHeight;
  386.             }
  387.  
  388.             var25 = this.legendFontHeight * 2 + this.maxItemLabelWidth + var4;
  389.             int var42 = var43 * var44;
  390.             int var8 = ((Component)this).size().width - var25;
  391.             int var9 = (((Component)this).size().height - 3 * var5 - this.titleFontHeight - var42) / 2 + var5;
  392.             var1.drawRect(var8 - this.legendFontHeight, var9 - var44 / 2, var25 - var4 + 2 * this.legendFontHeight, var42 + var44 / 2);
  393.  
  394.             for(int var2 = 0; var2 < var43; ++var2) {
  395.                var1.setColor(((Item)this.items.elementAt(var2)).color);
  396.                var1.fillRect(var8, var9, this.legendFontHeight, this.legendFontHeight);
  397.                if (this.barBorderEnabled) {
  398.                   var1.setColor(this.fontColor);
  399.                   var1.drawRect(var8, var9, this.legendFontHeight - 1, this.legendFontHeight - 1);
  400.                }
  401.  
  402.                var1.setColor(this.fontColor);
  403.                var1.drawString(((Item)this.items.elementAt(var2)).label, var8 + 2 * this.legendFontHeight, var9 + this.legendFontHeight - 1);
  404.                var9 += var44;
  405.             }
  406.          } else {
  407.             boolean var28 = false;
  408.             boolean var27 = false;
  409.             boolean var26 = false;
  410.             var25 = 0;
  411.          }
  412.  
  413.          int var23 = ((Component)this).size().width - 2 * var4 - var25;
  414.          int var24 = ((Component)this).size().height - this.titleFontHeight - this.legendFontHeight - 3 * var5;
  415.          double var10 = Math.max((double)var24 / (this.maxValue - this.minValue), (double)0.0F);
  416.          int var22 = Math.max(var23 / this.groupCount, 2);
  417.          int var6 = var4;
  418.          int var7 = var5 + var24 + (int)(this.minValue * var10);
  419.  
  420.          for(double var14 = (double)0.0F; var14 < this.maxValue; var14 += var12) {
  421.             var1.setColor(this.lineColor);
  422.             var1.drawLine(var6, var7 - (int)(var10 * var14), var6 + var23, var7 - (int)(var10 * var14));
  423.             var1.drawString("" + var14, var6 - this.legendFontMetrics.stringWidth("" + var14) - 2, var7 - (int)(var10 * var14) + this.legendFontHeight / 2);
  424.          }
  425.  
  426.          for(double var40 = (double)0.0F; var40 > this.minValue; var40 -= var12) {
  427.             var1.setColor(this.lineColor);
  428.             var1.drawLine(var6, var7 - (int)(var10 * var40), var6 + var23, var7 - (int)(var10 * var40));
  429.             var1.drawString("" + var40, var6 - this.legendFontMetrics.stringWidth("" + var40) - 2, var7 - (int)(var10 * var40) + this.legendFontHeight / 2);
  430.          }
  431.  
  432.          var6 += var22 / 2;
  433.          double var20 = (double)0.0F;
  434.          double var18 = (double)0.0F;
  435.          String var30 = "";
  436.          boolean var31 = true;
  437.          double var16 = (double)0.0F;
  438.  
  439.          for(int var3 = 0; var3 < this.itemCount; ++var3) {
  440.             ((Item)this.items.elementAt(var3)).value = (double)0.0F;
  441.             ((Item)this.items.elementAt(var3)).previousValue = (double)0.0F;
  442.          }
  443.  
  444.          for(int var36 = 0; var36 < this.rowCount; ++var36) {
  445.             int var32 = (Integer)this.pointerToItems.elementAt(var36);
  446.  
  447.             String var29;
  448.             try {
  449.                var29 = this.chartData.getData(var36 + 1, "Groups");
  450.             } catch (Exception var35) {
  451.                var29 = "";
  452.             }
  453.  
  454.             if (var36 > 0 && !var29.equals(var30)) {
  455.                for(int var37 = 0; var37 < this.itemCount; ++var37) {
  456.                   if (!var31) {
  457.                      var1.setColor(((Item)this.items.elementAt(var37)).color);
  458.                      var1.drawLine(var6 - var22, var7 - (int)((double)0.5F + var10 * ((Item)this.items.elementAt(var37)).previousValue), var6, var7 - (int)((double)0.5F + var10 * ((Item)this.items.elementAt(var37)).value));
  459.                      var1.fillRect(var6 - var22 - this.dotSize, var7 - (int)((double)0.5F + var10 * ((Item)this.items.elementAt(var37)).previousValue) - this.dotSize, this.dotSize * 2 + 1, this.dotSize * 2 + 1);
  460.                   }
  461.  
  462.                   if (!this.isCumulative) {
  463.                      ((Item)this.items.elementAt(var37)).copyValueAndReset();
  464.                   } else {
  465.                      ((Item)this.items.elementAt(var37)).copyValue();
  466.                   }
  467.                }
  468.  
  469.                if (this.legendFontMetrics.stringWidth(var30) > var22) {
  470.                   var30 = var30.substring(0, var30.length() * var22 / this.legendFontMetrics.stringWidth(var30));
  471.                }
  472.  
  473.                var1.setColor(this.fontColor);
  474.                var1.drawString(var30, var6 - this.legendFontMetrics.stringWidth(var30) / 2, var7 + this.legendFontHeight + this.dotSize);
  475.                var6 += var22;
  476.                var31 = false;
  477.             }
  478.  
  479.             var30 = var29;
  480.  
  481.             try {
  482.                var16 = new Double(this.chartData.getData(var36 + 1, "Values"));
  483.             } catch (Exception var34) {
  484.                var16 = (double)0.0F;
  485.             }
  486.  
  487.             Item var10000 = (Item)this.items.elementAt(var32);
  488.             var10000.value += var16;
  489.          }
  490.  
  491.          for(int var38 = 0; var38 < this.itemCount; ++var38) {
  492.             var1.setColor(((Item)this.items.elementAt(var38)).color);
  493.             var1.drawLine(var6 - var22, var7 - (int)((double)0.5F + var10 * ((Item)this.items.elementAt(var38)).previousValue), var6, var7 - (int)((double)0.5F + var10 * ((Item)this.items.elementAt(var38)).value));
  494.             var1.fillRect(var6 - var22 - this.dotSize, var7 - (int)((double)0.5F + var10 * ((Item)this.items.elementAt(var38)).previousValue) - this.dotSize, this.dotSize * 2 + 1, this.dotSize * 2 + 1);
  495.             var1.fillRect(var6 - this.dotSize, var7 - (int)((double)0.5F + var10 * ((Item)this.items.elementAt(var38)).value) - this.dotSize, this.dotSize * 2 + 1, this.dotSize * 2 + 1);
  496.          }
  497.  
  498.          if (this.legendFontMetrics.stringWidth(var30) > var22) {
  499.             var30 = var30.substring(0, var30.length() * var22 / this.legendFontMetrics.stringWidth(var30));
  500.          }
  501.  
  502.          var1.setColor(this.fontColor);
  503.          var1.drawString(var30, var6 - this.legendFontMetrics.stringWidth(var30) / 2, var7 + this.legendFontHeight + this.dotSize);
  504.       }
  505.  
  506.    }
  507. }
  508.