javascript 如何使用 d3js 获取 svg 元素的宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19710174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the width of an svg element using d3js?
提问by linto cheeran
I am using d3js to find the width of an svg element the code given below:
我正在使用 d3js 找到下面给出的代码的 svg 元素的宽度:
<script>
var body = d3.select("body");
console.log(body);
var svg = body.select("svg");
console.log(svg);
console.log(svg.style("width"));
</script>
<svg class="svg" height="3300" width="2550">
<image x="0" y="0" height="3300" width="2550" xlink:href="1.jpg"></image>
<rect class="word" id="15" x="118" y="259" width="182" height="28"
text="Substitute"></rect>
</svg>
But it returned this error:
但它返回了这个错误:
Uncaught TypeError: Cannot call method 'getPropertyValue' of null
未捕获的类型错误:无法调用 null 的方法“getPropertyValue”
I think, the svg variable is a null array.
我认为, svg 变量是一个空数组。
How can I get the width of an svg element using d3js?
如何使用 d3js 获取 svg 元素的宽度?
采纳答案by Andrew Surzhynskyi
<svg class="svg" height="3300" width="2550">
<image x="0" y="0" height="3300" width="2550" xlink:href="1.jpg"></image>
<rect class="word" id="15" x="118" y="259" width="182" height="28"
text="Substitute"></rect>
</svg>
<script>
var body = d3.select("body");
console.log(body);
var svg = body.select("svg");
console.log(svg);
console.log(svg.style("width"));
</script>
Just place your script after svg element is loaded by the browser and all will be fine.
只需在浏览器加载 svg 元素后放置您的脚本,一切都会好起来的。
回答by greg
if you have an SVG element w/ id=frame...
如果您有一个带有 id=frame 的 SVG 元素...
frame = d3.select('#frame')
.attr('class', 'frame')
fh = frame.style("height").replace("px", "");
fw = frame.style("width").replace("px", "");
fh and fw can now be used in math expressions.
fh 和 fw 现在可用于数学表达式。
you can also drop back to jQuery
你也可以回到 jQuery
fw = console.log($("#frame").width());
fh = console.log($("#frame").height());
which are numbers and can be used in math expressions
哪些是数字,可用于数学表达式
回答by kpmartin
var svg = d3.select("body")
.append("svg")
.attr("width", "100%")
.attr("height", "100%");
var w = parseInt(svg.style("width"), 10);
var h = parseInt(svg.style("height"), 10);
回答by circleofconfusion
Try using svg.attr("width"). It just gives a plain number string.
尝试使用 svg.attr("width")。它只是给出一个普通的数字字符串。
回答by user3724163
If this svg is saved in a variable, for example:
如果此 svg 保存在变量中,例如:
var vis = d3.select("#DivisionLabel").append("svg")
.attr("width", "100%")
.attr("height", height);
Then directly one can use the variable vis
to get the width of svg by:
然后可以直接使用该变量vis
通过以下方式获取 svg 的宽度:
console.log(vis.style("width"));
The result is in unit "px". Pay attention that vis.style("width") returns a string.
结果以“px”为单位。注意 vis.style("width") 返回一个字符串。
回答by Ricardo Boriba
// HTML
// HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<svg width="950" height="500"></svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="index1.js"></script>
</body>
</html>
//JavaScript
//JavaScript
const svg = d3.select('svg');
const width = +svg.attr('width');
const height = +svg.attr('height');
console.log(width)
console.log(height)
At this days this is what work for me.
在这一天,这对我有用。