I am using SVG for charts in an AJAX web application. When the source data is received, I would like to animate the elements of the chart (for example, bars) from the default values of 0 to the new data value. If the user selects a new data set, I request new data from the server, and then I would like to animate the panel from the previous value to the new value.
<rect x="0" y="0" width="1px" height="20px" fill="red">
<animate id="anim" attributeName="width" attributeType="XML"
begin="indefinite" from="0" to="100px" dur="0.8s" fill="remove"/>
</rect>
Every time the data is received, I set the attributes fromand toand call beginElement().
If I set fill="remove"SVG in animations, then the panel animates correctly every time a new data set is loaded, but, of course, between animations the panel returns to its default width.
Setting width.baseVal.valueto set a new base value before OR after a call beginElement()causes a very unpleasant flicker.
So, I tried using a fill="freeze"line to keep its width at the end of each animation. The problem is that the first animation works, but each subsequent call beginElement()immediately returns the panel to its default width and the animation stops working.
I am testing in Chrome 12.0. Here is an example to see how the animation breaks when using freeze.
http://jsfiddle.net/4ws9W/
<!DOCTYPE html>
<html>
<head><title>Test SVG animation</title></head>
<body>
<svg width="200px" height="20px" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="0" y="10px" width="200px" height="2px"/>
<rect x="0" y="0" width="1px" height="20px" fill="red">
<animate id="anim" attributeName="width" attributeType="XML" begin="indefinite" from="0" to="100px" dur="1.3s" fill="freeze"/>
</rect>
parent.anim = document.getElementById('anim');
</svg>
<br/>
<a href="javascript:anim.beginElement();">anim.beginElement();</a>
<br/>
<a href="javascript:anim.endElement();">anim.endElement();</a>
<br/>
<br/>
<a href="javascript:anim.setAttribute('to', '50px');">anim.setAttribute('to', '50px');</a>
<br/>
<a href="javascript:anim.setAttribute('to', '150px');">anim.setAttribute('to', '150px');</a>
<br/>
<br/>
<a href="javascript:anim.setAttribute('fill', 'remove');">anim.setAttribute('fill', 'remove');</a>
<br/>
<a href="javascript:anim.setAttribute('fill', 'freeze');">anim.setAttribute('fill', 'freeze');</a>
<br/>
</body>
</html>
How can I characterize the bandwidth, save its new width, and then animate it to new values as they become available?