JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
CSS
.node {
stroke-width: 1.5px;
}
text {
font: 10px sans-serif;
}
JavaScript
var width = 700,
height = 700;
var fill = d3.scale.category20c();
//baby names
var AsianNameList=[];
var BlackNameList=[];
var HispanicNameList=[];
var WhiteNameList=[];
//number of babies with name
var AsianNameCount=[];
var BlackNameCount=[];
var HispanicNameCount=[];
var WhiteNameCount=[];
//ethnicity of mother
var AsianEthnicity=[];
var BlackEthnicity=[];
var HispanicEthnicity=[];
var WhiteEthnicity=[];
var data=[]; //3D array of nameList, nameCount, ethnicity
var dobj=[]; //array formated specifically for hierarchical processing
d3.csv("https://data.cityofnewyork.us/resource/25th-nujf.csv?$limit=10000", function (error, root) {
if (error) throw error;
root.forEach(function (d) {
if (+d["RNK"] <= 10) {
//only add name if it's not uniquely in the array
if ((d["ETHCTY"]==="ASIAN AND PACIFIC ISLANDER") && (AsianNameList.indexOf(d["NM"]) ===-1)) {
AsianNameList.push(d["NM"]);
AsianNameCount.push(+d["CNT"]); //force the string into an integer
AsianEthnicity.push(d["ETHCTY"]);
}
else if ((d["ETHCTY"]==="BLACK NON HISPANIC") && (BlackNameList.indexOf(d["NM"]) ===-1)) {
BlackNameList.push(d["NM"]);
BlackNameCount.push(+d["CNT"]); //force the string into an integer
BlackEthnicity.push(d["ETHCTY"]);
}
else if ((d["ETHCTY"]==="HISPANIC") && (HispanicNameList.indexOf(d["NM"]) ===-1)) {
HispanicNameList.push(d["NM"]);
HispanicNameCount.push(+d["CNT"]); //force the string into an integer
HispanicEthnicity.push(d["ETHCTY"]);
}
else if ((d["ETHCTY"]==="WHITE NON HISPANIC") && (WhiteNameList.indexOf(d["NM"]) ===-1)) {
WhiteNameList.push(d["NM"]);
WhiteNameCount.push(+d["CNT"]); //force the string into an integer
WhiteEthnicity.push(d["ETHCTY"]);
}
}
});
data=[
//test data is commented out:
//["Tea","Coffee","Soda","Chips","Milk","Chocolate","Beer","Wine"],
//[130,30,200,40,230,150,80,65]
AsianNameList.concat(BlackNameList,WhiteNameList,HispanicNameList),...