home *** CD-ROM | disk | FTP | other *** search
- import allaire.dcf.recordset.AppletParamRecordset;
- import allaire.dcf.recordset.Query;
- import allaire.dcf.recordset.Recordset;
- import allaire.util.Debug;
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.util.Date;
- import java.util.Vector;
-
- public class PieChart extends Applet implements Runnable {
- Thread runner;
- Query query;
- int refreshTime;
- boolean debugInfoEnabled;
- String title;
- String titleFontName;
- Font titleFont;
- FontMetrics titleFontMetrics;
- int titleFontHeight;
- String legendFontName;
- Font legendFont;
- FontMetrics legendFontMetrics;
- int legendFontHeight;
- int legendHeight;
- int legendWidth;
- boolean legendEnabled;
- boolean sliceBorderEnabled;
- boolean showDateTime;
- Color backgroundColor;
- Color fontColor;
- Color[] defaultColors;
- Vector colors;
- Recordset chartData;
- int itemCount;
- int scale;
- int maxLegendWidth;
- double sum;
-
- public void start() {
- if (this.runner == null) {
- this.runner = new Thread(this);
- this.runner.start();
- }
-
- }
-
- public PieChart() {
- this.defaultColors = new Color[]{Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.cyan, Color.orange, Color.pink, Color.darkGray};
- }
-
- public void stop() {
- if (this.runner != null) {
- this.runner.stop();
- this.runner = null;
- }
-
- }
-
- private void getData() {
- this.sum = (double)0.0F;
-
- try {
- if (this.chartData == null) {
- this.chartData = new AppletParamRecordset(this, "ChartData");
- } else {
- this.query.execute();
- this.chartData = this.query.getRecordset();
- }
-
- if (this.refreshTime == 0) {
- ((Applet)this).getAppletContext().showStatus("Done");
- } else {
- ((Applet)this).getAppletContext().showStatus("...");
- }
-
- this.itemCount = this.chartData.getRowCount();
- this.colors.setSize(this.itemCount);
-
- for(int var1 = 0; var1 < this.itemCount; ++var1) {
- this.maxLegendWidth = Math.max(this.legendFontMetrics.stringWidth(this.chartData.getData(var1 + 1, "Items") + " (" + this.chartData.getData(var1 + 1, "Values") + ")"), this.maxLegendWidth);
-
- try {
- this.sum += Math.abs(new Double(this.chartData.getData(var1 + 1, "Values")));
- } catch (Exception var5) {
- this.sum += (double)0.0F;
- }
-
- if (!this.chartData.columnExists("Colors")) {
- if (var1 < 9) {
- this.colors.setElementAt(this.defaultColors[var1], var1);
- } else if (var1 < 18) {
- this.colors.setElementAt(this.defaultColors[var1 - 9].darker(), var1);
- } else if (var1 < 27) {
- this.colors.setElementAt(this.defaultColors[var1 - 18].darker().darker(), var1);
- } else {
- this.colors.setElementAt(Color.black, var1);
- }
- } else {
- String var2 = this.chartData.getData(var1 + 1, "Colors");
- if (var2.equals("red")) {
- this.colors.setElementAt(Color.red, var1);
- } else if (var2.equals("green")) {
- this.colors.setElementAt(Color.green, var1);
- } else if (var2.equals("blue")) {
- this.colors.setElementAt(Color.blue, var1);
- } else if (var2.equals("pink")) {
- this.colors.setElementAt(Color.pink, var1);
- } else if (var2.equals("orange")) {
- this.colors.setElementAt(Color.orange, var1);
- } else if (var2.equals("magenta")) {
- this.colors.setElementAt(Color.magenta, var1);
- } else if (var2.equals("cyan")) {
- this.colors.setElementAt(Color.cyan, var1);
- } else if (var2.equals("white")) {
- this.colors.setElementAt(Color.white, var1);
- } else if (var2.equals("yellow")) {
- this.colors.setElementAt(Color.yellow, var1);
- } else if (var2.equals("gray")) {
- this.colors.setElementAt(Color.gray, var1);
- } else if (var2.equals("darkGray")) {
- this.colors.setElementAt(Color.darkGray, var1);
- } else {
- this.colors.setElementAt(Color.black, var1);
- }
- }
- }
-
- } catch (Exception var6) {
- if (this.debugInfoEnabled) {
- Debug.write(((Throwable)var6).getMessage());
- }
-
- }
- }
-
- private void pause(int var1) {
- try {
- Thread.sleep((long)var1);
- } catch (InterruptedException var4) {
- if (this.debugInfoEnabled) {
- Debug.write(((Throwable)var4).getMessage());
- }
-
- }
- }
-
- public void run() {
- while(this.refreshTime != 0) {
- this.pause(this.refreshTime * 1000);
- this.getData();
- ((Component)this).repaint();
- }
-
- this.stop();
- }
-
- public synchronized void init() {
- String var1 = ((Applet)this).getParameter("RefreshTime");
- if (var1 == null) {
- this.refreshTime = 0;
- } else {
- this.refreshTime = Integer.parseInt(var1);
- var1 = ((Applet)this).getParameter("RefreshDataFromURL");
- if (var1 == null) {
- this.refreshTime = 0;
- } else {
- if (var1.indexOf(63) == -1) {
- var1 = var1 + '?';
- }
-
- this.query = new Query(var1);
- this.query.addParam("RefreshTime", String.valueOf(this.refreshTime));
- }
- }
-
- var1 = ((Applet)this).getParameter("DebugInfoEnabled");
- if (var1 == null) {
- this.debugInfoEnabled = false;
- } else if (var1.toLowerCase().equals("yes")) {
- this.debugInfoEnabled = true;
- } else {
- this.debugInfoEnabled = false;
- }
-
- this.title = ((Applet)this).getParameter("Title");
- if (this.title == null) {
- this.title = "Chart";
- }
-
- this.titleFontName = ((Applet)this).getParameter("TitleFontName");
- if (this.titleFontName == null) {
- this.titleFontName = "TimesRoman";
- }
-
- var1 = ((Applet)this).getParameter("TitleFontHeight");
- if (var1 == null) {
- this.titleFontHeight = 12;
- } else {
- this.titleFontHeight = Integer.parseInt(var1);
- }
-
- this.legendFontName = ((Applet)this).getParameter("LegendFontName");
- if (this.legendFontName == null) {
- this.legendFontName = "TimesRoman";
- }
-
- var1 = ((Applet)this).getParameter("LegendFontHeight");
- if (var1 == null) {
- this.legendFontHeight = 10;
- } else {
- this.legendFontHeight = Integer.parseInt(var1);
- }
-
- var1 = ((Applet)this).getParameter("ShowLegend");
- if (var1 == null) {
- this.legendEnabled = true;
- } else if (var1.toLowerCase().equals("yes")) {
- this.legendEnabled = true;
- } else {
- this.legendEnabled = false;
- }
-
- var1 = ((Applet)this).getParameter("DrawBorders");
- if (var1 == null) {
- this.sliceBorderEnabled = false;
- } else if (var1.toLowerCase().equals("yes")) {
- this.sliceBorderEnabled = true;
- } else {
- this.sliceBorderEnabled = false;
- }
-
- var1 = ((Applet)this).getParameter("ShowDateTime");
- if (var1 == null) {
- this.showDateTime = true;
- } else if (var1.toLowerCase().equals("yes")) {
- this.showDateTime = true;
- } else {
- this.showDateTime = false;
- }
-
- var1 = ((Applet)this).getParameter("BackgroundColor");
- if (var1 == null) {
- this.backgroundColor = ((Component)this).getBackground();
- } else {
- this.backgroundColor = new Color(Integer.valueOf(var1, 16));
- }
-
- var1 = ((Applet)this).getParameter("FontColor");
- if (var1 == null) {
- this.fontColor = Color.black;
- } else {
- this.fontColor = new Color(Integer.valueOf(var1, 16));
- }
-
- ((Component)this).setBackground(this.backgroundColor);
- this.titleFont = new Font(this.titleFontName, 1, this.titleFontHeight);
- this.titleFontMetrics = ((Component)this).getFontMetrics(this.titleFont);
- this.legendFont = new Font(this.legendFontName, 0, this.legendFontHeight);
- this.legendFontMetrics = ((Component)this).getFontMetrics(this.legendFont);
- this.colors = new Vector();
- this.getData();
- }
-
- public synchronized void paint(Graphics var1) {
- int var5 = 0;
- int var6 = 0;
- int var7 = 0;
- int var8 = 0;
- int var10 = 0;
- double var13 = (double)0.0F;
- boolean var16 = false;
- if (this.itemCount > 0) {
- int var3 = ((Component)this).size().width / 15;
- int var4 = Math.max(((Component)this).size().height / 15, 2 * this.legendFontHeight);
- int var11;
- int var12;
- if (this.legendEnabled) {
- var11 = Math.min((((Component)this).size().height - this.titleFontHeight - 3 * var4) / this.legendFontHeight, this.itemCount);
- var12 = (((Component)this).size().height - this.titleFontHeight - 3 * var4) / var11;
- if (var12 > 2 * this.titleFontHeight) {
- var12 = 2 * this.titleFontHeight;
- }
-
- this.legendWidth = this.legendFontHeight * 2 + this.maxLegendWidth + var3;
- this.legendHeight = var11 * var12;
- var7 = ((Component)this).size().width - this.legendWidth;
- var8 = (((Component)this).size().height - 3 * var4 - this.titleFontHeight - this.legendHeight) / 2 + var4;
- var1.setColor(this.fontColor);
- var1.drawRect(var7 - this.legendFontHeight, var8 - var12 / 2, this.legendWidth - var3 + 2 * this.legendFontHeight, this.legendHeight + var12 / 2);
- } else {
- var12 = 0;
- var11 = 0;
- this.legendWidth = this.legendHeight = 0;
- }
-
- this.scale = Math.max(Math.min(((Component)this).size().width - this.legendWidth - 3 * var3, ((Component)this).size().height - this.titleFontHeight - 3 * var4), 10);
- var1.setColor(this.fontColor);
- var1.setFont(this.titleFont);
- var1.drawString(this.title, Math.max((((Component)this).size().width - this.titleFontMetrics.stringWidth(this.title)) / 2, 0), ((Component)this).size().height - var4);
- var1.setFont(this.legendFont);
- if (this.showDateTime) {
- String var17 = (new Date()).toLocaleString();
- 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);
- }
-
- var5 = (((Component)this).size().width - this.legendWidth - 3 * var3 - this.scale) / 2 + var3 * 3 / 2;
- var6 = (((Component)this).size().height - this.titleFontHeight - 3 * var4 - this.scale) / 2 + var4;
-
- for(int var2 = 0; var2 < this.itemCount; ++var2) {
- try {
- var13 = Math.abs((double)100.0F * new Double(this.chartData.getData(var2 + 1, "Values")) / this.sum);
- } catch (Exception var20) {
- var13 = (double)0.0F;
- }
-
- int var9 = (int)Math.round((double)360.0F * var13 / (double)100.0F);
- var1.setColor((Color)this.colors.elementAt(var2));
- if (var2 == this.itemCount - 1) {
- if (var10 < 360) {
- var1.fillArc(var5, var6, this.scale, this.scale, var10, 360 - var10);
- }
- } else if (var9 > 0) {
- var1.fillArc(var5, var6, this.scale, this.scale, var10, var9);
- }
-
- if (this.sliceBorderEnabled) {
- var1.setColor(this.fontColor);
- 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));
- if (var2 == this.itemCount - 1) {
- var1.drawLine(var5 + this.scale / 2, var6 + this.scale / 2, var5 + this.scale, var6 + this.scale / 2);
- }
- }
-
- if (var9 > 3) {
- int var15 = this.legendFontMetrics.stringWidth("" + Math.round((float)var13) + "%");
- var1.setColor(this.fontColor);
- 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);
- var16 = false;
- } else if (!var16 && var9 > 0) {
- int var24 = this.legendFontMetrics.stringWidth("<1%");
- var1.setColor(this.fontColor);
- 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);
- var16 = true;
- }
-
- var10 += var9;
- if (this.legendEnabled && var2 < var11) {
- var1.setColor((Color)this.colors.elementAt(var2));
- var1.fillRect(var7, var8, this.legendFontHeight, this.legendFontHeight);
- if (this.sliceBorderEnabled) {
- var1.setColor(this.fontColor);
- var1.drawRect(var7, var8, this.legendFontHeight - 1, this.legendFontHeight - 1);
- }
-
- var1.setColor(this.fontColor);
-
- try {
- var1.drawString(this.chartData.getData(var2 + 1, "Items") + " (" + this.chartData.getData(var2 + 1, "Values") + ")", var7 + 2 * this.legendFontHeight, var8 + this.legendFontHeight - 1);
- } catch (Exception var19) {
- var1.drawString("Item " + (var2 + 1), var7 + 2 * this.legendFontHeight, var8 + this.legendFontHeight - 1);
- }
-
- var8 += var12;
- }
- }
-
- if (this.sliceBorderEnabled) {
- var1.setColor(this.fontColor);
- var1.drawOval(var5, var6, this.scale, this.scale);
- }
- }
-
- }
- }
-