Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pie charts...

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
I have a program that draws a pie chart with 4 slices. It draws the info from the Applet tag of the HTML file. I need it to draw 5 slices. Here is the code...

Code:
Java...

import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;

public class PieChart extends Applet {
   public void paint(Graphics g) {
      Graphics2D pen = (Graphics2D) g;

      final double INSET = 10;
      double diameter = Math.min(getWidth(), getHeight()) - INSET * 2;
      r = diameter / 2;
      centerX = Math.round(INSET + r);
      centerY = Math.round(INSET + r);
      theta = 0;

      pen.draw(new Ellipse2D.Double(INSET, INSET, diameter, diameter));

      int value1 = Integer.parseInt(getParameter("value1").trim()),
         value2 = Integer.parseInt(getParameter("value2").trim()),
         value3 = Integer.parseInt(getParameter("value3").trim()),
         value4 = Integer.parseInt(getParameter("value4").trim());

      String name1 = getParameter("name1").trim(),
            name2 = getParameter("name2").trim(),
            name3 = getParameter("name3").trim(),
            name4 = getParameter("name4").trim();

      double total = value1 + value2 + value3 + value4;

      drawPieSlice(pen, name1, value1 / total);
      drawPieSlice(pen, name2, value2 / total);
      drawPieSlice(pen, name3, value3 / total);
      drawPieSlice(pen, name4, value4 / total);
   }

   public void drawPieSlice(Graphics2D pen, String name, double fraction) {
         double thetaSlice = -fraction * 2 * Math.PI;
         Line2D.Double segment =
            new Line2D.Double(centerX, centerY,
                           centerX + r * Math.cos(theta),
                           centerY + r * Math.sin(theta));
         pen.draw(segment);
         theta = theta + thetaSlice / 2;
         pen.drawString(name,
                     Math.round(centerX + r * Math.cos(theta)),
                     Math.round(centerY + r * Math.sin(theta)));
         theta = theta + thetaSlice / 2;
      }

      private double theta, r;
      private long centerX, centerY;
}

HTML...

<HTML>
<HEAD>
<TITLE>A Java Applet
</TITLE>
</HEAD>
<BODY>
<CENTER>
<APPLET CODE=&quot;PieChart.class&quot; WIDTH=400 HEIGHT=500>
<PARAM NAME=&quot;name1&quot; VALUE=&quot;Golden Gate&quot;>
<PARAM NAME=&quot;value1&quot; VALUE=&quot;4200&quot;>
<PARAM NAME=&quot;name2&quot; VALUE=&quot;Brooklyn&quot;>
<PARAM NAME=&quot;value2&quot; VALUE=&quot;1595&quot;>
<PARAM NAME=&quot;name3&quot; VALUE=&quot;Delaware Memorial&quot;>
<PARAM NAME=&quot;value3&quot; VALUE=&quot;2150&quot;>
<PARAM NAME=&quot;name4&quot; VALUE=&quot;Mackinac&quot;>
<PARAM NAME=&quot;value4&quot; VALUE=&quot;3800&quot;>
</APPLET>
</CENTER>
</BODY>
</HTML>

And if you can help me do that, can you also help me with one that will draw any number of slices that is specified in the HTML file? The one with 5 slices is priority 1, but it would be nice to have one that would do as many slices as I wanted.

Thanks... &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
Hi,

just use for ... loops. Change the paint() method like this:


public void paint(Graphics g) {
Graphics2D pen = (Graphics2D) g;

final double INSET = 10;
double diameter = Math.min(getWidth(), getHeight()) - INSET * 2;
r = diameter / 2;
centerX = Math.round(INSET + r);
centerY = Math.round(INSET + r);
theta = 0;

pen.draw(new Ellipse2D.Double(INSET, INSET, diameter, diameter));


// new
int slices = Integer.parseInt(getParameter(&quot;slices&quot;).trim());
int[] values = new int[slices+1];
String[] names = new String[slices+1];
double total = 0.0;
for (int j = 1; j <= slices; j++) {
values[j] = Integer.parseInt(getParameter(&quot;value&quot;+j).trim());
names[j] = getParameter(&quot;name&quot;+j).trim();
total += values[j];
}
for (int j = 1; j <= slices; i++) {
drawPieSlice(pen, names[j], values[j]/total);
}

} // end paint


In the HTML code you must add the number of slices as a parameter. Something like:


<PARAM NAME=&quot;slices&quot; VALUE = &quot;6&quot;>

<PARAM NAME=&quot;name1&quot; VALUE=&quot;Golden Gate&quot;>
<PARAM NAME=&quot;value1&quot; VALUE=&quot;4200&quot;>
<PARAM NAME=&quot;name2&quot; VALUE=&quot;Brooklyn&quot;>
<PARAM NAME=&quot;value2&quot; VALUE=&quot;1595&quot;>
<PARAM NAME=&quot;name3&quot; VALUE=&quot;Delaware Memorial&quot;>
<PARAM NAME=&quot;value3&quot; VALUE=&quot;2150&quot;>
<PARAM NAME=&quot;name4&quot; VALUE=&quot;Mackinac&quot;>
<PARAM NAME=&quot;value4&quot; VALUE=&quot;3800&quot;>
<PARAM NAME=&quot;name5&quot; VALUE=&quot;Test1&quot;>
<PARAM NAME=&quot;value5&quot; VALUE=&quot;2000&quot;>
<PARAM NAME=&quot;name6&quot; VALUE=&quot;Test2&quot;>
<PARAM NAME=&quot;value6&quot; VALUE=&quot;5000&quot;>
 
Did you try running that? It just maked the pie chart draw lines untill in becomes a filled in, almost 3D circle. &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top