Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

PieChart.as

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function PieChart(x,y,radius,timeline,depth, /*optional:*/ startAngle){
	PieChart.instances++;
	this._mc = timeline.createEmptyMovieClip("PieChartInstance"+PieChart.instances, depth);
	this._mc._x = x; this._mc._y = y;
	this.radius = radius;
	this.divisions = new Array();
	this.startAngle = (startAngle == undefined) ? 0 : startAngle;
}
PieChart.instances = 0;
PieChart.prototype.setStartAngle = function(angleR){
	this.startAngle = angleR;
}
PieChart.prototype.setRadius = function(radius){
	this.radius = radius;
}
PieChart.prototype.setPosition = function(x,y){
	this._mc._x = x; this._mc._y = y;
}
PieChart.prototype.insertSlice = function(position, percent, fillColor){
	this.divisions.splice(position, 0, {p:percent, c:fillColor});
	return position;
}
PieChart.prototype.appendSlice = function(percent, fillColor){
	return this.insertSlice(this.divisions.length, percent, fillColor);
}
PieChart.prototype.removeSlice = function(position){
	this.divisions.splice(position, 1);
}
PieChart.prototype.clearSlices = function(){
	this.divisions = new Array();
}
PieChart.prototype.setSlicePercent = function(position, percent){
	this.divisions[position].p = percent;
}
PieChart.prototype.getSlicePercent = function(position){
	return this.divisions[position].p;
}
PieChart.prototype.getTotalPercent = function(){
	var tot = 0;
	for (var i=0; i<this.divisions.length; i++) tot += this.divisions[i].p;
	return tot;
}
PieChart.prototype.render = function(){
	this._mc.clear();
	this._mc.lineStyle(0,0,100);
	var a1 = this.startAngle, a2;
	for (var i=0; i<this.divisions.length; i++){
		a2 = a1 + this.divisions[i].p*Math.PI*2;
		if (this.divisions[i].p){
			this._mc.beginFill(this.divisions[i].c, 100);
			this._mc.CircleSegmentTo(0, 0, a1, a2, this.radius, "CW");
			this._mc.lineTo(0,0);
			this._mc.endFill();
		}
		a1 = a2;
	}
}