admin管理员组文章数量:1425888
Is there anything like Array.map()
for a D3 selection? To find the maximum length of a selection of SVG <text/>
elements, I currently do the following. However .selectAll(...)[0].map(...)
seems like a hack to me. Is there a better way?
var chi = svg.selectAll('.chi');
var xChi = d3.max(chi[0].map(function (itm) { return itm.getComputedTextLength(); }));
I can use selection.each()
to iterate a function over a selection, but I'm not sure how to bine this with d3.max()
. I tried d3.max(chi.each(function (itm) { return itm.getComputedTextLength(); }))
, but d3.each()
doesn't return an array of the return values
Is there anything like Array.map()
for a D3 selection? To find the maximum length of a selection of SVG <text/>
elements, I currently do the following. However .selectAll(...)[0].map(...)
seems like a hack to me. Is there a better way?
var chi = svg.selectAll('.chi');
var xChi = d3.max(chi[0].map(function (itm) { return itm.getComputedTextLength(); }));
I can use selection.each()
to iterate a function over a selection, but I'm not sure how to bine this with d3.max()
. I tried d3.max(chi.each(function (itm) { return itm.getComputedTextLength(); }))
, but d3.each()
doesn't return an array of the return values
2 Answers
Reset to default 2var allLength
d3.selectAll(".element")
.each(function(d, i) {allLength[i]=d3.select(this).node().getComputedTextLength() })
var maxLength = d3.max(a)
Yea..that bracket makes my eyes sore too.
d3.max(svg.selectAll('.chi').pop(), function(item) {
return itm.getComputedTextLength()
})
The question was from 2012. I don't know what d3 was like back then, but the current version has d3.max
accepts an accessor function to retrieve the value you're max-ing.
本文标签: javascriptArraymap() and D3 selectionStack Overflow
版权声明:本文标题:javascript - Array.map() and D3 selection? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745394467a2656749.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论