【发布时间】:2012-03-29 02:30:06
【问题描述】:
我正在学习 D3.js 并试图了解 data keys 与流图一起使用。我想改编official streamgraph example:
...这样每个路径都有一个明确的数据键,并且鼠标悬停记录数据键。
官方示例添加路径如下:
var area = d3.svg.area()
.x(function(d) { console.log('x', d.data); return d.x * w / mx; })
.y0(function(d) { return h - d.y0 * h / my; })
.y1(function(d) { return h - (d.y + d.y0) * h / my; });
vis.selectAll("path")
.data(data0)
.enter().append("path")
.style("fill", function() { return color(Math.random()); })
.attr("d", area);
我尝试将代码修改如下,但我不确定如何更改data0(当前为数组的数组)的结构以实现我想要的:
vis.selectAll("path")
.data(data0, function(d) { return d.name }) // Add key function
.enter().append("path")
.style("fill", function() { return color(Math.random()); })
.attr("d", area)
.on("mouseover", function (d,i) {
console.log("mouseover", d.name); // Log name property on mouseover
});
就目前而言,如果我没有对data0 的结构进行任何更改,毫无疑问它不起作用。我怎样才能将name 属性添加到data0 而不会弄乱area 和.data() 函数?
更新:更清楚一点:D3 文档说area function is expecting a two-dimensional array。因此,如果我将data0 从一个二维数组更改为一个对象数组,每个对象都有一个name 键和一个data 键,我怎样才能更改传递给area 的内容?
【问题讨论】:
标签: javascript d3.js stream-graph