Using RequireJS to load plotly and D3(使用 RequireJS 加载 plotly 和 D3)
问题描述
我正在尝试使用 RequireJS 在我的 js 代码中加载 plotly 和 d3 库.我试过这个:
I am trying to use RequireJS to load the plotly and d3 libraries in my js code. I have tried this:
require.config({
paths: {
d3: '/static/scripts/plotly/dependencies/d3.v3.min',
plotly: '/static/scripts/plotly/plotly.min'
}
});
require(['d3'], function(d3) {
d3();
});
require(['plotly'], function(plotly) {
plotly();
});
但这不起作用.当我的 js 函数被调用时,我得到:
But this doesn't work. When my js function is called I get:
Uncaught ReferenceError: plotly is not defined
Uncaught ReferenceError: Plotly is not defined
Uncaught ReferenceError: d3 is not defined
Uncaught TypeError: d3 is not a function
在这种情况下使用 RequireJS 的正确方法是什么?
What is the proper way to use RequireJS in this situation?
使用当前版本的代码更新帖子,仍然无法正常工作.
Updating post with current version of code, still not working.
在调用脚本中我现在有这个:
In the calling script I have this now:
require.config({
paths: {
heatmap: '/static/scripts/heatmap',
d3: '/static/scripts/plotly/dependencies/d3.v3.min',
plotly: '/static/scripts/plotly/plotly.min',
}
});
require(['d3', 'plotly', 'heatmap'], function(d3, plotly, heatmap) {
generate_heatmap();
});
它失败了:
Uncaught ReferenceError: Plotly is not defined(anonymous function) @ plotly.min.js:17
函数 Plotly(大写 P)在 plotly.min.js 中定义.我必须在某处添加对该函数的引用吗?
The function Plotly (upper case P) is defined in plotly.min.js. Do I have to add some reference to that function somewhere?
推荐答案
Plotly 需要一个 shim
,因为它依赖于 D3,根据 Plotly 文档,看起来还需要 jQuery.您的配置应如下所示:
You need a shim
for Plotly since it depends on D3, and according to the Plotly docs, it looks like jQuery is also needed. Your config should look like:
/*
main.js file
*/
require.config({
paths: {
d3: '/static/scripts/plotly/dependencies/d3.v3.min',
jquery: '/path/to/jquery',
plotly: '/static/scripts/plotly/plotly.min'
},
shim: {
plotly: {
deps: ['d3', 'jquery'],
exports: 'plotly'
}
}
});
require(['d3', 'plotly'], function(d3, plotly) {
console.log(plotly); // Object {Lib: Object, util: Object...}
console.log(d3); // Object {version: "3.5.6", behavior: Object...}
});
在您的 index.html 中,您应该只有一个脚本标签:
In your index.html you should have only a single script tag:
<script data-main="main" src="/path/to/require/folder/require.js"></script>
如果你想在另一个使用 require 的 JS 文件中使用 D3 和 Plotly,那么你应该使用 AMD 样式:
If you want to use D3 and Plotly in another JS file that uses require then you should be using the AMD style:
/*
foo-bar.js
*/
define(['d3', 'plotly'], function(d3, plotly) {
var foo = {};
function bar() {
// Some code here...
}
// This will allow other modules to use
// the foo object and the bar function.
return fooBar {
foo: foo,
bar: bar
}
});
现在,您可以在另一个模块中使用 foo-bar.js(假设两个文件位于同一目录级别):
Now, you can use foo-bar.js in another module (assuming the two files are on the same directory level):
define(['./foo-bar'], function(fooBar) {
console.log(fooBar.foo) // Object
console.log(fooBar.bar) // function() {}
});
这篇关于使用 RequireJS 加载 plotly 和 D3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 RequireJS 加载 plotly 和 D3
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01