D3: Difference between revisions
From charlesreid1
| Line 35: | Line 35: | ||
* Accessing arrays using notation <nowiki>data[0]</nowiki> versus <nowiki>data['x1']</nowiki> versus <nowiki>data.x1</nowiki> | * Accessing arrays using notation <nowiki>data[0]</nowiki> versus <nowiki>data['x1']</nowiki> versus <nowiki>data.x1</nowiki> | ||
* Notation <nowiki>+p[d]</nowiki> | * Notation <nowiki>+p[d]</nowiki> | ||
==Role of Maps== | |||
Array maps are really handy, because they create a mapping of an existing array to a new one. This can be used to transform an array (for example, you could take an array of numbers and transform it into an array of squares of those numbers) or to expand/reduce an array (for example, you could take a two-dimensional array and find the sum of each element over a particular dimension). | |||
Revision as of 07:01, 7 August 2012
Loading Data
To load multiple (arbitrary number) CSV files:
var filesArray = ["myrandedata.csv","myrandndata.csv","myrandudata.csv"];
var remaining = filesArray.length;
// from https://groups.google.com/forum/#!msg/d3-js/3Y9VHkOOdCM/YnmOPopWUxQJ
filesArray.forEach( function(f) {
d3.csv(f, function(data) {
mydata[f] = data;
if (!--remaining) doSomething();
});
});
function doSomething() {
filesArray.forEach( function(f) {
console.log( mydata[f] );
});
}
Things I learned about D3 while modifying Parallel example
- Role of maps
- Nesting functions
- Scope of Javascript
- How to use for loops instead of functions to avoid scope issues
- How to load multiple files using a counter to avoid scope/asynchronous issues
- The whole function notation
- Loading data as CSV (associated array) or as text (plain multidimensional array)
- Console
- Accessing arrays using notation data[0] versus data['x1'] versus data.x1
- Notation +p[d]
Role of Maps
Array maps are really handy, because they create a mapping of an existing array to a new one. This can be used to transform an array (for example, you could take an array of numbers and transform it into an array of squares of those numbers) or to expand/reduce an array (for example, you could take a two-dimensional array and find the sum of each element over a particular dimension).