简体   繁体   English

D3库可以与Electron(Atom shell)一起使用吗?

[英]Can D3 library be used with the Electron (Atom shell)?

Electron's website says that the applications made with electron can have access to node modules. Electron的网站称,用电子制作的应用程序可以访问节点模块。 Can they have access to the D3 library? 他们可以访问D3库吗? If so, how can it be set up? 如果是这样,怎么设置?

D3 is available as a Node.js module that can be imported into the JavaScript code you want to use to render your visualisation application. D3可用作Node.js模块 ,可以导入到要用于呈现可视化应用程序的JavaScript代码中。

As an example of how to integrate D3 into an Electron application, have a look at my D3 Space Filler Explorer application on GitHub. 作为如何将D3集成到Electron应用程序的示例,请查看我在GitHub上的D3 Space Filler Explorer应用程序。 This application visualises disk space use with multiple D3 pie charts and a D3 treemap. 此应用程序使用多个D3饼图和D3树图可视化磁盘空间。

One pattern I found useful was to inject the SVG element into the D3 visualisation, which differs from the usual approach in D3 examples which creates the SVG element in the visualisation. 我发现有用的一种模式是将SVG元素注入到D3可视化中,这与D3示例中的常规方法不同,后者在可视化中创建了SVG元素。 See examples of this dependency injection in the /app/js/pie-chart.js and /app/js/treemap-chart.js files. 请参阅/app/js/pie-chart.js和/app/js/treemap-chart.js文件中的此依赖项注入示例。

All (at least theoretically) pure JS modules are compatible with electron, since it also provides a (CommonJS) javascript runtime environment (io.js). 所有(至少在理论上)纯JS模块都与​​电子兼容,因为它还提供了一个(CommonJS)javascript运行时环境(io.js)。

The only important thing is that electron doesn't automatically sets the NODE_PATH variable and doesn't look in system/global modules path for require d modules. 唯一重要的是电子不会自动设置NODE_PATH变量,也不会查找require d模块的系统/全局模块路径。 So you just have to make sure that you have the path to d3.js on your NODE_PATH : 所以,你只需要确保你的路径d3.jsNODE_PATH

NODE_PATH="/PATH/TO/d3.js" electron /PATH/TO/APP

We solved this in our workteam installing d3 with Npm: 我们在使用Npm安装d3的工作团队中解决了这个问题:

npm install d3 --save

and in index.html we put this: 在index.html中我们把这个:

<script>var d3 = require("d3")</script>

We were getting this issue from nv.d3.js line 18, there is a little function requiring d3 as a node module and in our app we were using it in bower_components, so installing it with npm and requesting in your index directly from node_modules like as I said will probably solve this issue as it did with us. 我们从nv.d3.js第18行得到了这个问题,有一个小函数需要d3作为节点模块,在我们的应用程序中我们在bower_components中使用它,所以用npm安装它并直接从node_modules请求你的索引正如我所说,可能会像我们一样解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM