【问题标题】:correct way to call D3.js object in Drupal module在 Drupal 模块中调用 D3.js 对象的正确方法
【发布时间】: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


【解决方案1】:

根据您问题底部的陈述:

我应该在数组中添加一个引用吗?或者由于 D3.js 在 sites/all/libraries/d3 中,那么有一种通用的调用方式吗?

您将d3.js 文件放在错误的目录sites/all/libraries/d3.js 中。该文件应位于正确的目录中,如下面的屏幕截图所示。

按照以下步骤操作:

  • 下载D3 library
  • 提取到sites/all/libraries/d3/
  • 确保d3.js文件在正确的目录sites/all/libraries/d3/d3.js

截图:

之后,如果您发现另一个错误Uncaught TypeError: Cannot read property 'linear' of undefined,您应该阅读my answer

这对我有用。

希望对你有帮助。

【讨论】:

  • 谢谢,但我已经在正确的文件夹 sites/all/libraries/d3/ 中有 D3.js
  • 我需要在我的 js 脚本中引用 d3,我只是不知道该怎么做……使用 Drupal.d3 不起作用
  • 我根据你的配置(sites/all/libraries/d3.js)和错误(Uncaught ReferenceError: d3 is not defined)回答...只有当您将库配置到错误的目录时才会出现这种错误。
  • 对不起,但这不是我的配置,我将 d3.js 放入了站点/all/libraries/d3/ 不知道我是否正确表达了我的问题,但问题是如何引用 d3在我自己的脚本中
猜你喜欢
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多