home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Webroot / classes / CFGraphs / areachart.class (.txt) next >
Encoding:
Java Class File  |  1999-04-12  |  9.1 KB  |  535 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 AreaChart 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 orientation;
  36.    int shadow;
  37.    boolean barBorderEnabled;
  38.    boolean showDateTime;
  39.    boolean isCumulative;
  40.    String currentDateTime;
  41.    Color backgroundColor;
  42.    Color fontColor;
  43.    Color lineColor;
  44.    Vector items;
  45.    Vector pointerToItems;
  46.    Recordset chartData;
  47.    int rowCount;
  48.    int groupCount;
  49.    int itemCount;
  50.    int maxItemLabelWidth;
  51.    int maxGroupLabelWidth;
  52.    double maxGroupSum;
  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.             this.maxGroupSum = (double)0.0F;
  90.             double var7 = (double)0.0F;
  91.  
  92.             for(int var2 = 0; var2 < this.rowCount; ++var2) {
  93.                double var5;
  94.                try {
  95.                   var5 = Math.abs(new Double(this.chartData.getData(var2 + 1, "Values")));
  96.                } catch (Exception var11) {
  97.                   var5 = (double)0.0F;
  98.                }
  99.  
  100.                if (var2 == 0) {
  101.                   this.groupCount = this.itemCount = 1;
  102.                   this.pointerToItems.setSize(this.rowCount);
  103.                   this.pointerToItems.setElementAt(new Integer(this.itemCount - 1), var2);
  104.                   this.items.setSize(this.itemCount);
  105.                   this.items.setElementAt(new Item(this.chartData.getData(var2 + 1, "Items")), this.itemCount - 1);
  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.                      if (this.maxGroupSum < var7) {
  111.                         this.maxGroupSum = var7;
  112.                      }
  113.  
  114.                      this.maxGroupLabelWidth = Math.max(this.legendFontMetrics.stringWidth(this.chartData.getData(var2 + 1, "Groups")), this.maxGroupLabelWidth);
  115.                      if (!this.isCumulative) {
  116.                         var7 = (double)0.0F;
  117.                      }
  118.  
  119.                      ++this.groupCount;
  120.                   }
  121.  
  122.                   boolean var4 = false;
  123.  
  124.                   for(int var3 = 0; var3 < var2; ++var3) {
  125.                      if (this.chartData.getData(var2 + 1, "Items").equals(this.chartData.getData(var3 + 1, "Items"))) {
  126.                         var4 = true;
  127.                         this.pointerToItems.setElementAt(this.pointerToItems.elementAt(var3), var2);
  128.                         break;
  129.                      }
  130.                   }
  131.  
  132.                   if (!var4) {
  133.                      ++this.itemCount;
  134.                      this.pointerToItems.setElementAt(new Integer(this.itemCount - 1), var2);
  135.                      this.items.setSize(this.itemCount);
  136.                      this.items.setElementAt(new Item(this.chartData.getData(var2 + 1, "Items")), this.itemCount - 1);
  137.                      this.maxItemLabelWidth = Math.max(this.legendFontMetrics.stringWidth(this.chartData.getData(var2 + 1, "Items")), this.maxItemLabelWidth);
  138.                   }
  139.                }
  140.  
  141.                var7 += var5;
  142.             }
  143.  
  144.             if (this.maxGroupSum < var7) {
  145.                this.maxGroupSum = var7;
  146.             }
  147.  
  148.             for(int var13 = 0; var13 < this.itemCount; ++var13) {
  149.                if (var13 < 9) {
  150.                   ((Item)this.items.elementAt(var13)).color = defaultColors[var13];
  151.                } else if (var13 < 18) {
  152.                   ((Item)this.items.elementAt(var13)).color = defaultColors[var13 - 9].darker();
  153.                } else if (var13 < 27) {
  154.                   ((Item)this.items.elementAt(var13)).color = defaultColors[var13 - 18].darker().darker();
  155.                } else {
  156.                   ((Item)this.items.elementAt(var13)).color = Color.black;
  157.                }
  158.             }
  159.          }
  160.  
  161.          this.currentDateTime = (new Date()).toLocaleString();
  162.       } catch (Exception var12) {
  163.          if (this.debugInfoEnabled) {
  164.             Debug.write(((Throwable)var12).getMessage());
  165.          }
  166.  
  167.       }
  168.    }
  169.  
  170.    public boolean mouseExit(Event var1, int var2, int var3) {
  171.       if (var1.id == 505) {
  172.          ((Applet)this).getAppletContext().showStatus("");
  173.          return true;
  174.       } else {
  175.          return false;
  176.       }
  177.    }
  178.  
  179.    static {
  180.       defaultColors = new Color[]{Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.cyan, Color.orange, Color.pink, Color.darkGray};
  181.    }
  182.  
  183.    private void pause(int var1) {
  184.       try {
  185.          Thread.sleep((long)var1);
  186.       } catch (InterruptedException var4) {
  187.          if (this.debugInfoEnabled) {
  188.             Debug.write(((Throwable)var4).getMessage());
  189.          }
  190.  
  191.       }
  192.    }
  193.  
  194.    public boolean mouseEnter(Event var1, int var2, int var3) {
  195.       if (var1.id == 504) {
  196.          ((Applet)this).getAppletContext().showStatus("(c)1996 Allaire Corp.");
  197.          return true;
  198.       } else {
  199.          return false;
  200.       }
  201.    }
  202.  
  203.    public void run() {
  204.       while(this.refreshTime != 0) {
  205.          this.pause(this.refreshTime * 1000);
  206.          this.getData(2);
  207.          ((Component)this).repaint();
  208.       }
  209.  
  210.       this.stop();
  211.    }
  212.  
  213.    public synchronized void init() {
  214.       String var1 = ((Applet)this).getParameter("RefreshTime");
  215.       if (var1 == null) {
  216.          this.refreshTime = 0;
  217.       } else {
  218.          this.refreshTime = Integer.parseInt(var1);
  219.          var1 = ((Applet)this).getParameter("RefreshDataFromURL");
  220.          if (var1 == null) {
  221.             this.refreshTime = 0;
  222.          } else {
  223.             if (var1.indexOf(63) == -1) {
  224.                var1 = var1 + '?';
  225.             }
  226.  
  227.             this.query = new Query(var1);
  228.             this.query.addParam("RefreshTime", String.valueOf(this.refreshTime));
  229.          }
  230.       }
  231.  
  232.       var1 = ((Applet)this).getParameter("DebugInfoEnabled");
  233.       if (var1 == null) {
  234.          this.debugInfoEnabled = false;
  235.       } else if (var1.toLowerCase().equals("yes")) {
  236.          this.debugInfoEnabled = true;
  237.       } else {
  238.          this.debugInfoEnabled = false;
  239.       }
  240.  
  241.       this.title = ((Applet)this).getParameter("Title");
  242.       if (this.title == null) {
  243.          this.title = "Chart";
  244.       }
  245.  
  246.       this.titleFontName = ((Applet)this).getParameter("TitleFontName");
  247.       if (this.titleFontName == null) {
  248.          this.titleFontName = "TimesRoman";
  249.       }
  250.  
  251.       var1 = ((Applet)this).getParameter("TitleFontHeight");
  252.       if (var1 == null) {
  253.          this.titleFontHeight = 12;
  254.       } else {
  255.          this.titleFontHeight = Integer.parseInt(var1);
  256.       }
  257.  
  258.       this.legendFontName = ((Applet)this).getParameter("LegendFontName");
  259.       if (this.legendFontName == null) {
  260.          this.legendFontName = "TimesRoman";
  261.       }
  262.  
  263.       var1 = ((Applet)this).getParameter("ShowLegend");
  264.       if (var1 == null) {
  265.          this.legendEnabled = true;
  266.       } else if (var1.toLowerCase().equals("yes")) {
  267.          this.legendEnabled = true;
  268.       } else {
  269.          this.legendEnabled = false;
  270.       }
  271.  
  272.       var1 = ((Applet)this).getParameter("LegendFontHeight");
  273.       if (var1 == null) {
  274.          this.legendFontHeight = 10;
  275.       } else {
  276.          this.legendFontHeight = Integer.parseInt(var1);
  277.       }
  278.  
  279.       var1 = ((Applet)this).getParameter("DrawBorders");
  280.       if (var1 == null) {
  281.          this.barBorderEnabled = true;
  282.       } else if (var1.toLowerCase().equals("yes")) {
  283.          this.barBorderEnabled = true;
  284.       } else {
  285.          this.barBorderEnabled = false;
  286.       }
  287.  
  288.       var1 = ((Applet)this).getParameter("ShowDateTime");
  289.       if (var1 == null) {
  290.          this.showDateTime = true;
  291.       } else if (var1.toLowerCase().equals("yes")) {
  292.          this.showDateTime = true;
  293.       } else {
  294.          this.showDateTime = false;
  295.       }
  296.  
  297.       var1 = ((Applet)this).getParameter("Cumulative");
  298.       if (var1 == null) {
  299.          this.isCumulative = false;
  300.       } else if (var1.toLowerCase().equals("yes")) {
  301.          this.isCumulative = true;
  302.       } else {
  303.          this.isCumulative = false;
  304.       }
  305.  
  306.       var1 = ((Applet)this).getParameter("BackgroundColor");
  307.       if (var1 == null) {
  308.          this.backgroundColor = ((Component)this).getBackground();
  309.       } else {
  310.          this.backgroundColor = new Color(Integer.valueOf(var1, 16));
  311.       }
  312.  
  313.       var1 = ((Applet)this).getParameter("FontColor");
  314.       if (var1 == null) {
  315.          this.fontColor = Color.black;
  316.       } else {
  317.          this.fontColor = new Color(Integer.valueOf(var1, 16));
  318.       }
  319.  
  320.       var1 = ((Applet)this).getParameter("GridLineColor");
  321.       if (var1 == null) {
  322.          this.lineColor = Color.gray;
  323.       } else {
  324.          this.lineColor = new Color(Integer.valueOf(var1, 16));
  325.       }
  326.  
  327.       ((Component)this).setBackground(this.backgroundColor);
  328.       this.titleFont = new Font(this.titleFontName, 1, this.titleFontHeight);
  329.       this.titleFontMetrics = ((Component)this).getFontMetrics(this.titleFont);
  330.       this.legendFont = new Font(this.legendFontName, 0, this.legendFontHeight);
  331.       this.legendFontMetrics = ((Component)this).getFontMetrics(this.legendFont);
  332.       this.pointerToItems = new Vector();
  333.       this.items = new Vector();
  334.       this.getData(1);
  335.    }
  336.  
  337.    public synchronized void paint(Graphics var1) {
  338.       int[] var32 = new int[5];
  339.       int[] var33 = new int[5];
  340.       if (this.itemCount > 0) {
  341.          if (this.maxGroupSum == (double)0.0F) {
  342.             this.maxGroupSum = (double)1.0F;
  343.          }
  344.  
  345.          int var4 = ((Component)this).size().width / 15;
  346.          int var5 = ((Component)this).size().height / 15;
  347.          double var12 = Math.pow((double)10.0F, Math.floor(Math.log(this.maxGroupSum) / Math.log((double)10.0F)));
  348.          if (this.maxGroupSum / var12 < (double)2.0F) {
  349.             var12 /= (double)5.0F;
  350.          }
  351.  
  352.          if (this.maxGroupSum / var12 < (double)5.0F) {
  353.             var12 /= (double)2.0F;
  354.          }
  355.  
  356.          var1.setColor(this.fontColor);
  357.          var1.setFont(this.titleFont);
  358.          var1.drawString(this.title, Math.max((((Component)this).size().width - this.titleFontMetrics.stringWidth(this.title)) / 2, 0), ((Component)this).size().height - var5);
  359.          var1.setFont(this.legendFont);
  360.          if (this.showDateTime) {
  361.             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);
  362.          }
  363.  
  364.          int var25;
  365.          if (this.legendEnabled) {
  366.             int var44 = Math.min((((Component)this).size().height - this.titleFontHeight - 3 * var5) / this.legendFontHeight, this.itemCount);
  367.             int var45 = (((Component)this).size().height - this.titleFontHeight - 3 * var5) / var44;
  368.             if (var45 > 2 * this.titleFontHeight) {
  369.                var45 = 2 * this.titleFontHeight;
  370.             }
  371.  
  372.             var25 = this.legendFontHeight * 2 + this.maxItemLabelWidth + var4;
  373.             int var43 = var44 * var45;
  374.             int var8 = ((Component)this).size().width - var25;
  375.             int var9 = (((Component)this).size().height - 3 * var5 - this.titleFontHeight - var43) / 2 + var5;
  376.             var1.drawRect(var8 - this.legendFontHeight, var9 - var45 / 2, var25 - var4 + 2 * this.legendFontHeight, var43 + var45 / 2);
  377.  
  378.             for(int var2 = 0; var2 < var44; ++var2) {
  379.                var1.setColor(((Item)this.items.elementAt(var2)).color);
  380.                var1.fillRect(var8, var9, this.legendFontHeight, this.legendFontHeight);
  381.                if (this.barBorderEnabled) {
  382.                   var1.setColor(this.fontColor);
  383.                   var1.drawRect(var8, var9, this.legendFontHeight - 1, this.legendFontHeight - 1);
  384.                }
  385.  
  386.                var1.setColor(this.fontColor);
  387.                var1.drawString(((Item)this.items.elementAt(var2)).label, var8 + 2 * this.legendFontHeight, var9 + this.legendFontHeight - 1);
  388.                var9 += var45;
  389.             }
  390.          } else {
  391.             boolean var28 = false;
  392.             boolean var27 = false;
  393.             boolean var26 = false;
  394.             var25 = 0;
  395.          }
  396.  
  397.          int var23 = ((Component)this).size().width - 2 * var4 - var25;
  398.          int var24 = ((Component)this).size().height - this.titleFontHeight - this.legendFontHeight - 3 * var5;
  399.          double var10 = Math.max((double)var24 / this.maxGroupSum, (double)0.0F);
  400.          int var22 = Math.max(var23 / this.groupCount, 2);
  401.          int var6 = var4;
  402.          int var7 = var5 + var24;
  403.  
  404.          for(double var14 = (double)0.0F; var14 < this.maxGroupSum; var14 += var12) {
  405.             var1.setColor(this.lineColor);
  406.             var1.drawLine(var6, var7 - (int)(var10 * var14), var6 + var23, var7 - (int)(var10 * var14));
  407.             var1.drawString("" + var14, var6 - this.legendFontMetrics.stringWidth("" + var14) - 2, var7 - (int)(var10 * var14) + this.legendFontHeight / 2);
  408.          }
  409.  
  410.          var6 += var22 / 2;
  411.          double var20 = (double)0.0F;
  412.          double var18 = (double)0.0F;
  413.          String var30 = "";
  414.          boolean var31 = true;
  415.          var33[0] = var33[3] = var33[1] = var33[2] = var7;
  416.          var32[0] = var32[1] = var32[2] = var32[3] = var6;
  417.          var32[4] = var32[0];
  418.          var33[4] = var33[0];
  419.          double var16 = (double)0.0F;
  420.  
  421.          for(int var3 = 0; var3 < this.itemCount; ++var3) {
  422.             ((Item)this.items.elementAt(var3)).value = (double)0.0F;
  423.             ((Item)this.items.elementAt(var3)).previousValue = (double)0.0F;
  424.          }
  425.  
  426.          for(int var38 = 0; var38 < this.rowCount; ++var38) {
  427.             int var34 = (Integer)this.pointerToItems.elementAt(var38);
  428.  
  429.             String var29;
  430.             try {
  431.                var29 = this.chartData.getData(var38 + 1, "Groups");
  432.             } catch (Exception var37) {
  433.                var29 = "";
  434.             }
  435.  
  436.             if (var38 > 0 && !var29.equals(var30)) {
  437.                for(int var39 = 0; var39 < this.itemCount; ++var39) {
  438.                   var33[0] = var33[1];
  439.                   var33[3] = var33[2];
  440.                   var33[2] -= (int)((double)0.5F + Math.abs(var10 * ((Item)this.items.elementAt(var39)).value));
  441.                   var33[1] -= (int)((double)0.5F + Math.abs(var10 * ((Item)this.items.elementAt(var39)).previousValue));
  442.                   var32[4] = var32[0];
  443.                   var33[4] = var33[0];
  444.                   if (!var31) {
  445.                      var1.setColor(((Item)this.items.elementAt(var39)).color);
  446.                      var1.fillPolygon(var32, var33, 5);
  447.                      if (this.barBorderEnabled) {
  448.                         var1.setColor(this.fontColor);
  449.                         var1.drawPolygon(var32, var33, 5);
  450.                      }
  451.                   }
  452.  
  453.                   if (!this.isCumulative) {
  454.                      ((Item)this.items.elementAt(var39)).copyValueAndReset();
  455.                   } else {
  456.                      ((Item)this.items.elementAt(var39)).copyValue();
  457.                   }
  458.                }
  459.  
  460.                if (this.legendFontMetrics.stringWidth(var30) > var22) {
  461.                   var30 = var30.substring(0, var30.length() * var22 / this.legendFontMetrics.stringWidth(var30));
  462.                }
  463.  
  464.                if (!var31) {
  465.                   var1.setColor(this.fontColor);
  466.                   if (!this.isCumulative) {
  467.                      var1.drawString("" + var20, var32[1] - this.legendFontMetrics.stringWidth("" + var20) / 2, var33[1] - 5);
  468.                   } else {
  469.                      var1.drawString("" + var20, var32[1] - this.legendFontMetrics.stringWidth("" + var20), var33[1] - 2);
  470.                   }
  471.                }
  472.  
  473.                var1.setColor(this.fontColor);
  474.                var1.drawString(var30, var6 - this.legendFontMetrics.stringWidth(var30) / 2, var7 + this.legendFontHeight);
  475.                var6 += var22;
  476.                var20 = var18;
  477.                if (!this.isCumulative) {
  478.                   var18 = (double)0.0F;
  479.                }
  480.  
  481.                var32[0] = var32[1] = var6 - var22;
  482.                var32[2] = var32[3] = var6;
  483.                var33[0] = var33[3] = var33[1] = var33[2] = var7;
  484.                var32[4] = var32[0];
  485.                var33[4] = var33[0];
  486.                var31 = false;
  487.             }
  488.  
  489.             var30 = var29;
  490.  
  491.             try {
  492.                var16 = new Double(this.chartData.getData(var38 + 1, "Values"));
  493.             } catch (Exception var36) {
  494.                var16 = (double)0.0F;
  495.             }
  496.  
  497.             var18 += Math.abs(var16);
  498.             Item var10000 = (Item)this.items.elementAt(var34);
  499.             var10000.value += var16;
  500.          }
  501.  
  502.          for(int var40 = 0; var40 < this.itemCount; ++var40) {
  503.             var33[0] = var33[1];
  504.             var33[3] = var33[2];
  505.             var33[2] -= (int)((double)0.5F + Math.abs(var10 * ((Item)this.items.elementAt(var40)).value));
  506.             var33[1] -= (int)((double)0.5F + Math.abs(var10 * ((Item)this.items.elementAt(var40)).previousValue));
  507.             var32[4] = var32[0];
  508.             var33[4] = var33[0];
  509.             var1.setColor(((Item)this.items.elementAt(var40)).color);
  510.             var1.fillPolygon(var32, var33, 5);
  511.             if (this.barBorderEnabled) {
  512.                var1.setColor(this.fontColor);
  513.                var1.drawPolygon(var32, var33, 5);
  514.             }
  515.          }
  516.  
  517.          if (this.legendFontMetrics.stringWidth(var30) > var22) {
  518.             var30 = var30.substring(0, var30.length() * var22 / this.legendFontMetrics.stringWidth(var30));
  519.          }
  520.  
  521.          var1.setColor(this.fontColor);
  522.          if (!this.isCumulative) {
  523.             var1.drawString("" + var20, var32[1] - this.legendFontMetrics.stringWidth("" + var20) / 2, var33[1] - 5);
  524.             var1.drawString("" + var18, var32[2] - this.legendFontMetrics.stringWidth("" + var18) / 2, var33[2] - 5);
  525.          } else {
  526.             var1.drawString("" + var20, var32[1] - this.legendFontMetrics.stringWidth("" + var20), var33[1] - 2);
  527.             var1.drawString("" + var18, var32[2] - this.legendFontMetrics.stringWidth("" + var18), var33[2] - 2);
  528.          }
  529.  
  530.          var1.drawString(var30, var6 - this.legendFontMetrics.stringWidth(var30) / 2, var7 + this.legendFontHeight);
  531.       }
  532.  
  533.    }
  534. }
  535.