使用D3将SVG canvas附加到body以外的元素 [英] Append SVG canvas to element other than body using D3

查看:433
本文介绍了使用D3将SVG canvas附加到body以外的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果脚本不在该元素内,是否可以使用D3.js将SVG画布附加到除< body> 之外的元素?



例如,下面的代码不起作用:

 < html& 
< body>
< script src =../../ d3.v2.js>< / script>
< script>
var vis = d3.select(#container)
.append(svg:svg)
.attr(width,w)
.attr高度,h);
< / script>

< div id =container>< / div>

< / body>

我看到的所有示例使用 d3.select(body)。 append .... 但显然我们并不总是想把canvas附加到body。



我发现如果< script> 在容器div中,然后我可以使用 d3.select(#container)对我来说奇怪,我必须将我的脚本包含在我想要画布的特定容器中。

解决方案

您尝试在存在之前选择 #container 。您需要将您的代码放在 onload 中或将脚本移动到页面中的 #container 下面。 p>

Is it possible to append an SVG canvas to an element other than <body> using D3.js if the script is not inside that element?

For example, the code below does not work:

<html>
<body>
<script src="../../d3.v2.js"></script>
<script>
    var vis = d3.select("#container")
        .append("svg:svg")
          .attr("width",w)
          .attr("height",h);
</script>

<div id="container"></div>

</body>

All examples I have seen use d3.select("body").append.... but clearly we don't always want to append the canvas immediately to the body.

I found that if the <script> is inside the container div then I can use d3.select("#container") but it seems strange to me that I would have to include my script inside of the specific container(s) where I want the canvas.

解决方案

You're trying to select #container before it exists. You'll need to put your code in an onload or move the script below #container in the page.

这篇关于使用D3将SVG canvas附加到body以外的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆