-
If Then Else and XML
I am parsing and XML for flash that dynamically creates a bar chart. The chart plots specific values on a state by state basis triggered by a map of the united states with each state as a button. So if you click on Virginia you will see data from 8 different categories displayed for Virginia. There is also a button on each category you can click to see all 50 states compared in a bar chart format. This is working fine. I need to have the bar for the state clicked to be displayed in a different color from the rest. The bar color is a node in the xml file, see below;
<title name="Alabama">
<textt>AL</textt>
<color>87A25E</color>
<ftot>1931</ftot>
<frate>1118</frate>
<frank>35</frank>
<uelf>9.4</uelf>
<ueya>10.8</ueya>
<ue55>6.4</ue55>
<hpinc>-1.94</hpinc>
<hprank>21</hprank>
</title>
here is the XML load and bar creation;
var barNo:Number = 0;
var barHeight:Number = 0;
var barY:Number = 30;
var valueArr:Array;
var fmt:TextFormat = new TextFormat();
fmt.align = left;
fmt.color = 0x000000;
fmt.font = "Arial";
fmt.size = 10;
fmt.bold = true;
var graphXml:XML = new XML();
graphXml.load(_root.xml_path);
graphXml.ignoreWhite = true;
graphXml.onLoad = function(success) {
if (success) {
var graphArray:Array = this.firstChild.childNodes;
calculate(graphArray);
} else {
trace("No XML Found");
}
};
function calculate(arr:Array):Void {
valueArr = new Array();
for (var i = 0; i<arr.length; i++) {
var obj:Object = new Object();
obj.title = arr[i].childNodes[0].firstChild.nodeValue;
obj.color = arr[i].childNodes[1].firstChild.nodeValue;
obj.percent = arr[i].childNodes[2].firstChild.nodeValue;
valueArr.push(obj);
}
///bar thickness
barHeight = (Stage.height-(1+((arr.length-1)*3)))/arr.length;
createLabels(valueArr);
createGraph(valueArr,barNo);
}
function createLabels(arr:Array):Void {
var LabelHolder:MovieClip = _root.createEmptyMovieClip("LabelHolder", _root.getNextHighestDepth());
for (var i:Number = 0; i<arr.length; i++) {
var mm:MovieClip = LabelHolder.createEmptyMovieClip("mm"+i, LabelHolder.getNextHighestDepth());
mm._y = -45;
var tf:TextField = mm.createTextField("TT", mm.getNextHighestDepth(), 0, 1, 0, 0);
tf.autoSize = true;
tf.selectable = false;
tf.multiline = false;
tf.text = arr[i].title;
tf.setTextFormat(fmt);
}
updateFilter(LabelHolder, 1, 2)
}
function createGraph(arr:Array, no:Number):Void {
var bar:MovieClip = _root.createEmptyMovieClip("bar"+i, _root.getNextHighestDepth());
///Bar width
var barWidth:Number = (Number(arr[no].percent)/52100)*(Stage.width-(100+_root["LabelHolder"]._width));
bar.lineStyle(0,0x87A25E,0);
bar.beginFill(Number("0x"+arr[no].color),100);
bar.moveTo(2,0);
bar.lineTo(barWidth,0);
bar.lineTo(barWidth,barHeight);
bar.lineTo(2,barHeight);
bar.lineTo(2,0);
bar.endFill();
I have tried setting a variable (switch) but am having difficulty in interacting with XML nodes selectively.
Anyone done this before?
-
Code:
createGraph(valueArr,barNo, selectedNo);
Code:
function createGraph(arr:Array, no:Number, selNo:Number):Void {
var bar:MovieClip = _root.createEmptyMovieClip("bar"+no, _root.getNextHighestDepth());
///Bar width
var barWidth:Number = (Number(arr[no].percent)/52100)*(Stage.width-(100+_root["LabelHolder"]._width));
bar.lineStyle(0,0x87A25E,0);
if (selNo != no) {
bar.beginFill(0x000000,100);
} else {
bar.beginFill(Number("0x"+arr[no].color),100);
}
bar.moveTo(2,0);
bar.lineTo(barWidth,0);
bar.lineTo(barWidth,barHeight);
bar.lineTo(2,barHeight);
bar.lineTo(2,0);
bar.endFill();
}
-
Thanks right in line with where I was headed. Thanks for turning on the light for me.
Adam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|