从基准访问d3.js元素属性? [英] Accessing d3.js element attributes from the datum?

查看:167
本文介绍了从基准访问d3.js元素属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试访问cx& cy属性的一些特定的svg圈子,我已经绘制到屏幕使用d3.js的.data()函数,任何人都可以帮助?尝试访问它的代码如下。

I'm trying to access the cx & cy attributes of some specific svg circles which i have already drawn to the screen using d3.js's .data() function, can anyone help out? The code that's trying to access it is below.

d3.selectAll(".mynode").each( function(d, i){
  if(d.someId == targetId){
    console.log( d.attr("cx") );    // just trying to demo my point, doesn't work
  }
}



我对d3.js& javascript,所以我不知道如果我接近这个回到前面反正或者我可能错过了一个内置的解决方案?

I'm quite new to d3.js & javascript, so i'm not sure if i'm approaching this back to front anyways or perhaps i may have missed an inbuilt solution?

推荐答案

你的代码试图从一个数据项中获取一个svg属性,当你真正想要的是从svg DOM元素获取该属性,如:

Your code is trying to get an svg attribute from an item of data, when what you really want is to get that attribute from the svg DOM element, as in:

console.log(d3.selectAll(".mynode").attr("cx"));

这只会给你选择的第一个非空元素的属性;你也可以过滤您的选择,以获取您要查找的DOM元素:

This will only give you the attribute for the first non-null element of your selection; You can also filter your selection to get the DOM element you are looking for:

console.log(d3.selectAll(".mynode").filter(_conditions_).attr("cx"));

或者,如果要访问所有选定元素的属性,请使用

Or, if you'd like to access the attributes of all selected elements, use this in your each function:

d3.selectAll(".mynode").each( function(d, i){
  if(d.someId == targetId){
    console.log( d3.select(this).attr("cx") );
  }
}

这篇关于从基准访问d3.js元素属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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