home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Webroot / classes / CFGraphs / piechart.class (.txt) < prev   
Encoding:
Java Class File  |  1999-04-12  |  7.6 KB  |  379 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.Font;
  9. import java.awt.FontMetrics;
  10. import java.awt.Graphics;
  11. import java.util.Date;
  12. import java.util.Vector;
  13.  
  14. public class PieChart extends Applet implements Runnable {
  15.    Thread runner;
  16.    Query query;
  17.    int refreshTime;
  18.    boolean debugInfoEnabled;
  19.    String title;
  20.    String titleFontName;
  21.    Font titleFont;
  22.    FontMetrics titleFontMetrics;
  23.    int titleFontHeight;
  24.    String legendFontName;
  25.    Font legendFont;
  26.    FontMetrics legendFontMetrics;
  27.    int legendFontHeight;
  28.    int legendHeight;
  29.    int legendWidth;
  30.    boolean legendEnabled;
  31.    boolean sliceBorderEnabled;
  32.    boolean showDateTime;
  33.    Color backgroundColor;
  34.    Color fontColor;
  35.    Color[] defaultColors;
  36.    Vector colors;
  37.    Recordset chartData;
  38.    int itemCount;
  39.    int scale;
  40.    int maxLegendWidth;
  41.    double sum;
  42.  
  43.    public void start() {
  44.       if (this.runner == null) {
  45.          this.runner = new Thread(this);
  46.          this.runner.start();
  47.       }
  48.  
  49.    }
  50.  
  51.    public PieChart() {
  52.       this.defaultColors = new Color[]{Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.cyan, Color.orange, Color.pink, Color.darkGray};
  53.    }
  54.  
  55.    public void stop() {
  56.       if (this.runner != null) {
  57.          this.runner.stop();
  58.          this.runner = null;
  59.       }
  60.  
  61.    }
  62.  
  63.    private void getData() {
  64.       this.sum = (double)0.0F;
  65.  
  66.       try {
  67.          if (this.chartData == null) {
  68.             this.chartData = new AppletParamRecordset(this, "ChartData");
  69.          } else {
  70.             this.query.execute();
  71.             this.chartData = this.query.getRecordset();
  72.          }
  73.  
  74.          if (this.refreshTime == 0) {
  75.             ((Applet)this).getAppletContext().showStatus("Done");
  76.          } else {
  77.             ((Applet)this).getAppletContext().showStatus("...");
  78.          }
  79.  
  80.          this.itemCount = this.chartData.getRowCount();
  81.          this.colors.setSize(this.itemCount);
  82.  
  83.          for(int var1 = 0; var1 < this.itemCount; ++var1) {
  84.             this.maxLegendWidth = Math.max(this.legendFontMetrics.stringWidth(this.chartData.getData(var1 + 1, "Items") + " (" + this.chartData.getData(var1 + 1, "Values") + ")"), this.maxLegendWidth);
  85.  
  86.             try {
  87.                this.sum += Math.abs(new Double(this.chartData.getData(var1 + 1, "Values")));
  88.             } catch (Exception var5) {
  89.                this.sum += (double)0.0F;
  90.             }
  91.  
  92.             if (!this.chartData.columnExists("Colors")) {
  93.                if (var1 < 9) {
  94.                   this.colors.setElementAt(this.defaultColors[var1], var1);
  95.                } else if (var1 < 18) {
  96.                   this.colors.setElementAt(this.defaultColors[var1 - 9].darker(), var1);
  97.                } else if (var1 < 27) {
  98.                   this.colors.setElementAt(this.defaultColors[var1 - 18].darker().darker(), var1);
  99.                } else {
  100.                   this.colors.setElementAt(Color.black, var1);
  101.                }
  102.             } else {
  103.                String var2 = this.chartData.getData(var1 + 1, "Colors");
  104.                if (var2.equals("red")) {
  105.                   this.colors.setElementAt(Color.red, var1);
  106.                } else if (var2.equals("green")) {
  107.                   this.colors.setElementAt(Color.green, var1);
  108.                } else if (var2.equals("blue")) {
  109.                   this.colors.setElementAt(Color.blue, var1);
  110.                } else if (var2.equals("pink")) {
  111.                   this.colors.setElementAt(Color.pink, var1);
  112.                } else if (var2.equals("orange")) {
  113.                   this.colors.setElementAt(Color.orange, var1);
  114.                } else if (var2.equals("magenta")) {
  115.                   this.colors.setElementAt(Color.magenta, var1);
  116.                } else if (var2.equals("cyan")) {
  117.                   this.colors.setElementAt(Color.cyan, var1);
  118.                } else if (var2.equals("white")) {
  119.                   this.colors.setElementAt(Color.white, var1);
  120.                } else if (var2.equals("yellow")) {
  121.                   this.colors.setElementAt(Color.yellow, var1);
  122.                } else if (var2.equals("gray")) {
  123.                   this.colors.setElementAt(Color.gray, var1);
  124.                } else if (var2.equals("darkGray")) {
  125.                   this.colors.setElementAt(Color.darkGray, var1);
  126.                } else {
  127.                   this.colors.setElementAt(Color.black, var1);
  128.                }
  129.             }
  130.          }
  131.  
  132.       } catch (Exception var6) {
  133.          if (this.debugInfoEnabled) {
  134.             Debug.write(((Throwable)var6).getMessage());
  135.          }
  136.  
  137.       }
  138.    }
  139.  
  140.    private void pause(int var1) {
  141.       try {
  142.          Thread.sleep((long)var1);
  143.       } catch (InterruptedException var4) {
  144.          if (this.debugInfoEnabled) {
  145.             Debug.write(((Throwable)var4).getMessage());
  146.          }
  147.  
  148.       }
  149.    }
  150.  
  151.    public void run() {
  152.       while(this.refreshTime != 0) {
  153.          this.pause(this.refreshTime * 1000);
  154.          this.getData();
  155.          ((Component)this).repaint();
  156.       }
  157.  
  158.       this.stop();
  159.    }
  160.  
  161.    public synchronized void init() {
  162.       String var1 = ((Applet)this).getParameter("RefreshTime");
  163.       if (var1 == null) {
  164.          this.refreshTime = 0;
  165.       } else {
  166.          this.refreshTime = Integer.parseInt(var1);
  167.          var1 = ((Applet)this).getParameter("RefreshDataFromURL");
  168.          if (var1 == null) {
  169.             this.refreshTime = 0;
  170.          } else {
  171.             if (var1.indexOf(63) == -1) {
  172.                var1 = var1 + '?';
  173.             }
  174.  
  175.             this.query = new Query(var1);
  176.             this.query.addParam("RefreshTime", String.valueOf(this.refreshTime));
  177.          }
  178.       }
  179.  
  180.       var1 = ((Applet)this).getParameter("DebugInfoEnabled");
  181.       if (var1 == null) {
  182.          this.debugInfoEnabled = false;
  183.       } else if (var1.toLowerCase().equals("yes")) {
  184.          this.debugInfoEnabled = true;
  185.       } else {
  186.          this.debugInfoEnabled = false;
  187.       }
  188.  
  189.       this.title = ((Applet)this).getParameter("Title");
  190.       if (this.title == null) {
  191.          this.title = "Chart";
  192.       }
  193.  
  194.       this.titleFontName = ((Applet)this).getParameter("TitleFontName");
  195.       if (this.titleFontName == null) {
  196.          this.titleFontName = "TimesRoman";
  197.       }
  198.  
  199.       var1 = ((Applet)this).getParameter("TitleFontHeight");
  200.       if (var1 == null) {
  201.          this.titleFontHeight = 12;
  202.       } else {
  203.          this.titleFontHeight = Integer.parseInt(var1);
  204.       }
  205.  
  206.       this.legendFontName = ((Applet)this).getParameter("LegendFontName");
  207.       if (this.legendFontName == null) {
  208.          this.legendFontName = "TimesRoman";
  209.       }
  210.  
  211.       var1 = ((Applet)this).getParameter("LegendFontHeight");
  212.       if (var1 == null) {
  213.          this.legendFontHeight = 10;
  214.       } else {
  215.          this.legendFontHeight = Integer.parseInt(var1);
  216.       }
  217.  
  218.       var1 = ((Applet)this).getParameter("ShowLegend");
  219.       if (var1 == null) {
  220.          this.legendEnabled = true;
  221.       } else if (var1.toLowerCase().equals("yes")) {
  222.          this.legendEnabled = true;
  223.       } else {
  224.          this.legendEnabled = false;
  225.       }
  226.  
  227.       var1 = ((Applet)this).getParameter("DrawBorders");
  228.       if (var1 == null) {
  229.          this.sliceBorderEnabled = false;
  230.       } else if (var1.toLowerCase().equals("yes")) {
  231.          this.sliceBorderEnabled = true;
  232.       } else {
  233.          this.sliceBorderEnabled = false;
  234.       }
  235.  
  236.       var1 = ((Applet)this).getParameter("ShowDateTime");
  237.       if (var1 == null) {
  238.          this.showDateTime = true;
  239.       } else if (var1.toLowerCase().equals("yes")) {
  240.          this.showDateTime = true;
  241.       } else {
  242.          this.showDateTime = false;
  243.       }
  244.  
  245.       var1 = ((Applet)this).getParameter("BackgroundColor");
  246.       if (var1 == null) {
  247.          this.backgroundColor = ((Component)this).getBackground();
  248.       } else {
  249.          this.backgroundColor = new Color(Integer.valueOf(var1, 16));
  250.       }
  251.  
  252.       var1 = ((Applet)this).getParameter("FontColor");
  253.       if (var1 == null) {
  254.          this.fontColor = Color.black;
  255.       } else {
  256.          this.fontColor = new Color(Integer.valueOf(var1, 16));
  257.       }
  258.  
  259.       ((Component)this).setBackground(this.backgroundColor);
  260.       this.titleFont = new Font(this.titleFontName, 1, this.titleFontHeight);
  261.       this.titleFontMetrics = ((Component)this).getFontMetrics(this.titleFont);
  262.       this.legendFont = new Font(this.legendFontName, 0, this.legendFontHeight);
  263.       this.legendFontMetrics = ((Component)this).getFontMetrics(this.legendFont);
  264.       this.colors = new Vector();
  265.       this.getData();
  266.    }
  267.  
  268.    public synchronized void paint(Graphics var1) {
  269.       int var5 = 0;
  270.       int var6 = 0;
  271.       int var7 = 0;
  272.       int var8 = 0;
  273.       int var10 = 0;
  274.       double var13 = (double)0.0F;
  275.       boolean var16 = false;
  276.       if (this.itemCount > 0) {
  277.          int var3 = ((Component)this).size().width / 15;
  278.          int var4 = Math.max(((Component)this).size().height / 15, 2 * this.legendFontHeight);
  279.          int var11;
  280.          int var12;
  281.          if (this.legendEnabled) {
  282.             var11 = Math.min((((Component)this).size().height - this.titleFontHeight - 3 * var4) / this.legendFontHeight, this.itemCount);
  283.             var12 = (((Component)this).size().height - this.titleFontHeight - 3 * var4) / var11;
  284.             if (var12 > 2 * this.titleFontHeight) {
  285.                var12 = 2 * this.titleFontHeight;
  286.             }
  287.  
  288.             this.legendWidth = this.legendFontHeight * 2 + this.maxLegendWidth + var3;
  289.             this.legendHeight = var11 * var12;
  290.             var7 = ((Component)this).size().width - this.legendWidth;
  291.             var8 = (((Component)this).size().height - 3 * var4 - this.titleFontHeight - this.legendHeight) / 2 + var4;
  292.             var1.setColor(this.fontColor);
  293.             var1.drawRect(var7 - this.legendFontHeight, var8 - var12 / 2, this.legendWidth - var3 + 2 * this.legendFontHeight, this.legendHeight + var12 / 2);
  294.          } else {
  295.             var12 = 0;
  296.             var11 = 0;
  297.             this.legendWidth = this.legendHeight = 0;
  298.          }
  299.  
  300.          this.scale = Math.max(Math.min(((Component)this).size().width - this.legendWidth - 3 * var3, ((Component)this).size().height - this.titleFontHeight - 3 * var4), 10);
  301.          var1.setColor(this.fontColor);
  302.          var1.setFont(this.titleFont);
  303.          var1.drawString(this.title, Math.max((((Component)this).size().width - this.titleFontMetrics.stringWidth(this.title)) / 2, 0), ((Component)this).size().height - var4);
  304.          var1.setFont(this.legendFont);
  305.          if (this.showDateTime) {
  306.             String var17 = (new Date()).toLocaleString();
  307.             var1.drawString("(as of " + var17 + ")", Math.max((((Component)this).size().width - this.legendFontMetrics.stringWidth("(as of " + var17 + ")")) / 2, 0), ((Component)this).size().height - var4 + this.legendFontHeight * 3 / 2);
  308.          }
  309.  
  310.          var5 = (((Component)this).size().width - this.legendWidth - 3 * var3 - this.scale) / 2 + var3 * 3 / 2;
  311.          var6 = (((Component)this).size().height - this.titleFontHeight - 3 * var4 - this.scale) / 2 + var4;
  312.  
  313.          for(int var2 = 0; var2 < this.itemCount; ++var2) {
  314.             try {
  315.                var13 = Math.abs((double)100.0F * new Double(this.chartData.getData(var2 + 1, "Values")) / this.sum);
  316.             } catch (Exception var20) {
  317.                var13 = (double)0.0F;
  318.             }
  319.  
  320.             int var9 = (int)Math.round((double)360.0F * var13 / (double)100.0F);
  321.             var1.setColor((Color)this.colors.elementAt(var2));
  322.             if (var2 == this.itemCount - 1) {
  323.                if (var10 < 360) {
  324.                   var1.fillArc(var5, var6, this.scale, this.scale, var10, 360 - var10);
  325.                }
  326.             } else if (var9 > 0) {
  327.                var1.fillArc(var5, var6, this.scale, this.scale, var10, var9);
  328.             }
  329.  
  330.             if (this.sliceBorderEnabled) {
  331.                var1.setColor(this.fontColor);
  332.                var1.drawLine(var5 + this.scale / 2, var6 + this.scale / 2, (int)((double)0.5F + (double)var5 + (double)this.scale * ((double)1.0F + Math.cos((double)var10 * 6.28 / (double)360.0F)) / (double)2.0F), (int)((double)0.5F + (double)var6 + (double)this.scale * ((double)1.0F - Math.sin((double)var10 * 6.28 / (double)360.0F)) / (double)2.0F));
  333.                if (var2 == this.itemCount - 1) {
  334.                   var1.drawLine(var5 + this.scale / 2, var6 + this.scale / 2, var5 + this.scale, var6 + this.scale / 2);
  335.                }
  336.             }
  337.  
  338.             if (var9 > 3) {
  339.                int var15 = this.legendFontMetrics.stringWidth("" + Math.round((float)var13) + "%");
  340.                var1.setColor(this.fontColor);
  341.                var1.drawString("" + Math.round((float)var13) + "%", var5 + (int)((double)(this.scale / 2) + (double)(this.scale / 2 + var15) * Math.cos((double)(var10 + var9 / 2) * 6.28 / (double)360.0F)) - var15 / 2, var6 + (int)((double)(this.scale / 2) - (double)(this.scale / 2 + this.legendFontHeight) * Math.sin((double)(var10 + var9 / 2) * 6.28 / (double)360.0F)) + this.legendFontHeight / 2);
  342.                var16 = false;
  343.             } else if (!var16 && var9 > 0) {
  344.                int var24 = this.legendFontMetrics.stringWidth("<1%");
  345.                var1.setColor(this.fontColor);
  346.                var1.drawString("<1%", var5 + (int)((double)(this.scale / 2) + (double)(this.scale / 2 + var24) * Math.cos((double)(var10 + var9 / 2) * 6.28 / (double)360.0F)) - var24 / 2, var6 + (int)((double)(this.scale / 2) - (double)(this.scale / 2 + this.legendFontHeight) * Math.sin((double)(var10 + var9 / 2) * 6.28 / (double)360.0F)) + this.legendFontHeight / 2);
  347.                var16 = true;
  348.             }
  349.  
  350.             var10 += var9;
  351.             if (this.legendEnabled && var2 < var11) {
  352.                var1.setColor((Color)this.colors.elementAt(var2));
  353.                var1.fillRect(var7, var8, this.legendFontHeight, this.legendFontHeight);
  354.                if (this.sliceBorderEnabled) {
  355.                   var1.setColor(this.fontColor);
  356.                   var1.drawRect(var7, var8, this.legendFontHeight - 1, this.legendFontHeight - 1);
  357.                }
  358.  
  359.                var1.setColor(this.fontColor);
  360.  
  361.                try {
  362.                   var1.drawString(this.chartData.getData(var2 + 1, "Items") + "  (" + this.chartData.getData(var2 + 1, "Values") + ")", var7 + 2 * this.legendFontHeight, var8 + this.legendFontHeight - 1);
  363.                } catch (Exception var19) {
  364.                   var1.drawString("Item " + (var2 + 1), var7 + 2 * this.legendFontHeight, var8 + this.legendFontHeight - 1);
  365.                }
  366.  
  367.                var8 += var12;
  368.             }
  369.          }
  370.  
  371.          if (this.sliceBorderEnabled) {
  372.             var1.setColor(this.fontColor);
  373.             var1.drawOval(var5, var6, this.scale, this.scale);
  374.          }
  375.       }
  376.  
  377.    }
  378. }
  379.