Note that there are some explanatory texts on larger screens.

plurals
  1. POd3 transitioning from bar to pie and back
    primarykey
    data
    text
    <p><a href="http://jsfiddle.net/fresheyeball/52TNQ/" rel="nofollow noreferrer">LIVE DEMO</a></p> <p>So I have this notion that all single axis data should be allowed to be displayed in all the basic ways; and at the very least from a pie to a bar. Ideally this would be an animated transition, but thats were the difficulty comes in.</p> <p>Getting a pie chart to work is easy enough, as is getting a bar chart. Here is what I have so far:</p> <pre><code># fields width = 750 height = width/2 margin = 20 radius = (height-(margin*2))/2 # helpers pie = d3.layout.pie().value (d) -&gt; d arc = d3.svg.arc() .outerRadius(radius) .innerRadius(radius/4) x = d3.scale.linear().domain([0, 100]).range [0, width] $http.get('/Classification_Top_10_by_Count.json').success (data) -&gt; percents = (parseFloat item.Percent for item in data).sort d3.ascending svg = d3.select('#svgStage').append('svg') .attr('width', width+(margin*2)) .attr('height', height+(margin*2)) svg.data([percents]) g = svg.append('g') .attr('transform', "translate(#{radius},#{radius})") paths = g.selectAll 'path' paths.data(pie).enter().append('path') .attr('d', arc) toBars = -&gt; g.selectAll('path').transition().duration(2000) .attr 'd', (d, index) -&gt; # this is over complex because I was playing with it. cord = tl : [0, index*20] tr : [d.value*20, index*20] br : [d.value*20, index*20-20] bl : [0, index*20-20] oCord = [ cord.tl cord.tr cord.br cord.bl ] "M #{oCord[0][0]}, #{oCord[0][2]} A 0, 0 0 0, 0 #{oCord[1][0]}, #{oCord[1][3]} L #{oCord[2][0]}, #{oCord[2][4]} A 0, 0 0 0, 0 #{oCord[3][0]}, #{oCord[3][5]} Z" </code></pre> <p>Obviously for this to work its got to be path element to path element, and the transition is working now. Problem is it looks like crap. The moment it starts it looks garbled, until it over and becomes decent bar chart. </p> <p>I've been looking at this : <a href="http://d3-example.herokuapp.com/examples/showreel/showreel.html" rel="nofollow noreferrer">http://d3-example.herokuapp.com/examples/showreel/showreel.html</a> Which demonstrates a bar transitioning to a donut in much the way I would like. Looking at the source code, this is accomplished through a custom tween. (view source line 518)</p> <p>Now I'm in over my head. What is going on here? How can I get this transition to work? Has anyone else out there dealt with this problem? </p> <hr> <p><strong>UPDATE</strong></p> <p>Just to be clear, below illustrations the intention of my transition abit more clearly.</p> <p><img src="https://i.stack.imgur.com/51A7U.png" alt="enter image description here"></p> <hr> <p>Bounty clarity. I added a bounty to this question because I need an explanation of what was going wrong. Superboggly did that, so he got the bounty. However Amit Aviv's approach is superior, so I accept his answer as the most correct. I have also +1ed both.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload