Back to index

wims  3.65+svn20090927
Public Member Functions | Public Attributes | Package Attributes | Private Member Functions
PieChart Class Reference
Collaboration diagram for PieChart:
Collaboration graph
[legend]

List of all members.

Public Member Functions

synchronized void init ()
synchronized void paint (Graphics g)
String ReadApplet ()
void SetApplet (String i)

Public Attributes

int round = 1000
float values []
String labels []
float percent []
float angle []
float degree []
float radian []
float rawinput []
boolean showLabel = true
boolean showPercent = false
boolean showRadians = false
boolean showDegrees = false
boolean showInput = true

Package Attributes

String title
Font font
FontMetrics fontMetrics
int titleHeight = 15
int columns
Color colors []
int maxLabelWidth = 0
int maxValueWidth = 0
int max = 0
int strWidth = 0
String unit
int lx = 0
int ly = 0
int cx = 0
int cy = 0

Private Member Functions

void adjustLabel (int i)

Detailed Description

Definition at line 41 of file PieChart.java.


Member Function Documentation

void PieChart.adjustLabel ( int  i) [inline, private]

Definition at line 360 of file PieChart.java.

                                 {
    if ( (lx > cx) && (ly < cy) ){
        lx +=5;
        ly -=5;
    }

    if ( (lx > cx) && (ly > cy) ){
        lx +=5;
        ly +=10;
    }

    if ( (lx < cx) && (ly > cy) ){
        strWidth=fontMetrics.stringWidth(labels[i]);
        lx -= strWidth+5;
        if (lx < 0)
            lx=0;
    }

    if ( (lx < cx) && (ly < cy) ){
        strWidth=fontMetrics.stringWidth(labels[i]);
        lx -= strWidth+5;
        if (lx < 0)
           lx=0;
    }
  }

Here is the caller graph for this function:

synchronized void PieChart.init ( ) [inline]

Definition at line 71 of file PieChart.java.

                                  {
    String temp;font = new java.awt.Font("Sanserif", Font.BOLD, 12);
    fontMetrics = getFontMetrics(font);
    String bgColor=getParameter("bgcolor"); // Background color of Chart
    if (bgColor==null)
       setBackground(Color.white);
    else{
        if (bgColor.equals("red")) {
            setBackground(Color.red);
        } else if (bgColor.equals("green")) {
            setBackground(Color.green);
        } else if (bgColor.equals("blue")) {
            setBackground(Color.blue);
        } else if (bgColor.equals("pink")) {
            setBackground(Color.pink);
        } else if (bgColor.equals("orange")) {
            setBackground(Color.orange);
        } else if (bgColor.equals("magenta")) {
            setBackground(Color.magenta);
        } else if (bgColor.equals("cyan")) {
            setBackground(Color.cyan);
        } else if (bgColor.equals("white")) {
            setBackground(Color.white);
        } else if (bgColor.equals("yellow")) {
            setBackground(Color.yellow);
        } else if (bgColor.equals("gray")) {
            setBackground(Color.gray);
        } else if (bgColor.equals("darkGray")) {
            setBackground(Color.darkGray);
           } else {
        setBackground(Color.white);
           }
    }
    title = getParameter("title"); // Title of the Pie Chart
    if (title == null) {title = "WIMS Pie Chart";}

    unit = getParameter("unit"); // Title of the Pie Chart
    if (unit == null) {unit = "";}
    
    temp = getParameter("columns");
    if (temp == null) { columns = 5;} else {columns = Integer.parseInt(temp);}

    temp = getParameter("rounding");
    if (temp != null){round = Integer.parseInt(temp);if ( round < 1){ round=100;}}

    temp = getParameter("showdegrees");
    if (temp == null){showDegrees = false;}
    else
    {
       if (temp.equalsIgnoreCase("YES")){showDegrees = true;}
       if (temp.equalsIgnoreCase("NO")){showDegrees = false;}
    }
    temp = getParameter("showradians");
    if (temp == null){showRadians = false;}
    else
    {
       if (temp.equalsIgnoreCase("YES")){showRadians = true;}
       if (temp.equalsIgnoreCase("NO")){showRadians = false;}
    }

    temp = getParameter("showinput");
    if (temp == null){showInput = true;}
    else
    {
       if (temp.equalsIgnoreCase("YES")){showInput = true;}
       if (temp.equalsIgnoreCase("NO")){showInput = false;}
    }

    temp = getParameter("showlabel");
    if (temp == null) {showLabel = false;} 
    else 
    {
       if (temp.equalsIgnoreCase("YES")){showLabel = true;}
       if (temp.equalsIgnoreCase("NO")){showLabel = false;}
    }

    temp = getParameter("showpercent");
    if (temp == null) {showPercent = false;}
    else
    { 
        if (temp.equalsIgnoreCase("YES")){showPercent = true;}
        if (temp.equalsIgnoreCase("NO")){showPercent = false;}
    }
    
    radian = new float[columns];
    degree = new float[columns];
    rawinput = new float[columns];
    values = new float[columns];
    colors = new Color[columns];
    labels = new String[columns];
    percent= new float[columns];
    angle  = new float[columns];
    
    
    String tmp=null; 
    float totalValue=0;
    for (int i=0; i < columns; i++) {
       tmp=getParameter("Pvalue" + (i+1));
       float f = Float.valueOf(tmp.trim()).floatValue();
        values[i] =f;
       float t = Float.valueOf(tmp).floatValue();
       rawinput[i]=t;
        totalValue +=  values[i];
       // parse the label for this column
        temp = getParameter("P" + "label"+ (i+1) );
        labels[i] = (temp == null) ? "" : temp;
        maxLabelWidth = Math.max(fontMetrics.stringWidth((String)(labels[i])),maxLabelWidth);
       // parse the color attribute for this column
        temp = getParameter("P" + "color"+ (i+1) );
        if (temp != null) {
        if (temp.equals("red")) {
                  colors[i] = Color.red;
        } else if (temp.equals("green")) {
                  colors[i] = Color.green;
        } else if (temp.equals("blue")) {
                  colors[i] = Color.blue;
        } else if (temp.equals("pink")) {
                  colors[i] = Color.pink;
        } else if (temp.equals("orange")) {
                  colors[i] = Color.orange;
        } else if (temp.equals("magenta")) {
                  colors[i] = Color.magenta;
        } else if (temp.equals("cyan")) {
                  colors[i] = Color.cyan;
        } else if (temp.equals("white")) {
                  colors[i] = Color.white;
        } else if (temp.equals("yellow")) {
                  colors[i] = Color.yellow;
        } else if (temp.equals("gray")) {
                  colors[i] = Color.gray;
        } else if (temp.equals("darkGray")) {
                  colors[i] = Color.darkGray;
              } else {
                  colors[i] = Color.gray;
              }
           } else {
              colors[i] = Color.gray;
           }
    }
    float multiFactor = 100 / totalValue;

       for (int i=0; i < columns; i++) {
        percent[i]= (float) (Math.round(round*values[i] * multiFactor))/round;
        angle[i]  = (float) (Math.round(round*3.6*values[i] * multiFactor))/round;  // Calculation of Angle (360/100)
       degree[i] = (float) (Math.round(round*percent[i] * 3.6))/round ;
       radian[i] = (float) (Math.round(round*angle[i]/180))/round; // * pi rad !!!
    }

  }
synchronized void PieChart.paint ( Graphics  g) [inline]

Definition at line 223 of file PieChart.java.

                                             {
    int  x=0;
    int  y=0;
    int width=0,height=0;
    int ax=0,ay=0;          //For Drawing Black line from center to Peripherial
    int px=0,py=0;          //For Writing Percentage
    int radius=0;
    width=height=Math.min((getSize().width - 100),(getSize().height - 100));
    x=y=50;

    if ( getSize().width > width ){
        x = (getSize().width - width ) /2 ; 
    }

    cx = x + width/2;
    cy = y + height/2;
    radius = width/2;

    // Draw the Title of the Chart on Top of the Applet

    strWidth=fontMetrics.stringWidth(title);
    Font fnt = new java.awt.Font("Sanserif", Font.BOLD, 16);
    g.setFont(fnt);
    g.setColor(Color.red);
    g.drawString(title,((getSize().width - strWidth )/2),15);
    g.setFont(font);
    int initAngle=90;
    int sweepAngle=0;
    int incSweepAngle=0;
    int incLabelAngle= (int) (angle[0]/2);

    for (int i=0; i < columns; i++) {
        sweepAngle = (int) Math.round(angle[i]);
        g.setColor((Color)colors[i]);

        if (i==(columns-1)){
            sweepAngle = 360 - incSweepAngle;
            g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
            g.setColor(Color.black);
            g.drawArc(x,y,width,height,initAngle,(-sweepAngle));

            lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
            ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
           adjustLabel(i);
           int ly2=ly+15;

            if (showLabel)
           {
              g.drawString((String)labels[i] ,lx,ly);
              
              if (showDegrees){g.drawString(String.valueOf(degree[i]) + "\u00b0" ,lx,ly2);}
              
              else
              { 
                  if (showRadians){g.drawString( String.valueOf(radian[i]) +"\u03c0 rad" ,lx,ly2);}
                  else
                  {
                     if (showPercent){g.drawString( String.valueOf(percent[i]) +" %" ,lx,ly2);}
                     else
                     {
                         if (showInput){g.drawString( String.valueOf(rawinput[i])+"  "+unit,lx,ly2);}
                     }
                  }
              }
            }
           else
           {
              if (showDegrees){g.drawString(String.valueOf(degree[i]) + "\u00b0" ,lx,ly2);}
              
              else
              { 
                  if (showRadians){g.drawString( String.valueOf(radian[i]) +"\u03c0 rad" ,lx,ly2);}
                  else
                  {
                     if (showPercent){g.drawString( String.valueOf((Math.round(round*percent[i]))/round) +" %" ,lx,ly2);}
                     else
                     {
                         if (showInput){g.drawString( String.valueOf(rawinput[i])+"  "+ unit,lx,ly2);}
                     }
                  }
              }
           }
            break;
        }

        g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
        g.setColor(Color.black);
        g.drawArc(x,y,width,height,initAngle,(-sweepAngle));
        incSweepAngle +=sweepAngle;

        ax = (int) (cx + ( radius * Math.cos((incSweepAngle * 3.14f/180) - 3.14f/2)));
        ay = (int) (cy + ( radius * Math.sin((incSweepAngle * 3.14f/180) - 3.14f/2)));
        g.drawLine(cx,cy,ax,ay);

        lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
        ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
        adjustLabel(i);
       int ly2=ly+15;               
       if (showLabel){
            g.drawString((String)labels[i],lx,ly);
           if ( showDegrees){g.drawString(String.valueOf(degree[i]) + "\u00b0" ,lx,ly2);}
           else
           {
              if ( showRadians){g.drawString(String.valueOf(radian[i])+"\u03c0 rad" ,lx,ly2);}
              else
              {
                  if (showPercent){g.drawString( String.valueOf(percent[i]) +" %" ,lx,ly2);}
                  else
                  {
                     if (showInput){g.drawString( String.valueOf(rawinput[i])+"  "+unit,lx,ly2);}
                  }
              }
           }
       }
       else
       {
           if ( showDegrees){g.drawString(String.valueOf(degree[i]) + "\u00b0" ,lx,ly2);}
           else
           {
              if ( showRadians){g.drawString(String.valueOf(radian[i])+"\u03c0 rad" ,lx,ly2);}
              else
              {
                  if (showPercent){g.drawString( String.valueOf((Math.round(round*percent[i]))/round) +" %" ,lx,ly2);}
                  else
                  {
                     if (showInput){g.drawString( String.valueOf(rawinput[i])+"  "+unit,lx,ly2);}
                  }
              }      
           }
       }
        incLabelAngle = incLabelAngle + (int) (angle[i]/2 + angle[i+1]/2);
        initAngle += (-sweepAngle);
    }
    g.setColor(Color.black);
    g.drawLine(cx,cy,cx,cy-radius);
  }

Here is the call graph for this function:

String PieChart.ReadApplet ( ) [inline]

Definition at line 406 of file PieChart.java.

                            {
  // ATTENTION  MSIE-6 ? will not accept an array[]:it must be a string!!!
    int len=values.length;String reply="";
    if(len==0){
       reply="error empty answer";
    }
    else
    {
       float p;String l="";
       for(int i=0 ; i<len;i++){
           l=labels[i];
           p=rawinput[i];
           reply=reply+"segment "+i+" [ "+l+" ] = "+p+"\n";
       }
    }
    return reply;
  }
void PieChart.SetApplet ( String  i) [inline]

Definition at line 428 of file PieChart.java.

                                  {
    String tmp="";float input;
    StringTokenizer R = new StringTokenizer(i,",");
    int max=R.countTokens();
    float value[]=new float[max];float sum=0;
    for(int p=0;p<max;p++){
       tmp=R.nextToken();
       if(tmp.length()==0){tmp="0";}
       input=Float.valueOf(tmp).floatValue();
       System.out.println("input = " + input +"\n"); 
       rawinput[p]=input;
       value[p]=input;
       sum=sum+input;
    }
    for(int p=0;p<max;p++){
       input=value[p];
       // we show the correct circlediagram...maybe the sum of the degrees were != 360 ...we don't care
       angle[p] = 360*input/sum;
       percent[p] = 100*input/sum;
       radian[p] = (float) (input/sum);
       degree[p] = 360*input/sum;
    }
    repaint(); 
  }

Member Data Documentation

float PieChart.angle[]

Definition at line 53 of file PieChart.java.

Color PieChart.colors[] [package]

Definition at line 50 of file PieChart.java.

int PieChart.columns [package]

Definition at line 47 of file PieChart.java.

int PieChart.cx = 0 [package]

Definition at line 69 of file PieChart.java.

int PieChart.cy = 0 [package]

Definition at line 69 of file PieChart.java.

float PieChart.degree[]

Definition at line 54 of file PieChart.java.

Font PieChart.font [package]

Definition at line 44 of file PieChart.java.

FontMetrics PieChart.fontMetrics [package]

Definition at line 45 of file PieChart.java.

String PieChart.labels[]

Definition at line 51 of file PieChart.java.

int PieChart.lx = 0 [package]

Definition at line 68 of file PieChart.java.

int PieChart.ly = 0 [package]

Definition at line 68 of file PieChart.java.

int PieChart.max = 0 [package]

Definition at line 59 of file PieChart.java.

int PieChart.maxLabelWidth = 0 [package]

Definition at line 57 of file PieChart.java.

int PieChart.maxValueWidth = 0 [package]

Definition at line 58 of file PieChart.java.

Definition at line 52 of file PieChart.java.

float PieChart.radian[]

Definition at line 55 of file PieChart.java.

Definition at line 56 of file PieChart.java.

int PieChart.round = 1000

Definition at line 48 of file PieChart.java.

Definition at line 64 of file PieChart.java.

Definition at line 65 of file PieChart.java.

Definition at line 61 of file PieChart.java.

Definition at line 62 of file PieChart.java.

Definition at line 63 of file PieChart.java.

int PieChart.strWidth = 0 [package]

Definition at line 60 of file PieChart.java.

String PieChart.title [package]

Definition at line 43 of file PieChart.java.

int PieChart.titleHeight = 15 [package]

Definition at line 46 of file PieChart.java.

String PieChart.unit [package]

Definition at line 66 of file PieChart.java.

float PieChart.values[]

Definition at line 49 of file PieChart.java.


The documentation for this class was generated from the following file: