Im currently working on the d3 curriculum. Right from the start i found the multiple use of attr()
/style()
syntax very inconvenient and was already thinking of a way to come around it. Contrary to my expectations, attr()
wont accept object as parameter, one which could hold multiple attributes and their respective values, or at least thats the case with codepen d3 module. Im sure there must a a technique to reduce the code and im here to ask fellow coders for some hints . I wonder is there a way to make a custom
attr()
method, one that could attach multiple attributes at time, something like d3.select('svg).myAttr(...)
, but my skills are not sufficient. So far i came up with simple function, which would accept as arguments an element and an object with attributes to attach to it:
function myAtr(elem, obj){
for (key in obj){
elem.attr(key, obj[key])
}
}
But obviously it cant be applied similarly to the d3 syntax(which i wish), by applying it as a method (selection.method()
)