简体   繁体   English

选择D3,Jquery

[英]Selections in D3, Jquery

In the answer Mike posted here , he overviews three different ways to apply a change to a matched element based on a index or custom filter. 在回答麦克张贴在这里 ,他概述三种不同的方式来改变应用到基于一个指标或者自定义过滤匹配的元素。 I'm trying to clarify, hopefully for more people than just myself, the actual selections in these solutions. 我试图澄清,希望更多的人,而不仅仅是我自己,这些解决方案中的实际选择。

So given a document with 6 SVG rects with class .bar , we have these different selections and what they return: 因此,给定一个带有6个SVG rects with class .bar的文档,我们有这些不同的选择以及它们返回的内容:

d3.select(".bar"): d3.select( “巴”):

[Array[1]  
0: rect.[object SVGAnimatedString]  
length: 1  
parentNode: html  
__proto__: Array[0]  

d3.selectAll(".bar"): d3.selectAll( “巴”):

[Array[6]  
0: rect.[object SVGAnimatedString]  
1: rect.[object SVGAnimatedString]  
2: rect.[object SVGAnimatedString]  
3: rect.[object SVGAnimatedString]  
4: rect.[object SVGAnimatedString]  
5: rect.[object SVGAnimatedString]  
length: 6  
parentNode: html  
__proto__: Array[0]  

$(".bar"): $( “巴”):

[
<rect class=​"dataBars" x=​"53.191489361702125" width=​"212.7659574468085" y="4.761904761904762" height=​"11.11111111111111">​</rect>​,  
<rect class=​"dataBars" x=​"74.46808510638297" width=​"372.3404255319149" y=​"20.634920634920636" height=​"11.11111111111111">​</rect>​, 
<rect class=​"dataBars" x=​"127.6595744680851" width=​"212.7659574468085" y=​"36.507936507936506" height=​"11.11111111111111">​</rect>,​  
<rect class=​"dataBars" x=​"31.914893617021274" width=​"212.7659574468085" y=​"52.38095238095238" height=​"11.11111111111111">​</rect>​,  
<rect class=​"dataBars" x=​"159.5744680851064" width=​"265.9574468085106" y=​"68.25396825396825" height=​"11.11111111111111">​</rect>​,  
<rect class=​"dataBars" x=​"234.04255319148936" width=​"138.29787234042553" y=​"84.12698412698413" height=​"11.11111111111111">​</rect>​,
]  

Now here's where it get's more tricky (for me at least), say I want to apply a style to the 3rd rectangle, this rectangle can be found using 现在这里变得更加棘手(至少对我来说),说我想将style应用到第3个矩形,可以使用这个矩形找到

d3.selectAll(".bar")[0][2]  

But if we want to then use the d3.selection.attr() , that returns 但是如果我们想要使用d3.selection.attr() ,则返回

TypeError: Property 'style' of object #<SVGRectElement> is not a function

But we can then wrap this selection 但是我们可以包装这个选择

d3.select(d3.selectAll(".bars rect")[0][2]).style("fill", "red")

and this will work as expected. 这将按预期工作。

Then, if we want to apply a filter, such as 然后,如果我们想要应用过滤器,例如

filter(function (d) { return d === 5 || d === 15;}

the d3.selectAll(".bar") must be used, and d3.select(d3.selectAll(".bar")) will not work correctly. 必须使用d3.selectAll(".bar") ,并且d3.select(d3.selectAll(".bar"))将无法正常工作。

I've read Mike's excellent tutorials and documentation on selections, but just when I think I have it figured out, something pops up and surprises me. 我已经阅读了Mike关于选择的优秀教程和文档,但就在我认为我已经弄明白的时候,有些东西突然出现并让我感到惊讶。 So what is the difference between these selections, and how do I know which one to use when? 那么这些选择之间的区别是什么,以及如何知道何时使用哪一个? Thank you very much, and sorry for the long post! 非常感谢,对不起,很长的帖子!

I've tried to do this in the past and run into similar errors. 我过去曾试图这样做并遇到类似的错误。 Then I realized that I was not really following the intended API. 然后我意识到我并没有真正遵循预期的API。 The second that you start accessing selection elements by index, you're way off the beaten path. 第二个你开始按索引访问选择元素,你离开了人迹罕至的道路。

See nested selections 请参阅嵌套选择

So, if you wanted to style your third bar, you would do 所以,如果你想要设计你的第三个酒吧,你会这样做

d3.selectAll(".bar").style("color", function(d,i) { return i === 2 ? "red" : null; } )

And if your selection is one level more nested than that, make it function(d,i,j) and similarly go from there. 如果您的选择是比这更嵌套的一个级别,请将其设置为function(d,i,j) ,同样从那里开始。 Etc. 等等。

In general, you shouldn't access elements of a returned selection by their indices, but rather filter or use a subselection. 通常,您不应通过索引访问返回选择的元素,而应过滤或使用子选择。 This way, you can apply .attr() and .atyle() without problems. 这样,您可以.atyle()应用.attr().atyle() The reason for this is that D3 doesn't return a "pure" array of DOM elements (like jquery does), but the elements in a wrapper that supports the D3 operations. 原因是D3不返回DOM元素的“纯”数组(如jquery所做),而是返回支持D3操作的包装器中的元素。 It still behaves somewhat like an array though -- if you index into it, you get a "pure" DOM element. 它仍然表现得像一个数组 - 如果你索引它,你会得到一个“纯粹的”DOM元素。 You can also give a DOM element to d3.select() and it will generate the wrapper around this element that enables all the D3 stuff. 您还可以为d3.select()提供一个DOM元素,它将生成围绕此元素的包装器,以启用所有D3内容。

If you have a look at the documentation for filter() , you'll find some examples on how to use it and subselections. 如果你看一下filter()文档 ,你会发现一些关于如何使用它和子选择的例子。 You can use these techniques to get the third element of a selection for example. 例如,您可以使用这些技术来获取选择的第三个元素。 Using the .filter() function only makes sense if you have bound data to the objects you're filtering, otherwise a subselection should do what you want. 使用.filter()函数只有在将数据绑定到要过滤的对象时才有意义,否则子选择应该执行您想要的操作。

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

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