gpt4 book ai didi

d3.js - 你能在 d3 中通过 foo.style({color :blah, background :blah})?

转载 作者:行者123 更新时间:2023-12-04 13:39:23 27 4
gpt4 key购买 nike

我将在 svg 元素上设置各种 css 样式,并认为我可以做类似的事情

d3.selectAll(".whatever")
.style(function(d) { return {"color":getColor(d), "background":getBackground(d)}});

现在,这不起作用,但我想知道我是否可以做一些类似于集中设置整体样式属性而不是单独设置样式属性的事情。

注意:正如 Ray 建议的那样,您可以执行以下操作(我假设您已经将数据附加到节点):
d3.selectAll(".whatever")
.attr("style",function(d) {
return cssStyleStringYouWantToUse(d);
});

最佳答案

仅适用于 D3 v3 :
报价 the documentation :

If you want to set several style properties at once, use an object literal like so:

selection.style({'stroke': 'black', 'stroke-width': 2})


但是,这对于函数来说是不可能的,因此在您的情况下,您仍然必须使用“长格式”。

关于d3.js - 你能在 d3 中通过 foo.style({color :blah, background :blah})?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23860013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com