Solved

drawpath refresh with new data from xml

Posted on 2011-09-21
1
263 Views
Last Modified: 2012-05-12
hi, I'm to using the XML data to draw a chart.. the chart has 40 variations and the data is read in from the XML which works okay... the drawpath work okay... however I'm stuck when trying to redraw the path with the next set of data..

the first lot of data come in and draws the chart fine

but when the second lot of data loads im finding it dificult to refresh the chart sprites.... ive added the code below first the xML.. then the AS

again, i'd appreciate any advise and help


 conPlotData.xml
var XMLdataRequest:URLRequest = new URLRequest("conPlotData.xml");
// create a loader
var XMLdataLoaded:URLLoader = new URLLoader();
// new variable that is XML data
var XMLdata:XML;


// define the line style


 
var plot_commands:Vector.<int> = new Vector.<int>(); // establish a new Vector object for the commands parameter
plot_commands.push(1);// 1 moveTo command followed by 3 lineTo commands// use  the Vector array push() method to add moveTo() and lineTo() values

var plot_coord:Vector.<Number> = new Vector.<Number>();// establish a new Vector object for the data parameter
var bondFloor_coord:Vector.<Number> = new Vector.<Number>();// establish a new Vector object 
var parity_coord:Vector.<Number> = new Vector.<Number>();// establish a new Vector object 

var cbSprite:Sprite = new Sprite();
var bfSprite:Sprite = new Sprite();
var paritySprite:Sprite = new Sprite();

var spriteHolder:Sprite = new Sprite();

cbSprite.graphics.lineStyle(6,0xFFFFFF);
bfSprite.graphics.lineStyle(6,0xFFFF00);
paritySprite.graphics.lineStyle(6,0x00FFFF);
 
 
var chart_width:Number = 1800;
var dataPoints:Number = chart_width/12
var dataPoints2:Number = dataPoints;
var dataPointScale:Number = 600;
var dataPointScale2:Number = new Number;


var bPrice:Number = new Number;
var bPriceX:Number = new Number;

var bondFloor:Number = new Number;
var bondFloorX:Number = new Number;

var parity:Number = new Number;
var parityX:Number = new Number;

var scaleYDiv:Number = new Number;//number of divisions on Y scale
var bpTop:Number = new Number;//bond price high (could be other pricing)
var bpLow:Number = new Number;//bond price low (could be other pricing)
var pxTop:Number = new Number;//pixel position top (should be zero)
var pxLow:Number = new Number;//pixel position low (dependent on height of chart)
var pixelX:Number = new Number;//the number required to multiply the bpTop - bPrice
var a1X:Number = new Number;//the number required to multiply the bpTop - bPrice
var a2X:Number = new Number;//the number required to multiply the bpTop - bPrice


scaleYDiv = 10;
bpTop = 140; // later date we need to add this from xml and give it rounding up
bpLow = 70; //later date we need to add this from xml and give it rounding down
pxTop = 600;
pxLow = 0; //chart is 600 heigh but need to add this dynamically
pixelX = (pxTop-pxLow)/(bpTop-bpLow)
trace(pixelX);




//trace(Math.round(dataPoints));
function XMLdataLoadedFunction(event:Event):void
	{
		
		XMLdata = new XML(XMLdataLoaded.data);
		
		for(var i1:Number=0; i1<=11;i1++)
			{
				//use the Vector array push() method to add a set of coordinate pairs

				plot_commands.push(2);
				
			}
			
			
			dataPointScale2 = 0;
			var ii:Number=1;
			
				var CBTimer:Timer = new Timer(100, 40);
				CBTimer.addEventListener(TimerEvent.TIMER, CBtimerListener);
				function CBtimerListener (e:TimerEvent):void
				{
			 
						for each(var a:XML in XMLdata.xyPlot.(@ImpVol==ii))
							
							{
								
								//trace (a.name() + " : " + a.toXMLString());
								plot_coord.push(Math.round(dataPointScale2));
								
								bondFloor_coord.push(Math.round(dataPointScale2));
								
								parity_coord.push(Math.round(dataPointScale2));
								
								
								trace ("result = " + a.@CBPrice + "     ii = " + ii);
								
								bPrice  = (a.@CBPrice);
								bPriceX = pixelX*(bpTop - bPrice)
								plot_coord.push((bPriceX));		
								
								bondFloor  = (a.@BondFloor);
								bondFloorX = pixelX*(bpTop - bondFloor)							
								bondFloor_coord.push((bondFloorX));
								
								parity  = (a.@Parity);
								parityX = pixelX*(bpTop - parity)							
								parity_coord.push((parityX));
								
								
								dataPointScale2 = dataPointScale2 + dataPoints
								
							}
				
				ii = ii + 1	
				trace("Timer is Triggered+++++++++");	
				
				cbSprite.graphics.drawPath(plot_commands, plot_coord);							
				bfSprite.graphics.drawPath(plot_commands, bondFloor_coord);
				paritySprite.graphics.drawPath(plot_commands, parity_coord);
				
				spriteHolder.addChild(cbSprite)				
				spriteHolder.addChild(bfSprite);
				spriteHolder.addChild(paritySprite);
				
				addChild(spriteHolder)
			    
				
				
												
				}
			
				CBTimer.start();
						
	}
trace(bondFloor_coord)

//add event listener then run function
XMLdataLoaded.addEventListener(Event.COMPLETE, XMLdataLoadedFunction);
XMLdataLoaded.load(XMLdataRequest);

Open in new window

var XMLdataRequest:URLRequest = new URLRequest("conPlotData.xml");
// create a loader
var XMLdataLoaded:URLLoader = new URLLoader();
// new variable that is XML data
var XMLdata:XML;


// define the line style


 
var plot_commands:Vector.<int> = new Vector.<int>(); // establish a new Vector object for the commands parameter
plot_commands.push(1);// 1 moveTo command followed by 3 lineTo commands// use  the Vector array push() method to add moveTo() and lineTo() values

var plot_coord:Vector.<Number> = new Vector.<Number>();// establish a new Vector object for the data parameter
var bondFloor_coord:Vector.<Number> = new Vector.<Number>();// establish a new Vector object 
var parity_coord:Vector.<Number> = new Vector.<Number>();// establish a new Vector object 

var cbSprite:Sprite = new Sprite();
var bfSprite:Sprite = new Sprite();
var paritySprite:Sprite = new Sprite();

var spriteHolder:Sprite = new Sprite();

cbSprite.graphics.lineStyle(6,0xFFFFFF);
bfSprite.graphics.lineStyle(6,0xFFFF00);
paritySprite.graphics.lineStyle(6,0x00FFFF);
 
 
var chart_width:Number = 1800;
var dataPoints:Number = chart_width/12
var dataPoints2:Number = dataPoints;
var dataPointScale:Number = 600;
var dataPointScale2:Number = new Number;


var bPrice:Number = new Number;
var bPriceX:Number = new Number;

var bondFloor:Number = new Number;
var bondFloorX:Number = new Number;

var parity:Number = new Number;
var parityX:Number = new Number;

var scaleYDiv:Number = new Number;//number of divisions on Y scale
var bpTop:Number = new Number;//bond price high (could be other pricing)
var bpLow:Number = new Number;//bond price low (could be other pricing)
var pxTop:Number = new Number;//pixel position top (should be zero)
var pxLow:Number = new Number;//pixel position low (dependent on height of chart)
var pixelX:Number = new Number;//the number required to multiply the bpTop - bPrice
var a1X:Number = new Number;//the number required to multiply the bpTop - bPrice
var a2X:Number = new Number;//the number required to multiply the bpTop - bPrice


scaleYDiv = 10;
bpTop = 140; // later date we need to add this from xml and give it rounding up
bpLow = 70; //later date we need to add this from xml and give it rounding down
pxTop = 600;
pxLow = 0; //chart is 600 heigh but need to add this dynamically
pixelX = (pxTop-pxLow)/(bpTop-bpLow)
trace(pixelX);




//trace(Math.round(dataPoints));
function XMLdataLoadedFunction(event:Event):void
	{
		
		XMLdata = new XML(XMLdataLoaded.data);
		
		for(var i1:Number=0; i1<=11;i1++)
			{
				//use the Vector array push() method to add a set of coordinate pairs

				plot_commands.push(2);
				
			}
			
			
			dataPointScale2 = 0;
			var ii:Number=1;//can change this number up to 40 but need to step through 1 to 40
			
				var CBTimer:Timer = new Timer(100, 40);
				CBTimer.addEventListener(TimerEvent.TIMER, CBtimerListener);
				function CBtimerListener (e:TimerEvent):void
				{
			 
						for each(var a:XML in XMLdata.xyPlot.(@ImpVol==ii))
							
							{
								
								//trace (a.name() + " : " + a.toXMLString());
								plot_coord.push(Math.round(dataPointScale2));
								
								bondFloor_coord.push(Math.round(dataPointScale2));
								
								parity_coord.push(Math.round(dataPointScale2));
								
								
								trace ("result = " + a.@CBPrice + "     ii = " + ii);
								
								bPrice  = (a.@CBPrice);
								bPriceX = pixelX*(bpTop - bPrice)
								plot_coord.push((bPriceX));		
								
								bondFloor  = (a.@BondFloor);
								bondFloorX = pixelX*(bpTop - bondFloor)							
								bondFloor_coord.push((bondFloorX));
								
								parity  = (a.@Parity);
								parityX = pixelX*(bpTop - parity)							
								parity_coord.push((parityX));
								
								
								dataPointScale2 = dataPointScale2 + dataPoints
								
							}
				
				ii = ii + 1	
				trace("Timer is Triggered+++++++++");	
				
				cbSprite.graphics.drawPath(plot_commands, plot_coord);							
				bfSprite.graphics.drawPath(plot_commands, bondFloor_coord);
				paritySprite.graphics.drawPath(plot_commands, parity_coord);
				
				spriteHolder.addChild(cbSprite)				
				spriteHolder.addChild(bfSprite);
				spriteHolder.addChild(paritySprite);
				
				addChild(spriteHolder)
			    
				
				
												
				}
			
				CBTimer.start();
						
	}
trace(bondFloor_coord)

//add event listener then run function
XMLdataLoaded.addEventListener(Event.COMPLETE, XMLdataLoadedFunction);
XMLdataLoaded.load(XMLdataRequest);

Open in new window

0
Comment
Question by:nth1971
1 Comment
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 500 total points
ID: 36580318
A couple of thoughts for you to try out:

1. you're re-using the same Sprite to draw your items on, but you're still using addChild() to re-add it to the displayList. You should consider using removeChild() to remove the old version of the Sprite before adding it. You'll get an error if you remove something that isn't there, so you should run a check first. If it hasn't been added, it's parent should be null. So if (spriteHolder.parent) removeChild(spriteHolder);

2. You're using the sprite's graphics property to draw graphics to the Sprite, but you should remember to use the graphics.clear() method to get rid of your old data (assuming you're not just trying to overlay the new data on the old).

3. you're making cbSprite and bfSprite part of the application scope by declaring those variables outside of your load() handler. Consider removing them from the application scope and just scoping them inside the handler. You don't really need to keep any references to them, heck you don't even need a reference to spriteHolder if you want to get right down to it. So up near the top of your load handler, just declare your sprites there: var cbSprite:Sprite = new Sprite() etc...

These are shots in the dark after scanning through your code.Try these out and if nothing pans out, let us know!

T
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now