Created
November 4, 2017 18:02
-
-
Save PatriciaW43/fe81b673a90a3cd5f32b7aceca9559c0 to your computer and use it in GitHub Desktop.
Problem with d3.nest rollup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<meta charset="utf-8" /><script src="http://d3js.org/d3.v4.min.js"></script><script src="http://d3js.org/queue.v1.min.js"></script><script src="http://d3js.org/topojson.v1.min.js"></script> | |
<div id="demo"> | |
<div id="data-div"> </div> | |
<script> | |
var nested_data = []; | |
function findTally (nested_data, ward) { | |
for (var index = 0; index < nested_data.length;index++){ | |
var row = nested_data[index]; | |
console.log ("row.key -> " + row.key + " row.value -> " + row.value + " row.values -> " + row.values); | |
var wardin=row.key; | |
// var tallyValue = row.value; | |
if(ward == wardin){ | |
if (row.value === undefined) {return row.values; } else {return row.value;}; | |
};// end if | |
}; // end for | |
return 0; | |
}; // end function | |
d3.csv("http://stopplastics.ca/sites/default/d3_files/ward_petition_counts-1.csv", function(error, tally) { | |
var nested_data = d3.nest() | |
.key(function(d) { return d.ward; }) | |
.rollup(function(v) { return v.length; }) | |
.entries(tally); | |
var tally = findTally (nested_data,30); | |
console.log ("tally -> " + tally); | |
}); // end of csv | |
</script></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rollup sometimes returns key/values instead of key/value (intermittently although always when I was testing it now.) I had to add the check for key.value === undefined in order to get it to work consistently. I am using this script to create a map (in a Drupal website). http://www.stopplastics.ca/toronto-petition-signatures
(p.s. I'm far from a expert in javascript)