简体   繁体   English

将D3图插入CMS中的div

[英]Insert D3 Graph into a div in CMS

I'm using Bludit CMS to build a simple website. 我正在使用Bludit CMS构建一个简单的网站。 The only task I could handle is to add a D3 Graph into the site. 我唯一可以处理的任务是将D3图添加到站点中。 My Graph and the code looks pretty like this: http://bl.ocks.org/mbostock/1093130 我的图和代码看起来像这样: http : //bl.ocks.org/mbostock/1093130

The graph is displayed at the position, where I insert the script tag of the graph. 该图显示在我插入该图的脚本标签的位置。 The problem is, that I'm not able to add a script tag in Bludit CMS, only HTML. 问题是,我无法在Bludit CMS中添加脚本标记,只能在HTML中添加脚本标记。 Now, I thought about adding an empty div tag with an id to my CMS editor like 现在,我考虑将一个带有id的空div标签添加到CMS编辑器中,例如

<div id="myGraph">
</div>

and add the graph programmatically to this div. 并以编程方式将图形添加到该div。 How can I do that? 我怎样才能做到这一点? One further note: I want to try to do it only with Javascript without using jQuery. 进一步说明:我想尝试仅使用Javascript而不使用jQuery。 I don't want to include jQuery to my page only to add a graph to my website. 我不想将jQuery包含到我的页面中只是为了向我的网站添加图形。

If you can't add JavaScript to a page you won't be able to use D3. 如果无法将JavaScript添加到页面,则将无法使用D3。

If you can load in code from an external file then use: 如果可以从外部文件加载代码,请使用:

<script src="myfile.js"></script>

If you can only add it to every page rather than just one then check for your id and then execute the code. 如果您只能将其添加到每一页而不是仅添加到每一页,则请检查ID,然后执行代码。

if( d3.select("#myGraph") ) {
   var svg = d3.select("#myGraph")
         .append( "svg")
         .attr("width", 1000)
         .attr("height", 1000);

   // Do stuff with SVG.
}

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

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