pinalbhuta Calculating status...
Hi,
I have a class file of LoadXML its not working can anyone help me in that.
ActionScript Code:
package
{
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.filters.DropShadowFilter;
public class PieChart extends MovieClip
{
private var piechart:MovieClip = new MovieClip();
private var titles:Array = new Array();
private var values:Array = new Array();
private var xmlFile:XML;
private var pie:Number;
private var radiansSoFar:Number = 0;
private var urlLoader:URLLoader = new URLLoader();
private var tween:Tween;
private var format:TextFormat = new TextFormat();
public function PieChart():void
{
/*Text Format*/
format.color = 132744;
format.align = "left";
format.font = "Century Gothic";
format.size = 11;
LoadXML();
buildPieChart(xmlData);
}
private function LoadXML(file:String = "pie.xml"):void
{
urlLoader.load(new URLRequest(file));
urlLoader.addEventListener(Event.COMPLETE, parseXML);
}
private function parseXML(e:Event):void
{
xmlFile = new XML(e.target.data);
//trace(xmlFile);
pie = xmlFile.children().length;
//trace(pie);
buildPieChart(xmlData);
}
private function buildPieChart(xmlData:XML):void
{
var titleList:XMLList = xmlFile.item.title;
var valueList:XMLList = xmlFile.item.value;
for each (var titleElement:XML in titleList)
{
titles.push(titleElement);
}
for each (var valueElement:XML in valueList)
{
values.push(valueElement);
}
var colors:Array = new Array();
colors.push(0x990000);
colors.push(0x000066);
colors.push(0x990099);
colors.push(0xFFFF99);
colors.push(0x336699);
colors.push(0x330000);
colors.push(0x99CC00);
colors.push(0x000033);
colors.push(0x663366);
colors.push(0x333300);
colors.push(0x330033);
var radians:Array = new Array();
var totalValue:Number = 0;
for(var i:int = 0; i < values.length; i++)
{
totalValue = totalValue + Number(values[i]);
}
for(i =0; i < values.length; i++)
{
radians.push(Number(values[i])/totalValue*2);
}
drawlines(250, 250, 200, radians);
function drawlines(centerx, centery, radius, radians)
{
var colorkey:Number = 0;
var coloralpha:Number = 1;
var shadow:DropShadowFilter = new DropShadowFilter();
shadow.distance = 2;
shadow.angle = 45;
shadow.color =0x000000;
for(var i:int = 0; i < radians.length; i++)
{
piechart.graphics.beginFill(colors[colorkey], coloralpha);
piechart.graphics.moveTo(0,0);
piechart.graphics.lineTo(Math.sin(radiansSoFar*Math.PI)*radius, Math.cos(radiansSoFar*Math.PI)*radius);
piechart.graphics.lineTo(0,0);
piechart.graphics.endFill();
piechart.filters = [shadow];
piechart.x = stage.stageWidth/2;
piechart.y = stage.stageHeight/2;
for(var n:Number = 0; n <= radians[i]; n += .0001)
{
piechart.graphics.lineTo(Math.sin((radiansSoFar+n)*Math.PI)*radius, Math.cos((radiansSoFar+n)*Math.PI)*radius);
}
radiansSoFar += radians[i];
stage.addChild(piechart);
addLabel(radians[i], titles[i], radiansSoFar, radius, colors[colorkey], coloralpha);
if(colorkey == colors.length-1)
{
colorkey = 0;
coloralpha -= .25;
}
else
{
colorkey += 1;
}
}
}
function addLabel(radians, itemtitle, radiansSoFar:Number, radius:Number, color, coloralpha)
{
var format:TextFormat = new TextFormat();
format.align = "left";
format.font = "Century Gothic";
format.size = 11;
//format.bold = true;
var label:TextField = new TextField();
label.width = 1;
label.height = 1;
label.autoSize = "left";
label.antiAliasType = "advanced";
label.text = itemtitle + "( "+ Math.round((radians/2*100)).toString() + "%)";
label.border = false;
label.setTextFormat(format);
var textRadians:Number = radiansSoFar-(radians/2);
label.x = (stage.stageWidth/2)+Math.sin(textRadians*Math.PI)*radius;
label.y = (stage.stageHeight/2)+Math.cos(textRadians*Math.PI)*radius;
if(textRadians > 0 && textRadians < .5)
{
label.y -= label.height/2;
label.y += 10;
label.x += 10;
}
if(textRadians > .5 && textRadians < 1)
{
label.y -= label.height/2;
label.x += 10;
label.y -= 10;
}
if(textRadians > 1 && textRadians < 1.5)
{
label.y -= label.height/2;
label.x -= label.width;
label.x -= 10;
label.y -= 10;
}
if(textRadians > 1.5 && textRadians <= 2)
{
label.y -= label.height/2;
label.x -= label.width;
label.x -= 10;
label.y += 10;
}
if(textRadians == 0 || textRadians == 2)
{
label.y += 10+label.height/2;
}
if(textRadians == .5)
{
label.x += 10+label.width/2;
}
if(textRadians == 1)
{
label.y -= 10+label.height/2;
}
if(textRadians == 1.5)
{
label.x -= 10+label.width/2;
}
stage.addChild(label);
}
}
}
}
here is the XML file
Quote:
<?xml version=”1.0? encoding=”utf-8??>
<items>
<item>
<title>a</title>
<value>26</value>
</item>
<item>
<title>b</title>
<value>14</value>
</item>
<item>
<title>c</title>
<value>9</value>
</item>
<item>
<title>d</title>
<value>6</value>
</item>
<item>
<title>e</title>
<value>3</value>
</item>
<item>
<title>f</title>
<value>3</value>
</item>
<item>
<title>g</title>
<value>2</value>
</item>
<item>
<title>h</title>
<value>1</value>
</item>
<item>
<title>TV</title>
<value>36</value>
</item>
</items>
779 Views
15 Replies
Latest reply:
Andrei1, Aug 26, 2010 4:53 PM



