javascript - 是否可以将 d3.js 与 gatsby.js 框架一起使用?

标签 javascript node.js d3.js svg gatsby

我正在创建个人网站/作品集,我偶然发现了强大的 gatsby.js 包。

出于研究目的,我还必须可视化一个复杂的数据集,我想使用 d3.js,并在我的 Gatsby 支持的网站中包含我创建的仪表板。

可以在 React 组件中使用 d3 -> https://medium.com/@Elijah_Meeks/interactive-applications-with-react-d3-f76f7b3ebc71

理论上Gatsby应该可以支持d3集成,但是我的尝试至今都失败了。

这是我尝试过的:

完成 Gatsby 教程 https://www.gatsbyjs.org/tutorial/

我正在使用来自 gatsbyjs 文档的完整的第 4 个教程站点,并添加了以下内容

npm install --save d3

添加了 utils/d3.js

文件内容

import d3 from "d3"
module.exports = d3

我还在 gatsby-config.js 插件中添加了 d3。

我运行 gatsby develop,收到以下错误,挂起。

success delete html files from previous builds — 0.005 s
success open and validate gatsby-config.js — 0.003 s
(node:8725) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 2): Error: Unable to find plugin "d3"
(node:8725) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

任何反馈都会有所帮助,如果这是一项棘手的壮举,那么我实现 d3 集成和简单的个人网站框架目标的阻力最小的途径是什么?

2017 年 9 月 8 日更新

我切换到 Hello World!调试 d3 问题的教程。我试过d3d3-node npm 包。

import d3 from "d3" 添加到我的 index.js 文件后,在引导完成后出现了两个类似的错误。

这两个错误在编译尝试中循环并分别输出:

d3 错误

ERROR  Failed to compile with 2 errors  
These dependencies were not found:

* child_process in ./~/xmlhttprequest/lib/XMLHttpRequest.js

* fs in ./~/xmlhttprequest/lib/XMLHttpRequest.js

To install them, you can run: npm install --save child_process fs

d3 Node 错误 将 index.js 上的导入切换为“d3-node”

ERROR  Failed to compile with 13 errors

These dependencies were not found:

* fs in ./~/jsdom/lib/jsdom.js, ./~/jsdom/lib/jsdom/browser/resource-loader.js and 3 others
* net in ./~/tough-cookie/lib/cookie.js, ./~/forever-agent/index.js and 1 other
* child_process in ./~/jsdom/lib/jsdom/living/xmlhttprequest.js
* tls in ./~/forever-agent/index.js, ./~/tunnel-agent/index.js

To install them, you can run: npm install --save fs net child_process tls

These relative modules were not found:

* ../file-api/FileReader-impl.js in ./~/jsdom/lib/jsdom/living/generated/FileReader.js
* ../events/MutationEvent-impl.js in ./~/jsdom/lib/jsdom/living/generated/MutationEvent.js

最佳答案

这个问题与最新版本的 D3 v4 使用一堆 node.js 依赖关系有关,正如这个 gatsby github 问题中所解释的:

https://github.com/gatsbyjs/gatsby/issues/2107

解决方案是修改您的 webpack 配置,使其加载正确版本的 d3。

我在 gatsby 项目中使用 D3 创建一个力导向图,我发现我可以通过只加载 d3-force 库而不是整个 d3 包来回避这个问题。无论如何,这是更可取的,因为让 D3 直接操作 DOM 是反 react 的。更好的方法是使用 D3 进行计算并对渲染使用react,如下所示:

https://medium.com/walmartlabs/d3v4-forcesimulation-with-react-8b1d84364721

关于javascript - 是否可以将 d3.js 与 gatsby.js 框架一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46065992/

相关文章:

node.js - Node js : How to access local variable between the methods

d3.js - 通过fabric.js推送D3.js输出以支持IE8?

javascript - D3.js 中 voronoi 多边形的生成颜色

javascript - D3.js:缩放 svg 时错过刻度线

javascript - 以特定格式将数组保存到MySQL

javascript - 服务器端 setInterval/clearInterval 与 Meteor

javascript - Electron的WebView.loadURL触发重新加载

javascript - Meebo Bar 等工具栏如何工作?

javascript - 使用 AngularJS 加载 JSON 文件

javascript - 全类事件发生故障