home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 May / comcd0502.iso / homepage / javaspecial / 10_01 / charts / piechart / PieChart.java < prev    next >
Encoding:
Java Source  |  1999-10-08  |  8.8 KB  |  289 lines

  1. // PieChart.java  for Drawing Bar Chart.
  2. // Author : Saket Kumar
  3. // Email Id : saket_kumar@hotmail.com
  4.  
  5. import java.awt.*;
  6. import java.io.*;
  7. import java.lang.*;
  8.  
  9. public class PieChart extends java.applet.Applet {
  10.  
  11.     String    title;
  12.     Font        font;
  13.     FontMetrics fontMetrics;
  14.     int        titleHeight = 15;
  15.     int        columns;
  16.     int        values[];
  17.     Color       colors[];
  18.     String      labels[];
  19.     float       percent[];
  20.     float       angle[];
  21.     int        maxLabelWidth = 0;
  22.     int         maxValueWidth = 0;
  23.     int        max = 0;
  24.     int         strWidth=0;
  25.     boolean     showLabel=true;   // Whether to display label or not
  26.     boolean     showPercent=true; // Whether to display percent or not
  27.  
  28.     int lx=0,ly=0;          //For Writing Label
  29.     int cx=0,cy=0;          //Center of Circle
  30.  
  31.   public synchronized void init() {
  32.  
  33.     String temp;
  34.  
  35.     font = new java.awt.Font("Sanserif", Font.BOLD, 12);
  36.     fontMetrics = getFontMetrics(font);
  37.     String bgColor=getParameter("bgcolor"); // Background color of Chart
  38.  
  39.     if (bgColor==null)
  40.        setBackground(Color.white);
  41.     else{
  42.         if (bgColor.equals("red")) {
  43.             setBackground(Color.red);
  44.         } else if (bgColor.equals("green")) {
  45.             setBackground(Color.green);
  46.         } else if (bgColor.equals("blue")) {
  47.             setBackground(Color.blue);
  48.         } else if (bgColor.equals("pink")) {
  49.             setBackground(Color.pink);
  50.         } else if (bgColor.equals("orange")) {
  51.             setBackground(Color.orange);
  52.         } else if (bgColor.equals("magenta")) {
  53.             setBackground(Color.magenta);
  54.         } else if (bgColor.equals("cyan")) {
  55.             setBackground(Color.cyan);
  56.         } else if (bgColor.equals("white")) {
  57.             setBackground(Color.white);
  58.         } else if (bgColor.equals("yellow")) {
  59.             setBackground(Color.yellow);
  60.         } else if (bgColor.equals("gray")) {
  61.             setBackground(Color.gray);
  62.         } else if (bgColor.equals("darkGray")) {
  63.             setBackground(Color.darkGray);
  64.         } else {
  65.         setBackground(Color.white);
  66.         }
  67.     }
  68.  
  69.     title = getParameter("title"); // Title of the Pie Chart
  70.  
  71.     if (title == null) {
  72.         title = "Pie Chart";
  73.     }
  74.  
  75.     temp = getParameter("columns");
  76.     if (temp == null) {
  77.         columns = 5;
  78.     } else {
  79.         columns = Integer.parseInt(temp);
  80.     }
  81.  
  82.     temp = getParameter("showlabel");
  83.     if (temp == null) {
  84.         showLabel = true;
  85.     } else {
  86.         if (temp.equalsIgnoreCase("YES"))
  87.             showLabel = true;
  88.         if (temp.equalsIgnoreCase("NO"))
  89.             showLabel = false;
  90.         else
  91.             showLabel = true;
  92.     }
  93.  
  94.     temp = getParameter("showpercent");
  95.     if (temp == null) {
  96.         showPercent = true;
  97.     } else {
  98.         if (temp.equalsIgnoreCase("YES"))
  99.             showPercent = true;
  100.         if (temp.equalsIgnoreCase("NO"))
  101.             showPercent = false;
  102.         else
  103.             showPercent = true;
  104.     }
  105.  
  106.     values = new int[columns];
  107.     colors = new Color[columns];
  108.     labels = new String[columns];
  109.     percent= new float[columns];
  110.     angle  = new float[columns];
  111.  
  112.     float totalValue=0;
  113.  
  114.     for (int i=0; i < columns; i++) {
  115.  
  116.         temp = getParameter("Pvalue" + (i+1));
  117.         if (temp != null) {
  118.         try {
  119.             values[i] = Integer.parseInt(temp);
  120.         } catch (NumberFormatException e) {
  121.             values[i] = 0;
  122.         }
  123.         }
  124.         totalValue +=  values[i];
  125.         if (values[i] > max) {
  126.         max = values[i];
  127.         }
  128.  
  129.         // parse the label for this column
  130.         temp = getParameter("P" + "label"+ (i+1) );
  131.         labels[i] = (temp == null) ? "" : temp;
  132.         maxLabelWidth = Math.max(fontMetrics.stringWidth((String)(labels[i])),
  133.                      maxLabelWidth);
  134.  
  135.         // parse the color attribute for this column
  136.         temp = getParameter("P" + "color"+ (i+1) );
  137.         if (temp != null) {
  138.         if (temp.equals("red")) {
  139.             colors[i] = Color.red;
  140.         } else if (temp.equals("green")) {
  141.             colors[i] = Color.green;
  142.         } else if (temp.equals("blue")) {
  143.             colors[i] = Color.blue;
  144.         } else if (temp.equals("pink")) {
  145.             colors[i] = Color.pink;
  146.         } else if (temp.equals("orange")) {
  147.             colors[i] = Color.orange;
  148.         } else if (temp.equals("magenta")) {
  149.             colors[i] = Color.magenta;
  150.         } else if (temp.equals("cyan")) {
  151.             colors[i] = Color.cyan;
  152.         } else if (temp.equals("white")) {
  153.             colors[i] = Color.white;
  154.         } else if (temp.equals("yellow")) {
  155.             colors[i] = Color.yellow;
  156.         } else if (temp.equals("gray")) {
  157.             colors[i] = Color.gray;
  158.         } else if (temp.equals("darkGray")) {
  159.             colors[i] = Color.darkGray;
  160.         } else {
  161.             colors[i] = Color.gray;
  162.         }
  163.         } else {
  164.         colors[i] = Color.gray;
  165.         }
  166.     }
  167.     float multiFactor = 100 / totalValue;
  168.  
  169.     for (int i=0; i < columns; i++) {
  170.         percent[i]= values[i] * multiFactor;
  171.         angle[i]  = (float) (percent[i] * 3.6) ;  // Calculation of Angle (360/100)
  172.     }
  173.  
  174.   }
  175.  
  176. // paint method
  177.  
  178.   public synchronized void paint(Graphics g) {
  179.     int  x=0;
  180.     int  y=0;
  181.     int width=0,height=0;
  182.     int ax=0,ay=0;          //For Drawing Black line from center to Peripherial
  183.     int px=0,py=0;          //For Writing Percentage
  184.     int radius=0;
  185.     width=height=Math.min((getSize().width - 100),(getSize().height - 100));
  186.     x=y=50;
  187.  
  188.     if ( getSize().width > width ){
  189.         x = (getSize().width - width ) /2 ; 
  190.     }
  191.  
  192.     cx = x + width/2;
  193.     cy = y + height/2;
  194.     radius = width/2;
  195.  
  196.     // Draw the Title of the Chart on Top of the Applet
  197.  
  198.     strWidth=fontMetrics.stringWidth(title);
  199.     Font fnt = new java.awt.Font("Sanserif", Font.BOLD, 16);
  200.     g.setFont(fnt);
  201.     g.setColor(Color.red);
  202.     g.drawString(title,((getSize().width - strWidth )/2),15);
  203.     g.setFont(font);
  204.     int initAngle=90;
  205.     int sweepAngle=0;
  206.     int incSweepAngle=0;
  207.     int incLabelAngle= (int) (angle[0]/2);
  208.  
  209.     for (int i=0; i < columns; i++) {
  210.         sweepAngle = (int) Math.round(angle[i]);
  211.         g.setColor((Color)colors[i]);
  212.  
  213.         if (i==(columns-1)){
  214.             sweepAngle = 360 - incSweepAngle;
  215.             g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
  216.             g.setColor(Color.black);
  217.             g.drawArc(x,y,width,height,initAngle,(-sweepAngle));
  218.  
  219.             if (showLabel){
  220.                 lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
  221.                 ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
  222.                 adjustLabel(i);
  223.                 g.drawString((String)labels[i],lx,ly);
  224.             }
  225.             if (showPercent){
  226.                 px = (int) (cx + ((radius*2/3) * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
  227.                 py = (int) (cy + ((radius*2/3) * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
  228.                 g.drawString(String.valueOf(Math.round(percent[i]))+"%",px,py); 
  229.             }
  230.             break;
  231.         }
  232.  
  233.         g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
  234.         g.setColor(Color.black);
  235.         g.drawArc(x,y,width,height,initAngle,(-sweepAngle));
  236.         incSweepAngle +=sweepAngle;
  237.  
  238.         ax = (int) (cx + ( radius * Math.cos((incSweepAngle * 3.14f/180) - 3.14f/2)));
  239.         ay = (int) (cy + ( radius * Math.sin((incSweepAngle * 3.14f/180) - 3.14f/2)));
  240.         g.drawLine(cx,cy,ax,ay);
  241.  
  242.         if (showLabel){
  243.             lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
  244.             ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
  245.             adjustLabel(i);
  246.             g.drawString((String)labels[i],lx,ly);
  247.         }
  248.         if (showPercent){
  249.             px = (int) (cx + ((radius*2/3) * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
  250.             py = (int) (cy + ((radius*2/3) * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
  251.             strWidth = fontMetrics.stringWidth(Math.round(percent[i])+"%");
  252.             g.drawString(String.valueOf(Math.round(percent[i]))+"%",(px - strWidth/2),py);
  253.         }
  254.  
  255.         incLabelAngle = incLabelAngle + (int) (angle[i]/2 + angle[i+1]/2);
  256.         initAngle += (-sweepAngle);
  257.     }
  258.     g.setColor(Color.black);
  259.     g.drawLine(cx,cy,cx,cy-radius);
  260.   }
  261.  
  262.   private void adjustLabel(int i){
  263.     if ( (lx > cx) && (ly < cy) ){
  264.         lx +=5;
  265.         ly -=5;
  266.     }
  267.  
  268.     if ( (lx > cx) && (ly > cy) ){
  269.         lx +=5;
  270.         ly +=10;
  271.     }
  272.  
  273.     if ( (lx < cx) && (ly > cy) ){
  274.         strWidth=fontMetrics.stringWidth(labels[i]);
  275.         lx -= strWidth+5;
  276.         if (lx < 0)
  277.             lx=0;
  278.     }
  279.  
  280.     if ( (lx < cx) && (ly < cy) ){
  281.         strWidth=fontMetrics.stringWidth(labels[i]);
  282.         lx -= strWidth+5;
  283.         if (lx < 0)
  284.             lx=0;
  285.     }
  286.   }
  287.  
  288. }
  289.