As I'm making my first steps into d3.js I couldn't help myself noticing its approach is very similar to jQuery.
My question is:
When I need to modify multiple CSS properties of a
style
attribute of matching element is there a shorthand approach, like jQuery or ReactJS provide, like.style({width:100, height:100, backgroundColor:'lightgreen'})`
if I need to apply
width:100px
,height:100px
andbackground-color:lightgreen
to a<div>
.
Sure, I may chain those, but changing multiple properties this way may become tedious:
d3
.select('#test')
.style('width','100px')
.style('height','100px')
.style('background-color','lightgreen')
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script><div id="test"></div>
Or I may combine multiple desired properties within a class and assign that class with a .classed()
, which may also overcomplicate CSS stylesheet when dynamic properties are required:
d3
.select('#test')
.classed('testclass', true)
.testclass {
height: 100px;
width: 100px;
background-color: lightgreen;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script><div id="test"></div>
But those are not techniques I'm interested in.
The d3.style () function in D3.js is used to style the specified node (attribute) with the specified name (Value). In this, if the node has an inline style with the specified name, its value is returned and if the node has not an inline style, the calculated value is returned. Hey geek!
Select an element by id with DOM API and apply the setAttribute with the style attribute. It is easy to apply multiple CSS styles in jquery. CSS styles can also be applied using the selector attribute. Html element is applied styles using element selector (elementname), class selector (.selector) and id selector (#selector) syntax.
The d3.style () function in D3.js is used to style the specified node (attribute) with the specified name (Value). In this, if the node has an inline style with the specified name, its value is returned and if the node has not an inline style, the calculated value is returned.
Because so many different things can be hiding inside of style - maybe the stroke width, or the fill, or the opacity, etc - it gets its very own method, .style (). When you use style, the first parameter is the attribute you want to edit, and the second parameter is the value you’d like it to be.
The accepted answer is not correct ("there's no such syntax documented in API reference"), you can set multiple styles using d3-selection-multi
. Pay attention to the fact that you have to use the method styles()
, not style()
. So, in your case, it would be:
.styles({width:100, height:100, 'background-color':'lightgreen'})
Here is your snippet with that change:
d3.select('#test')
.styles({
'width': '100px',
'height': '100px',
'background-color': 'lightgreen'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div id="test"></div>
<script src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
As d3-selection-multi
is not part of the default bundle, you'll have to reference it separately.
Note: I claimed (in initial version of this answer) that there's no embedded method to solve the OP's problem. And, as of D3 v6.7.0 you still cannot pass your styles as an object directly to
.style()
method
Two options you got by the time of this writing:
const style = {"width":"100px","height":"100px","background-color":"lightgreen"}
Object.entries(style).forEach(([prop,val]) => d3.select("#test").style(prop,val))
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script><div id="test"></div>
Why I would discourage you from doing the latter:
So, whether you apply 1-line solution or add up to 13kB of legacy code to your application bundle for that sole purpose - is totally up to you.
Alternatively, you can use the attr()
method and add the styles as part of the attribute of that particular tag being targeted:
d3.select("#test")
.attr("style", "width: 100px; height: 100px; background-color: lightgreen;")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With