Drawing Graphs in ActionScript

Select the number of men and women to see a pie graph.

Get Adobe Flash player

Experiment: Download the movie, and then change the color. Make a graph of the number of children and adults. Use appropriate names for all of the variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
sldMen.addEventListener(Event.CHANGE, showGraph);
sldWomen.addEventListener(Event.CHANGE, showGraph);
var genderGraph:Sprite = new Sprite();
genderGraph.x=140;
genderGraph.y=230;
addChild(genderGraph);

function showGraph(e:Event):void {
	lblMen.text=""+sldMen.value;
	lblWomen.text=""+sldWomen.value;
	var total:int=sldMen.value+sldWomen.value; //number of people=men+women
	var menPercent:int=sldMen.value/total*100; 
	lblMenPercent.text=""+menPercent+"%";
	var womenPercent:int=sldWomen.value/total*100;
	lblWomenPercent.text=""+womenPercent+"%";
	genderGraph.graphics.clear();
    genderGraph.graphics.lineStyle(1, 0x000000); //black lines between pieces
	var womenDegrees=360*womenPercent/100; //percent of 360 degrees
	piPiece(genderGraph,100,0,womenDegrees, 0xFF0000); //red from 0 to women degrees
	piPiece(genderGraph,100,womenDegrees,360, 0x0000FF); //blue for everything else
} //showGraph

function piPiece(graph,radius,startDeg, endDeg, color):void {
	graph.graphics.beginFill(color);
	graph.graphics.moveTo(0, 0);
	for (var i:int = startDeg; i<=endDeg; i++) {
		graph.graphics.lineTo(radius * Math.cos(i / 180 * Math.PI), radius * Math.sin(i / 180 * Math.PI));
	} //draw line for each degree in piece
	graph.graphics.endFill();
} //piPiece
CODE

NEXT: A line graph of hours worked