我肯定对 d3 越来越熟悉了,开始探索过渡和什么不是,并开始真正了解数据是如何组合在一起的。
我正在研究一个具体的例子,一个气泡图,http ://bl.ocks.org/mbostock/4063269 :
我试图重新利用它来代表人口随时间的增长。这是我的 JSON
{
"name":"Population over Time",
"children":[
{
"name": "1790",
"children": [
{"locID":"1","name": "New York city, NY", "size": 33131},
{"locID":"2","name": "Philadelphia city, PA", "size": 28522},
{"locID":"3","name": "Boston town, MA", "size": 18320},
{"locID":"4","name": "Charleston city, SC", "size": 16359},
{"locID":"5","name": "Baltimore town, MD", "size": 13503},
{"locID":"6","name": "Northern Liberties township, PA", "size": 9913},
{"locID":"7","name": "Salem town, MA", "size": 7921},
{"locID":"8","name": "Newport town, RI", "size": 6716},
{"locID":"9","name": "Providence town, RI", "size": 6380},
{"locID":"10","name": "Marblehead town, MA", "size": 5661},
{"locID":"11","name": "Southwark district, PA", "size": 5661},
{"locID":"12","name": "Gloucester town, MA", "size": 5317},
{"locID":"13","name": "Newburyport town, MA", "size": 4837},
{"locID":"14","name": "Portsmouth town, NH", "size": 4720},
{"locID":"15","name": "Sherburne town (Nantucket), MA", "size": 4620},
{"locID":"16","name": "Middleborough town, MA", "size": 4526},
{"locID":"17","name": "New Haven city, CT", "size": 4487},
{"locID":"18","name": "Richmond city, VA", "size": 3761},
{"locID":"19","name": "Albany city, NY", "size": 3498},
{"locID":"20","name": "Norfolk borough, VA", "size": 2959},
{"locID":"21","name": "Petersburg town, VA", "size": 2828},
{"locID":"22","name": "Alexandria town, VA", "size": 2748},
{"locID":"23","name": "Hartford city, CT", "size": 2683},
{"locID":"24","name": "Hudson city, NY", "size": 2584}
]
},
{
"name": "1800",
"children": [
{"locID":"1","name": "New York city, NY", "size": 60515},
{"locID":"2","name": "Philadelphia city, PA", "size": 41220},
{"locID":"25","name": "Baltimore city, MD", "size": 26514},
{"locID":"3","name": "Boston town, MA", "size": 24937},
{"locID":"4","name": "Charleston city, SC", "size": 18824},
{"locID":"6","name": "Northern Liberties township, PA", "size": 10718},
{"locID":"11","name": "Southwark district, PA", "size": 9621},
{"locID":"7","name": "Salem town, MA", "size": 9457},
{"locID":"9","name": "Providence town, RI", "size": 7614},
{"locID":"20","name": "Norfolk borough, VA", "size": 6926},
{"locID":"8","name": "Newport town, RI", "size": 6739},
{"locID":"13","name": "Newburyport town, MA", "size": 5946},
{"locID":"18","name": "Richmond city, VA", "size": 5737},
{"locID":"26","name": "Nantucket town, MA", "size": 5617},
{"locID":"14","name": "Portsmouth town, NH", "size": 5339},
{"locID":"12","name": "Gloucester town, MA", "size": 5313},
{"locID":"27","name": "Schenectady city, NY", "size": 5289},
{"locID":"19","name": "Albany city, NY", "size": 5289},
{"locID":"10","name": "Marblehead town, MA", "size": 5211},
{"locID":"28","name": "New London city, CT", "size": 5150},
{"locID":"29","name": "Savannah city, GA", "size": 5146},
{"locID":"30","name": "Alexandria town, DC", "size": 4971},
{"locID":"16","name": "Middleborough town, MA", "size": 4458},
{"locID":"31","name": "New Bedford town, MA", "size": 4361},
{"locID":"32","name": "Lancaster borough, PA", "size": 4292},
{"locID":"17","name": "New Haven city, CT", "size": 4049},
{"locID":"33","name": "Portland town, ME", "size": 3704},
{"locID":"24","name": "Hudson city, NY", "size": 3664},
{"locID":"23","name": "Hartford city, CT", "size": 3523},
{"locID":"21","name": "Petersburg town, VA", "size": 3521},
{"locID":"34","name": "Washington city, DC", "size": 3210},
{"locID":"35","name": "Georgetown town, DC", "size": 2993},
{"locID":"36","name": "York borough, PA", "size": 2503}
]
}
]
}
没有做很多很酷的事情,我已经能够从每年显示一组特定的数据开始,调整过滤器让我加载它只显示特定的年份,但看起来这是不正确的方式,因为如果我手动将过滤器更改为下一年,那么它只显示 1800 的气泡,但中心有空白区域,好像它仍在尝试加载 1790 气泡。
var node = svg.selectAll(".node")
.data(bubble.nodes(classes(root))
.filter(function(d) {
if(d.depth == 1 && d.packageName == year){
return d;
}
else{
return null;
}
}))
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
我也很难找到一种方法来使用 JS 命令更改该过滤器,以便人们可以在年份之间切换。我怎样才能更好地理解这一点?