October 6, 2008 | Posted in: ActionScript 3,Code,Flash by Quentin

Recently I was tasked with creating a Flash Chart Application for Pollster.com. One of the challenges in building this app involved allowing the user to turn on and off specific types of poll data, ie. live phone, automated, internet or mail based polls. Sounds simple enough, right? It was. The challenge camewith giving the user the ability to filter out specific pollsters within each category or “mode”. The reason this was a challenge was due to the fact that each category had a pollster associated with multiple data points within the chart, therefore, the list consisted of many repeat listings of the same pollster name.

Being that I already had an array created from the data feed, which was used to place the data points, I figured I could pull my list of pollsters from that array to create my new “pollster” array. Now, there are a ton of ways to accomplish such a task, but I want to focus on one specific method that I used. The every() method of the Array class. I’ve read a lot about people using splice(), slice(), pop(), shift(), etc., but nothing about every(). Nothing at all. Ever . So I figured I’d use it to accomplish the task at hand.

Basically, every() “executes a test function on each item in the array until an item is reached that returns false for the specified function” – Adobe LiveDocs. So let’s get on with some sample code…

Let’s first create an array that will plot our “data points”. In this array we need a series of objects that give us a name, mode, associated pollster,  x and y positions.

// let's create our data point array
var dpoints:Array = new Array();

// loop through our data (use your imagination here)
// and populate our array with objects
for (var i:int = 0; i < infoTotal; i++)
{
	var iname:String 	= info[i].name.toString();
	var imode:String 	= info[i].mode.toString();
	var ipollster:String = info[i].pollster.toString();
	var ixpos:Number 	= info[i].xpos;
	var iypos:Number 	= info[i].ypos;
	var obj:Object 		= {
				name:iname,
				mode:imode,
				pollster:ipollster,
				xpos:ixpos,
				ypos:iypos
				};
	dpoints.push(obj);
}

Let's imagine this data is getting fed to us from an xml document that consists of thousands of results and within those results, we have a total of 10 different pollsters. These 10 pollsters are of course, associated with thousands of data points that will be present on our chart, so if we were to create a list from this array, we would see each of these names multiple times, which is not what we want at all. So how then, do we go about extracting these pollster names to create our list?

Let's assume we only want to create a list of pollsters in the "live phone" category and add a few lines to our code:

// let's create our data point array
var dpoints:Array = new Array();

// create our pollster array
var pollsters:Array = new Array();

// loop through our data (use your imagination here)
// and populate our array with objects
for (var i:int = 0; i < infoTotal; i++)
{
	var iname:String 	= info[i].name.toString();
	var imode:String 	= info[i].mode.toString();
	var ipollster:String = info[i].pollster.toString();
	var ixpos:Number 	= info[i].xpos;
	var iypos:Number 	= info[i].ypos;
	var obj:Object 		= {
				name:iname,
				mode:imode,
				pollster:ipollster,
				xpos:ixpos,
				ypos:iypos
				};
	dpoints.push(obj);

	// run our every() function call
	var listed:Boolean = pollsters.every(checkArr);
	if (!listed)
	{
		pollsters.push(obj);
	}

	// the function - checks whether the pollster exists in our list
	// and returns an answer (true/false)
	function checkArr(element:*)
	{
		return (element.pollster == obj.pollster
			&& element.mode == "live");
	}
}

That's pretty much it. Mission Accomplished. You should now have two arrays, one for your data points and another to serve as your list of pollsters for the "live phone" data category. Although, this is an extremely simplified version of the code I actually created for the pollster.com chart app, it should give you a general idea of how it works and inspire further experimentation with the every() class.

If you have your own uses for this class that you'd like to share or see where my example can be improved, please feel free to share in the comments.

Comments
  1. whu?
    October 8, 2008 | 5:50 am

    i just actually had about a half hour to read through properly and take in a bit of Ur strategy. .good isht Q, i can see Ur vision.

Leave a comment

You must be logged in to post a comment.