Quick Search for:  in language:    
VOTE,Just,cool,chart,ANYWAYS,please
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Java/ Javascript Stats

 Code: 220,465. lines
 Jobs: 92. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Java/ Javascript.
Click here to see a screenshot of this code!vok - The vocabulary trainer
By Thorsten Stärk on 1/7

(Screen Shot)

Java, Calculator
By Rockwell on 1/4


Eatable Interface
By Rockwell on 1/4


Superclass Person
By Rockwell on 1/4


Draws Cube Function
By Rockwell on 1/4


Rectangle Class
By Rockwell on 1/4


Find Number of Upper and Lower Case Letters in a Command Line Argument String
By Rockwell on 1/4


anagrams
By Rockwell on 1/4


Text Reader with Tokenizer
By Rockwell on 1/4


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Java Pie Chart

Print
Email
 
VB icon
Submitted on: 1/30/2001 10:03:55 PM
By: Mike McNaughton  
Level: Intermediate
User Rating: By 3 Users
Compatibility:Java (JDK 1.2), JavaScript

Users have accessed this code 14234 times.
 
(About the author)
 
     Just a cool pie chart... VOTE FOR ME ANYWAYS, please! :)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: Java Pie Chart
// Description:Just a cool pie chart... 
//     VOTE FOR ME ANYWAYS, please! :)
// By: Mike McNaughton
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=2085&lngWId;=2//for details.//**************************************
//     

/* Pie chart Applet */
/* this program is in the public domain. 
*/
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.lang.Math;
// =====================================
//     ===========================
// A struct class to hold data for one w
//     edge

    class PieItem {
    public double frac; // each one has a number
    public String label; // and a label
    PieItem (String s) { // constructor
    StringTokenizer t = new StringTokenizer(s, ",");
    frac = Double.valueOf(t.nextToken()).doubleValue();
    label = t.nextToken();
} // constructor

} // PieItem
//The view of the pie. class PieView extends Canvas { PieItem[] wedges; // The data for the pie double total = 0.0; // Total of all wedges static final int ncolors = 5; Color wedgeColor[] = new Color[5]; int pieViewSize; // size of square to incise pie into static final int pieBorderWidth = 10; // pixels from circle edge to side int pieDiameter; // derived from the view size int pieRadius; // .. int pieCenterPos; // .. public PieView(int asize, PieItem[] avec) { // constructor this.pieViewSize = asize; // copy args this.wedges = avec; pieDiameter = pieViewSize-2*pieBorderWidth; pieRadius = pieDiameter/2; pieCenterPos = pieBorderWidth+pieRadius; this.setFont(new Font("Helvetica",Font.BOLD,12)); this.setBackground(Color.white); for (int i = 0; i<wedges.length; i++) { total += wedges[i].frac; }
wedgeColor[0] = Color.green; // colors that black looks good on wedgeColor[1] = Color.pink; wedgeColor[2] = Color.cyan; wedgeColor[3] = Color.red; wedgeColor[4] = Color.yellow; } // constructor
public void paint(Graphics g) { int startDeg = 0; int arcDeg; int x, y; double angleRad; g.setColor(Color.lightGray); // shadow g.fillOval(pieBorderWidth+3,pieBorderWidth+3,pieDiameter,pieDiameter); g.setColor(Color.gray); // "other" is gray g.fillOval(pieBorderWidth,pieBorderWidth,pieDiameter,pieDiameter); int wci = 0; int i; for (i = 0; i<THis.wedges.length; i++) { // draw wedges arcDeg = (int)((this.wedges[i].frac / total) * 360); g.setColor(wedgeColor[wci++]); g.fillArc(pieBorderWidth,pieBorderWidth,pieDiameter,pieDiameter, startDeg, arcDeg); if (wci >= ncolors) { wci = 0; // rotate colors }
startDeg += arcDeg; } // draw wedges
startDeg = 0; // do labels so they go on top of the wedges. for (i = 0; i<THis.wedges.length; i++) { arcDeg = (int)((this.wedges[i].frac / total) * 360); if (arcDeg > 3) { // don't label small wedges g.setColor(Color.black); angleRad = (float) (startDeg+(arcDeg/2))* java.lang.Math.PI / 180.0; x = pieCenterPos + (int)((pieRadius/1.3)*java.lang.Math.cos(angleRad)); y = pieCenterPos - (int)((pieRadius/1.3)*java.lang.Math.sin(angleRad)) + 5; // 5 is about half the height of the text g.drawString(this.wedges[i].label, x, y); } // don't label small wedges
startDeg += arcDeg; } // for
} // paint()
public Dimension preferredSize () { return new Dimension (pieViewSize,pieViewSize); } // preferredSize
} // PieView
// ===================================== // =========================== // The Pie chart applet public class Pie extends Applet { private PieView the_pie = null; public Pie() { // constructor // Nothing happens here, can't get args // yet. } // constructor
public void init () { String stemp; double dtemp; int i; // Read the applet arguments stemp = this.getParameter("title"); String chartTitle = (stemp == null) ? "" : stemp; stemp = this.getParameter("subtitle"); String chartSubTitle = (stemp == null) ? "" : stemp; int nargs = 0; while (this.getParameter("arg"+nargs) != null) { nargs++; // just count the arguments } // while
PieItem[] v = new PieItem[nargs]; // allocate storage for (i=0; i<nargs; i++) { v[i] = new PieItem(getParameter("arg"+i)); // parse argument } // for
int d = (i+1)/2; // Shell sort do { for (i=0; i < nargs-d; i++) { if (v[i].frac < v[i+d].frac) { dtemp = v[i].frac; // swap v[i].frac = v[i+d].frac; v[i+d].frac = dtemp; stemp = v[i].label; v[i].label = v[i+d].label; v[i+d].label = stemp; } } // for
d -= 1; } while (d > 0);
int h = this.size().height; int w = this.size().width; the_pie = new PieView(h-50, v); // shd be min(h-50,w)? this.setLayout(new BorderLayout(0,0)); this.setBackground(Color.white); this.add("Center", the_pie); this.add("North", new Label(chartTitle)); this.add("South", new Label(chartSubTitle)); } // init
// Boiler plate for HotJava public String getAppletInfo () { return "Pie 1997-06-14 THVV"; } // getAppletInfo
public String [][] getParameterInfo () { String [][] info = { };
return info; } // getParameterInfo
// Main program for testing only. public static void main(String args[]) { Frame f = new Frame("Pie"); Pie p = new Pie(); p.init(); p.start(); f.add("Center",p); f.resize(512,512); f.show(); } // main
} // Pie


Other 16 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
2/4/2001 8:50:44 AM:Nice work
Nice i liked it needs a little 
modification thats all...
mahesh 
vhatkar
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/13/2001 6:49:18 PM:Devon
How does this thing work?  I'm not a 
very experienced Java programmer, some 
basic instructions of use would be 
helpful.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Java/ Javascript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.