D3js:何时使用.datum()和.data()? [英] D3js: When to use .datum() and .data()?

查看:257
本文介绍了D3js:何时使用.datum()和.data()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用面积图时,我经常看到 .datum 。例如:

I often see .datum when an area chart is used. For example:

svg = d3.select("#viz").append("svg").datum(data)

有关 .datum

Are there any rules of thumb for when .datum is needed?

var area = d3.svg.area()  
    .x(function(d) { return x(d.x); })  
    .y0(height)  
    .y1(function(d) { return y(d.y); });  

var svg = d3.select("body").append("svg")  
    .attr("width", width)  
    .attr("height", height);  

svg.append("path") 
    .datum(data)
    .attr("d", area); 


推荐答案

我认为文档给出了一个很好的答案: https://github.com/mbostock/d3/wiki/Selections#wiki-datum

I think the documentation gives a good answer to this question: https://github.com/mbostock/d3/wiki/Selections#wiki-datum.

基本上,关键是在某些情况下,当你做选择时,你对进入/退出集不感兴趣。

Basically, the point is that in some cases you are not interested in the enter/exit sets when you do a selection. If this is the case, which often seems to be the case for the full chart, you use datum.

更新:这取决于:什么时候你期望没有动态更新,这似乎是在你给的例子的情况下,基准是好的。为什么?因为还没有路径svg元素,只有一个路径元素,一旦添加就不会改变。

Update: It depends: when you expect no dynamic updates, which seems to be the case in your given example, datum is okay. Why? Because there is no path svg element yet, there is only one path element and once it is added it will not change.

如果你有多个路径元素和动态更改(例如,每秒钟后,最旧的数据元素被删除,并且新的数据元素被添加),您将需要需要数据。这将给你三组:没有数据存在的图形元素的集合,数据被更新的元素的集合和之前没有数据项存在的元素的集合(分别地,输入,更新和退出集)。一旦你需要这个,我建议你阅读d3文档。

If you where to have multiple path elements and dynamic changes (e.g. after each second the oldest data element gets removed and a new one gets added), than you will need data. This will give you three sets: the sets of graphic elements for which no data exists any longer, the set of elements for which the data is updated and the set of elements for which no data item existed before (respectively, the enter, update and exit sets). Once you need this I suggest you read up on the d3 documentation.

显然,计算这三组是不是没有成本。实际上,当你使用大(我认为d3可以扩展到成千上万的项目)数据集时,这只会成为一个问题。

Obviously, calculating these three sets is not without a cost. In practice, this should only become a problem when you're working with "large" (I think d3 scales up to 10s of thousands of items) data sets.

这篇关于D3js:何时使用.datum()和.data()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆