Ok so now that I am on the web, I have to get data organised (again). Here are the results of some of my playing.
This first sketch here I have ran through a for loop of the collection to build an associative array where the key is the object type and the value is a count of how many items there are of that type. I don't think I fully understand JavaScript associative arrays yet - I have been thinking of them like hash maps in Processing, but I think that really they are just a
normal object (and that even a singular object is the first item in an array), and that keys are not keys but actually object properties. When testing if my associative array already contains a particular object type I use
objectTypeList.hasOwnProperty(object_type). When getting items I can call them either with
objectTypeList[object_type] or
objectTypeList.object_type, but they dont have an index and it appears I cant get their length - to run through a for loop I can use
for(key in objectTypeList).
To display, I am adding each item to an unordered html list with
$.append, which I am also using to format the object type as bold. It would be better to put the object type and the count in different html tags with unique classes so that the formatting can be done separately with CSS. The yellow background is however thanks to CSS.
|
A list of object types and count of items of that type |
If I wanted to get all the keys out as an array I could do so with
.keys however this appears to only have support in the very
newest browsers. For now I will run through a for loop and get each individually. This leaves me wondering if I am missing a better approach to organising my data?
|
A list of object types and titles of each item of that type |
Breakthrough! Yay! This second sketch here is organised. I have built lists of each object type, adding the items of that type - no need for custom classes, the items appear good to go straight from the JSON as JavaScript objects. Displaying all the titles is proof that I can access the individual items. I am in control! Now the count is the length of the lists.
Problem - handling undefined keys. Don't know how to skip. It appears all items have a title and object type recorded, but there is variable use of most other parameters. Something to come back to..
Next step in getting organised. Sorting. JavaScript .
sort() worked nicely on this list of keys, which I extracted as described above. The sort function defaults to sorting alphabetically for a list of strings, to sort numerically I had to write a simple comparator function that compared the item list lengths. Once I have a sorted list of keys I can loop through it and using the key still access the individual items.
|
A list of object types sorted alphabetically |
|
A list of object types sorted by count descending of items of that type |
And finally I was able to load images in, simply using the
<img> html tag and the
$.append. What a relief! Now I have all the basic ingredients to make some of browser for the NMA collection.
|
A list of object types and images of each item of that type if available |
Actually it turns out the problem above about identifying undefined properties/keys was very simple to resolve with an
if(key == undefined).
|
A list of object types and images of each item of that type if available with a count of available images |
And just to tidy up my weekend of getting data organised, I was able to make each image a link to the corresponding item record in the NMA online catalogue. Quite satisfying!
|
Item record in the NMA online catalogue linked from my list of object types |
A big thanks to
Mitchell for getting me started with some of his sketches.
My next steps will be to try working with the full dataset (I have only been using the first 800 item here), and to draw only what is on screen so there are not huge numbers of images flying around (see if I can get ajax to work now?). Then I will need to learn to draw so that I can get more sophisticated formatting and so that I can design some analytical visualisations (charts, graphs etc). For these I could try
Processing.js or
Raphael or
D3.js which supercedes
Protovis.
No comments:
Post a Comment