【发布时间】:2018-03-28 22:15:05
【问题描述】:
我正在尝试使用带有 Drupal 7.x 的 D3 来可视化网络,但我不知道在我的 js 文件中引用 D3 的正确方法是什么。
到目前为止,我可以构建 json 并使用渲染数组将其传递给我的 js 脚本(并在控制台中显示),但随后调用 D3.js 失败:
Uncaught ReferenceError: d3 is not defined
到目前为止,这是我的 js 代码:
(function($){
Drupal.behaviors.mymodule = {
attach : function(context, settings) {
if(Drupal.settings.mymodule.graph_json !== ''){
// visualize the json
console.log(Drupal.settings.mymodule.graph_json);
// create the newtork
d3.json(Drupal.settings.mymodule.graph_json, function (error, json) {
if (error){
return console.warn(error);
}
});
}
}
};
}(jQuery));
我应该在数组中添加一个引用吗?或者由于 D3.js 在 sites/all/libraries/d3 中,那么有一种通用的调用方式吗?
【问题讨论】:
-
你没有在任何地方引用 d3,要么在你的 html/php 文件的顶部加载它,要么调用
import /path/to/libraries/d3.js,如果你可以使用 es205 模块。 Look here for more methods that may be helpful -
感谢您的评论,我也没有对 jQuery 的任何引用,因为 Drupal 为我处理了这些......因为我已经为 Drupal 安装了 D3 模块,我想知道是否有Drupal 以某种方式处理 D3 引用的方式...
-
您在外部函数中指定 jQuery tho。在包装函数中尝试
(function($, d3){...}(jQuery, d3)) -
如果我喜欢你的建议,我会收到
Uncaught SyntaxError: Unexpected identifier错误
标签: javascript d3.js drupal-7