简体   繁体   English

d3.js:如何添加数据密钥?

[英]d3.js: How to add a data key?

I'm learning D3.js and trying to get my head around data keys used with streamgraphs. 我正在学习D3.js并试图了解与流程图一起使用的数据键 I would like to adapt the official streamgraph example : 我想改编一下官方的流图示例

在此输入图像描述

...so that each path has an explicit data key, and so that the mouseover logs the data key. ...以便每个路径都有一个显式数据键,以便鼠标悬停记录数据键。

The official example adds paths as follows: 官方示例添加路径如下:

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);

I tried adapting the code as follows, but I'm not sure how to change the structure of data0 (currently an array of arrays) to achieve what I want: 我尝试调整代码如下,但我不知道如何更改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
  });

As it stands, without my having made any changes to the structure of data0 , it unsurprisingly does not work. 就目前而言,如果没有对data0的结构进行任何更改,那么毫无疑问它不起作用。 How can I add a name property to data0 without also messing up the area and .data() functions? 如何在不弄乱area.data()函数的情况下为data0添加name属性?

UPDATE: To be a bit clearer: the D3 docs say that the area function is expecting a two-dimensional array . 更新:更清楚一点:D3文档说区域函数期望二维数组 So if I change data0 from a two-dimensional array, to an array of objects, each with a name key and a data key, how can I also change what I pass to area ? 因此,如果我将data0从二维数组更改为一个对象数组,每个对象都有一个name键和一个data键,我怎样才能更改传递给area

The data in the example doesn't have a "name" property, so you would need to add that to the data to use it. 示例中的数据没有“name”属性,因此您需要将其添加到数据中才能使用它。 The data keys you refer to are used when merging/updating data, ie you have drawn some paths already and then update (some of them). 合并/更新数据时使用您引用的数据键,即您已经绘制了一些路径然后更新(其中一些)。 The .data() function will try to figure out what data is updated and what data is new. .data()函数将尝试确定更新的数据和新数据。 If that doesn't work for you, you can use the data key to help it, ie in your case tell it that things with the same name are the same data. 如果这对您不起作用,您可以使用数据键来帮助它,即在您的情况下告诉它具有相同名称的东西是相同的数据。

If what you mean by data keys are "data legends", then you might want to take a look at the following examples where I've completely separated the placement of magnitudes, legend bullets and legend text in different areas of the charts. 如果数据键的含义是“数据图例”,那么您可能需要查看以下示例,其中我在图表的不同区域中完全分离了幅度,图例项目符号和图例文本的位置。

In each of the examples, you'll clearly see how the data is labeled, structured, passed in, and used. 在每个示例中,您将清楚地看到数据是如何标记,结构化,传入和使用的。

I also tied them together through mouseover and mouseout events so that mousing over or out of any element causes all elements in a set to change color simultaneously. 我还通过mouseover和mouseout事件将它们绑在一起,这样鼠标悬停在任何元素之上或之外会导致集合中的所有元素同时改变颜色。

I hope this helps. 我希望这有帮助。

Frank 坦率

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM