From charlesreid1

Line 45: Line 45:
<source lang="javascript">
<source lang="javascript">
var abc=[1,2,3,4,5];
var abc=[1,2,3,4,5];
def = abc.map( function(d) { return [ +d, +d + 4 ] } );  
def = abc.map( function(d) { return [ +d, +d + 4 ] } );  
def.forEach( function(p) {
def.forEach( function(p) {
     console.log(p);
     console.log(p);
Line 67: Line 65:
<source lang="javascript">
<source lang="javascript">
var abc=[[1,1],[2,4],[3,9],[4,16],[5,25],[6,36]];                                         
var abc=[[1,1],[2,4],[3,9],[4,16],[5,25],[6,36]];                                         
def = abc.map( function(d) { return d[1] } );                                             
def = abc.map( function(d) { return d[1] } );                                             
console.log(def);
console.log(def);
</source>
</source>
Line 77: Line 73:
<pre>
<pre>
[1, 4, 9, 16, 25, 36]  
[1, 4, 9, 16, 25, 36]  
</pre>
===Transformation Example: Values to Squared Values===
<source lang="javascript">
var abc=[1,2,3,4,5];
def = abc.map( function(d) { return Math.pow(+d,2) } );
console.log(def);
</source>
results in:
<pre>
[1, 4, 9, 16, 25]
</pre>
</pre>

Revision as of 07:12, 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).

Expansion Example: Single Dimension to Multiple Dimension Array

var abc=[1,2,3,4,5];
def = abc.map( function(d) { return [ +d, +d + 4 ] } ); 
def.forEach( function(p) {
    console.log(p);
}

Results in:

[1, 5]
[2, 6]
[3, 7]
[4, 8]
[5, 9]

Contraction Example: Multiple Dimension to Single Dimension Array

var abc=[[1,1],[2,4],[3,9],[4,16],[5,25],[6,36]];                                        
def = abc.map( function(d) { return d[1] } );                                            
console.log(def);

Results in:

[1, 4, 9, 16, 25, 36] 

Transformation Example: Values to Squared Values

var abc=[1,2,3,4,5];
def = abc.map( function(d) { return Math.pow(+d,2) } );
console.log(def);

results in:

[1, 4, 9, 16, 25]