Piechart.java.

Java To Go!
Your Way to a Flicker-Free 3D Pie Chart
Anand Jain

Listing 2. Piechart.java.


import java.awt.*;
import java.applet.*;
import java.util.*;


public class Piechart extends Applet {
	

Image buffer; // used for double buffering Graphics gContext; // used for double buffering Label lblChartTitle; String []lblLegend = new String[16]; Slice []slice = new Slice[16]; Color backGroundColor = Color.white; Color []color = { new Color(43,14,114), new Color(238,156,104), Color.blue, new Color(223,23,122), new Color(0,143,224), new Color(242,155,190), new Color(247,198,0), new Color(127,196,36), new Color(253,252,155), new Color(186,112,164), new Color(121,197,161), new Color(143,86,68), new Color(116,195,242), Color.yellow, new Color(0,146,64), new Color(245,198,144) }; // colors int numOfSlices; int biggestNumberIndx=0; public final int MAXSLICES = 16; final int chartBase = 360; final int legendRectStartPosx=5; final int legendRectStartPosy=215; final int legendLblStartPosx=20; final int legendLblStartPosy=225; int [] intPercent = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // for disp. in chart int legendLabelMaxLength = 8; int xLegendDistance = 102; int yLegendDistance = 15; int intTotal = 0; // total number of items recd. int [] intValue = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // values picked up from the html page int [] intDegree = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // converted values static int sliceno=0; public void init() { String chartTitle; chartTitle = getParameter("TITLE"); lblChartTitle = new Label(chartTitle,Label.CENTER); // center align if(lblChartTitle==null) lblChartTitle= new Label(" "); lblChartTitle.setBounds(0,0,25,400); add(lblChartTitle); readParams(); // reads the parameters from html file } // init() // getting the <params> from the applet private void readParams() { String tmpChartValues; for(numOfSlices =0;numOfSlices < MAXSLICES;numOfSlices++) { tmpChartValues = getParameter("SLICE"+(numOfSlices+1)); if(tmpChartValues.trim().equals("")) break; // get out if read all values int indexOf = tmpChartValues.indexOf("-"); String strValue = tmpChartValues.substring(0,indexOf); lblLegend[numOfSlices] = new String( tmpChartValues.substring(indexOf+1,tmpChartValues.length())); float floatValue = (new Float(strValue)).floatValue(); intValue[numOfSlices] = (int)(floatValue*10000); // converting float to int.

// but preserving decimals

// by multiplying by 10000 intTotal = intValue[numOfSlices] + intTotal; } allocSlices(); // find out the share of each slice } private void allocSlices() { // getting the index of the biggest value for(int i =0;i <numOfSlices;i++) { if(intValue[biggestNumberIndx] < intValue[i]) { biggestNumberIndx = i; } } int tmp=0; int tmp1=0; for(int i =0;i <numOfSlices;i++) { // allocating values - % and 360 both if(i != biggestNumberIndx) { // if not biggest then process float ftmp = (float) (chartBase * intValue[i]) / intTotal ; float fpercent = (float) (100 * intValue[i]) / intTotal ; intDegree[i] = Math.round(ftmp); intPercent[i] = Math.round(fpercent); tmp = tmp+intDegree[i]; tmp1=tmp1+intPercent[i]; } else { float ftmp = (float) (chartBase * intValue[i]) / intTotal ; } } intDegree[biggestNumberIndx] = chartBase - tmp; // remaining allocating to the biggest value intPercent[biggestNumberIndx] = 100-tmp1; // remaining % allocating to the biggest value int startAngle=0; for(int i =0;i <numOfSlices;i++) { slice[i] = new Slice(startAngle,intDegree[i],color[i].darker()); startAngle= startAngle+intDegree[i]; } } public void start() { buffer = createImage(400, 300); // used for double buffering gContext = buffer.getGraphics(); // used for double buffering gContext.setColor(backGroundColor); gContext.fillRect(0,0,400,300); gContext.setColor(Color.lightGray); gContext.fillOval(76,110,250,99); // shadow gContext.setColor(Color.black); gContext.fillOval(75,101,250,100); // baseline drawSlices(); // draw slices drawLegends(); // draw legends } // start() private void drawSlices() { for(int j=30; j>0; j-){ Slice.setDimensions(75,70+j,250,100); for(int i =0;i <numOfSlices;i++) { slice[i].draw(gContext); // drawing the 'depth' of each slice } } for(int i =0;i <numOfSlices;i++) { slice[i].draw(gContext,color[i]); // drawing the brighter version on the top } } private void drawLegends() { sliceno=0; String tmpString; final int MAXLEGENDS_HORIZ=4, MAXLEGENDS_VERT=4; for(int i =0; i< MAXLEGENDS_HORIZ;i++) { for(int j =1; j<= MAXLEGENDS_VERT;j++) { //drawing the legends if(sliceno >= numOfSlices) break; slice[sliceno].drawRect(new Rectangle( legendRectStartPosx+(i*xLegendDistance), legendRectStartPosy+( j*yLegendDistance),10,10), gContext); gContext.setColor(Color.black); tmpString = lblLegend[sliceno] ; int lengthofString = tmpString.length(); tmpString = tmpString.substring(0,( lengthofString >legendLabelMaxLength? legendLabelMaxLength : lengthofString)); gContext.drawString(tmpString+" (" + intPercent[sliceno] +"%)", legendLblStartPosx+ (i*xLegendDistance),legendLblStartPosy+(j*yLegendDistance)); sliceno++; } if(sliceno >= numOfSlices) break; } } public void paint(Graphics g) { g.drawImage(buffer,0,0,this); } // paint() public void stop() { } // stop() }

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.