javascript - 检索数据绑定(bind) d3

标签 javascript plugins d3.js

我正在尝试创建一个 d3 插件来解决这个 stackoverflow 问题:

How to make a d3 plugin?

但在他展示的示例中

(function() {
    d3.selection.prototype.editable = d3.selection.enter.prototype.editable = function() {
        return this.attr('data-editable', true);
      };
})();

我不明白他如何实际检索与选择相关的数据。这是否可以通过 d3.selection 的扩展来检索?我对 d3 源代码进行了一些修改,但发现自己比以前更加困惑。

编写过 d3 扩展/插件的人可以指导我正确的方向吗?

最佳答案

在 javascript 中,this 引用的对象(通常,在上面的示例代码中)由调用 this 出现的函数的对象确定。

因此,行 return this.attr('data-editable', true); 将返回与调用 editable 完全相同的 d3 选择对象。

因此,您将返回正常的旧 d3 选择对象,就像在普通 d3 方法链接模式中一样。一旦你有了这个,获取数据就是 just a matter of looking up the API for the d3 selection object.

如果您对如何取回数据特别感兴趣,请查看 data 方法。从上面的链接中,当不带参数调用该方法时:

If values is not specified, then this method returns the array of data for the first group in the selection. The length of the returned array will match the length of the first group, and the index of each datum in the returned array will match the corresponding index in the selection. If some of the elements in the selection are null, or if they have no associated data, then the corresponding element in the array will be undefined.

关于javascript - 检索数据绑定(bind) d3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14135338/

相关文章:

plugins - Grails Spock 插件的 ClassNotFoundException

javascript - 如何使用 REST API 将 Parse.com 中的日期列设置为 NULL?

javascript - CSS 和 Javascript 优化(速度问题)

javascript - 在 JavaScript 中使用正则表达式删除元字符周围的空格

javascript - 在此 d3.js 日历 View 中包含 json?

svg - 从中间逐渐向外画一条路径

javascript - SVG foreignObject 不在 Safari 中显示

Javascript 如果函数不能与 getElementById 一起正常工作

javascript - 如何使用 jQuery 在 Chrome 扩展中打开的选项卡上运行自定义函数

java - Jenkins中插件的动态更新