Can Not Perform A Class Transition In D3.js
Solution 1:
Simply put, you are trying to do this:
circle.classed("blackCircle",true);
circle.transition()
.classed("blackCircle",false);
.classed("whiteCircle",true);
The issue is that a transition doesn't offer a classed
method. If you look at the error message you get, you can confirm this: circle.transition(...).classed is not a function
.
(See the docs for available methods)
A d3 transition interpolates between a starting value and an end value - and as a boolean value is either true or false, it is hard to transition between these two points using only those two values. Hence, classed
cannot be a method of a d3 transition in its current form.
If you would like this to work with d3, you could abandon the class approach and apply styles directly using selection.style()
and transition.style()
.
For example:
var svg = d3.select("body")
.append("svg");
var circle = svg.append("circle")
.attr("cx",100)
.attr("cy",50)
.attr("r", 20)
.style("fill","steelblue");
var t = d3.transition()
.duration(3000);
circle.transition(t)
.style("fill","orange");
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
I'll also add that something like:
circle.attr("class","whiteCircle");
circle.transition().attr("class","blackCircle");
Won't work as well, despite .attr() being a method of d3.transition(). This is because the interpolator will attempt to interpolate the string values between start and end using d3.interpolateString. Given two strings without numbers, the second value will be used at all points on the transition (See this fiddle).
Also, you use background as a css property for the svg circle, you need to use fill when working with svg elements. Likewise, instead of border you need to use stroke, stroke-width, etc
Post a Comment for "Can Not Perform A Class Transition In D3.js"