You can abstract out the code which determines the rotation angle simplify the expression as:
function rotation(d, x) {
return (x(d.x + d.dx) - x(d.x)) > 50 ? 0 : 90;
}
// ...
.attr("transform", function (d) {
return "translate(" + [(x(d.x + d.dx / 2)), (y(d.y + d.dy / 2))] + ")" +
"rotate(" + rotation(d, x) + ")";
})
Then whenever you need the value of rotation of a point which is bound to data d
, call rotation(d, x)
.