在d3.js v4中重新绑定导出 [英] Rebinding exports in d3.js v4

查看:804
本文介绍了在d3.js v4中重新绑定导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用模块系统创建地图。我或多或少习惯了D3.js v3,但我仍然习惯v4。

I'm creating a map using the modules system. I'm more or less used to D3.js v3 but I am still getting used to v4.

我试图添加一个调度,但我不知道如何重新绑定V4中的导出,因为此功能现在不可用。

I am trying to add a dispatch but I don't know how to rebind the exports in V4, as this function is not available now.

所以对于我的dispatch(_dis)和我的特定事件(changetype d3 v3将在返回导出之前正确,例如:

So for my dispatch (_dis) and my particular event ("changetype"), the rebind in d3 v3 would be right before returning the exports, for example:

d3.mapDots = function (districts){

   var _dis = d3.dispatch('changetype');

   (...)

   exports.color = function(_c){
       if(!arguments.length) return color;
       color = _c;
       return this;
   };    

   d3.rebind(exports,_dis,"on");
   return exports
   };

有没有人知道如何在v4中做这个?我一直在尝试dispatch.apply,但它不工作。

Does anyone know how to do this in v4? I've been trying dispatch.apply but it doesn't work.

谢谢!

推荐答案

好问题。看来,调度对象在某种程度上更改,并且 d3.rebind 不再存在。因为后者已经消失了,似乎没有办法复制(通过 d3.rebind .on()方法。相反,你必须实现自己的。 请参阅这里如何实施bostock d3-brush

Good question. Looks like the dispatch object has somewhat changed, and that d3.rebind no longer exists. Because the latter is gone, it appears that there's no way to "copy" (via d3.rebind) the .on() method. Instead you must implement your own. See here how bostock implemented d3-brush.

我放在 this jsFiddle 来演示如何使用D3 v4实现你要求的内容。

I put together this jsFiddle to demonstrate how to achieve with D3 v4 what you're asking.

重要的是实现 .on 方法:

instance.on = function() {
  var value = dispatcher.on.apply(dispatcher, arguments);
  return value === dispatcher ? instance : value;
}

而且,调度就像这样

dispatcher.call("was_clicked", this, "Hello, Foo!");

这篇关于在d3.js v4中重新绑定导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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