diff --git a/chartist/chartist-tests.ts b/chartist/chartist-tests.ts index 513ee255dd..db4fbfbfcb 100644 --- a/chartist/chartist-tests.ts +++ b/chartist/chartist-tests.ts @@ -14,7 +14,7 @@ new Chartist.Line('.ct-chart', { } }); -var chart = new Chartist.Line('.ct-chart', { +var lineChart = new Chartist.Line('.ct-chart', { labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], series: [ [5, 5, 10, 8, 7, 5, 4, null, null, null, 10, 10, 7, 8, 6, 9], @@ -43,8 +43,6 @@ new Chartist.Line('.ct-chart', { ] }); - - var data = { series: [5, 3, 4] }; @@ -66,3 +64,53 @@ new Chartist.Pie('.ct-chart', { total: 200, showLabel: false }); + + +// Animation Donut example + +var chart = new Chartist.Pie('.ct-chart', { + series: [10, 20, 50, 20, 5, 50, 15], + labels: [1, 2, 3, 4, 5, 6, 7] +}, { + donut: true, + showLabel: false +}); + +chart.on('draw', function(data: any) { + if(data.type === 'slice') { + // Get the total path length in order to use for dash array animation + var pathLength = data.element._node.getTotalLength(); + + // Set a dasharray that matches the path length as prerequisite to animate dashoffset + data.element.attr({ + 'stroke-dasharray': pathLength + 'px ' + pathLength + 'px' + }); + + // Create animation definition while also assigning an ID to the animation for later sync usage + var animationDefinition: any = { + 'stroke-dashoffset': { + id: 'anim' + data.index, + dur: 1000, + from: -pathLength + 'px', + to: '0px', + easing: Chartist.Svg.Easing.easeOutQuint, + // We need to use `fill: 'freeze'` otherwise our animation will fall back to initial (not visible) + fill: 'freeze' + } + }; + + // If this was not the first slice, we need to time the animation so that it uses the end sync event of the previous animation + if(data.index !== 0) { + animationDefinition['stroke-dashoffset'].begin = 'anim' + (data.index - 1) + '.end'; + } + + // We need to set an initial value before the animation starts as we are not in guided mode which would do that for us + data.element.attr({ + 'stroke-dashoffset': -pathLength + 'px' + }); + + // We can't use guided mode as the animations need to rely on setting begin manually + // See http://gionkunz.github.io/chartist-js/api-documentation.html#chartistsvg-function-animate + data.element.animate(animationDefinition, false); + } +});