ArrayCollection column counts

As part of an example of trying to read data from SharePoint and display it in a Flex/Flash application, I needed to see if I could create pie charts from the data.  After figuring out how to get the list data as an xml output, then cleaning up the data to insert it into a ArrayCollection, the next step was to count the number of times a particular value was used in a particular column.  If I was using SQL, it would be  a straight forward grouping and count.  The work around was to filter out the unique values and then filter the ArrayCollection on that value getting the count.

Finding the unique values was simple, because I knew someone had already created the query example for doing so.  It was just a matter of searching the web and finding the code.


//takes an AC and the filters out all duplicate entries
public static function getUniqueValues (collection:ArrayCollection, propertyName:String):ArrayCollection {
var length:Number = collection.length;
var dict:Dictionary = new Dictionary();

//this should be whatever type of object you have inside your AC
var obj:Object;
for(var i:int = 0; i < length; i++){
obj = collection.getItemAt(i);

dict[obj[propertyName]] = obj[propertyName];
}

//this bit goes through the dictionary and puts data into a new AC
var unique:ArrayCollection = new ArrayCollection();
for(var propertyString:String in dict){
unique.addItem(dict[propertyString]);
}
return unique;
}

With the unique values being returned, then I just needed to loop through each value to do a filter on the original ArrayCollection to get the count. There were several examples that I found, but they didn’t work, because they didn’t completely do what I needed.  Part of the script had to include the filter function within the overall function.  By creating the filter function as a variable, the loop call could use the filter for each filter by value.  Since I planned adding an object to the final array of name and value, I used the name part of the array to hold the filter value.  Then when the count was returned, but putting the array in to a temporary array and getting the length, I added the count to the object.  The final code looks extremely simple.


//Takes an AC of values and finds the number of matching values in another AC to return a count
public static function getValueCounts(sourceCollection:ArrayCollection, valueCollection:ArrayCollection, propertyName:String):ArrayCollection {
var length:Number = valueCollection.length;
var countArray:ArrayCollection =  new ArrayCollection();

var filterFunction:Function = function(element:*, index:int, arr:Array):Boolean {
return element[propertyName] == this.name;
}

var thisValue:String;
var countObject:Object;

for(var i:int = 0; i < length; i++){
countObject = new Object();
countObject.name = valueCollection.getItemAt(i);
var tmpCollection:Array = sourceCollection.source.filter(filterFunction, countObject);
sourceCollection.refresh();

countObject.value = tmpCollection.length;
countArray.addItem(countObject);
}

return countArray;
}

To use this, I simply create a FilterBy array from the original using the column I want to find unique values in, then pass the original, the unique values and the column to the function to get the name/value count array.


FilterValuesArray = getUniqueValues(SourceArrayCollection, "ColumnToFilter");
ValuesCountArray = getValueCounts(SourceArrayCollection, FilterValuesArray , "ColumnToFilter");

I used the final count array to display a pie chart.

Filming Heist H2O

The weekend of June 24 I got together with a The RTP “Let’s Make a Movie” Meetup group of people and we filmed a short movie in 48 hours.  We were part of the 48 Hour Film Project, a filming competition that requires teams to write, film, edit and submit a 7 minute film within a 48 hour time period.  This is the second year that I have competed in the contest and this year’s effort was much better than last year’s.

The contest starts with team leaders drawing film genres randomly.  If the group doesn’t like the genre, they can take their chance at drawing from 5 wild card genres.  This is what I did, since I thought ‘Romance‘ might be a little hard to do.  The wild card that I drew was ‘Heist’, which I was very excited about getting.  This year a team resource had a full sized RV to use for filming and editing.  Doing a heist in a RV seems like a funny sort of thing to do.

On Friday we all gathered at a house in Raleigh and discussed options for the movie.  We had 3 main characters and a couple of other people who wanted to do some acting.  We had a secondary location of my brother’s house, which had a small pool  A silly thought of steeling the pool came to mind and we started working on the Heist.  I decided to make our two relatively recent to America actors into sort of stereo typical roles.  Looks and accents really helped with them being “Ninja Girl” and “Voodoo Priestess”.   To throw a twist on the main character, I set the adult female actor to be the plumber.  My returning child actress was aged up 6 years to be a disgruntled teenager.   With help from a couple other group members, writing was done by midnight.

Saturday morning started at 6 am for me.  I opened up the script and did some minor edits on the lines and made sure that the script was 7 minutes long.  The minimum length allowed is 4 minutes, but based on last year’s competition, the closer to the max of 7 minutes, the better.  I headed over to the house in Raleigh were we started figuring out the opening shot as the actors arrived.  By 10 am, we were shooting film and getting the actors ready for their first scenes.  I had initially hoped to have multiple cameras going at the same time so that I could use Adobe Premiere’s multiple camera tool to quickly put together different camera angles in the same shot.  Unfortunately we ended up with a prosumer camera and my BlackBerry PlayBook as cameras.  Later we would determine that the camera was set to SD instead of HD and the heavy duty protective cover on the PlayBook caused film quality issues.

By 2 pm we had the inside RV scenes done and we go ready to hit the road to head towards my brother’s place about an hour away.  After a little detour and delay, we made it there by 4 pm and started shooting.  We quickly figured out some shots to take of the RV rolling up to the house and got those done.  Then we hurried through multiple takes to get the rest of the scene done, plus a flash back scene of me playing “The Scot”.  We finished filming by 7 pm, which was better than last year.  The actors from the other side of the state headed home and the rest of us piled into the RV and headed back to Raleigh.

With only the editors left, we tried downloading the film to start picking shots and unfortunately it didn’t go well.  The electricity in the RV was having glitches and causing my editing software to crash.  After a few hours, we gave up and I went to sleep in the RV.  I woke up at 6 am and started trying the film again, but noticed we had other issues, the video sound was out of sync.  Mel had to re-download the video from the camera, which took another 2 hours.

As time ticked away, we got the shots we wanted and Mel started working on the special effects, only to be slowed up by compiling.  We were past the stop time of 5 pm to get to Greensboro on time and I was getting nervous.  Other issues included my Jeep’s transmission acting up and having to ask my wife to drive me to the drop -off location after she just drove back from the beach.  In what seemed like forever, the compiling was done, put on a flash drive and a dvd, checked, tagged and ready to go.  My wife got me to the drop-off with 3 minutes to spare.  Whew!!

Since I had a few other opportunities to practice directing and making a movie, this year’s attempt was much easier.  Also, the overall product was put together much much better.  Mostly due to shooting the entire scene, unlike I did last year.  The movie was well received at the premiere night, which made me very happy.  Hopefully you will enjoy it as well.