chart.js饼图动画,播放一次,在页面中的某个位置通过垂直滚动

我有一个简单的饼图,从chart.js开箱即可加载动画 – 我试图允许动画通过长垂直网页中的某个滚动点进行排队 – 想法是针对以下代码执行,只有一次新用户滚动或点击页面位置 – 动画将排队一次,就是这样。 这会造成很多麻烦 – 因为我已经能够使用断点和窗口滚动执行和显示/隐藏页面位置的元素,但是这个动画不会与内联执行,而是我试图实现这一点的每一种方式; 每当浏览器滚动条调整最轻微时,动画会刷新并重放。 它只是刷新,滚动>动画刷新>滚动>动画刷新。 有任何指针; chart.js文档在这方面不是很有用,因为大部分演示都是onDomready。 我已经发现了使用断点执行jQuery的方法,正如您在下面的注释代码中看到的那样,但实际的动画只是忽略并在每次滚动调整时触发。

这里还有主要的外部chart.js文件,它支持以下function。

Chart.JS外部JS

var breakpoint = false; $(window).scroll(function() { if ($(this).scrollTop() > 1300 && !breakpoint ) { // do stuff // $(window).scroll(function() { // if ($(this).scrollTop() > 1100) { // $(function () { // $(document).ready(function () { var chart = null; var colors = Highcharts.getOptions().colors, categories = ['Shop', 'Buy', 'Own'], name = 'Browser brands', data = [{ // y: 55.11, // color: colors[0], // drilldown: { // name: 'MSIE versions', // categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], // data: [10.85, 7.35, 33.06, 2.81], // color: colors[0] // } // }, { y: 3, color: colors[8], drilldown: { name: 'Buy', // categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'], data: [10.20, 10.83, 11.58, 13.12, 5.43], color: colors[8] } }, { y: 7, color: colors[8], drilldown: { name: 'Own', // categories: ['Chrome 5.0', 'Chrome 6.0', 'Chrome 7.0', 'Chrome 8.0', 'Chrome 9.0', // 'Chrome 10.0', 'Chrome 11.0', 'Chrome 12.0'], data: [0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22], color: colors[8] } }, { y: 10, color: colors[8], drilldown: { name: 'Own', // categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon', // 'Safari 3.1', 'Safari 4.1'], data: [4.55, 5.42, 6.23, 0.21, 0.20, 0.19, 0.14], color: colors[8], } // }, { // y: 2.14, // color: colors[8], // drilldown: { // name: 'Home', // // categories: ['Opera 9.x', 'Opera 10.x', 'Opera 11.x'], // data: [20.1, 0.37, 1.65], // color: colors[8] // } }]; // Build the data array var browserData = []; for (var i = 0; i < data.length; i++) { // add browser data browserData.push({ name: categories[i], y: data[i].y, color: data[i].color }); } // Create the chart chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'pie' }, title: { text: null }, series: [{ name: '', data: browserData, innerSize: '20%' }], tooltip: { valueSuffix: '%', positioner: function () { return { x: this.chart.series[0].center[0] - (this.label.width / 2) + 8, y: this.chart.series[0].center[1] - (this.label.height / 2) + 8 }; } }, plotOptions: { pie: { cursor: 'pointer', dataLabels: { connectorColor: 'white' }, point: { events: { mouseOver: function () { if (!this.connector.dynamicConnector) { var width = 16, height = 24; // Extract the connector start position var dArr = this.connector.d.split(' '); var startY = dArr.pop(), startX = dArr.pop(); var start = [parseFloat(startX), parseFloat(startY)]; // Construct the triangle var path = 'M ' + start[0] + ' ' + start[1] + 'L ' + (start[0] + height) + ' ' + (start[1] - (width / 2)) + ' L ' + (start[0] + height) + ' ' + (start[1] + (width / 2)) + ' L ' + start[0] + ' ' + start[1]; // Convert the section angle from radian to degree and apply to the trangle // Setup rotation, x, y required by the updateTransform method this.connector.rotation = (this.angle * 180) / Math.PI; this.connector.x = startX; this.connector.y = startY; this.connector.updateTransform(); this.connector.attr('stroke', this.color); this.connector.attr('fill', Highcharts.Color(this.color).brighten(0.2).get()); this.connector.attr('d', path); this.connector.dynamicConnector = true; } this.connector.show(); }, mouseOut: function () { this.connector.hide(); } } } } } }); } }); 

如果有人有兴趣 – 我用这个插件实现了我的目标。 Appear.js

  

__

 /** * Highcharts plugin to defer initial series animation until the element has appeared. Requieres * jQuery.appear. */ (function (H) { function deferAnimate (proceed, init) { var series = this, $renderTo = $(this.chart.container.parentNode); // Prevent pre-rendering without animation if (init) { series.group.hide(); } // Prepare for animation if (init) { $renderTo.appear(); // initialize appear plugin proceed.call(this, init); // It is appeared, run animation } else if ($renderTo.is(':appeared')) { proceed.call(series); series.group.show(); // It is not appeared, halt animation until appear } else { $renderTo.on('appear', function () { if (!series.animated) { proceed.call(series); series.group.show(); series.animated = true; } }); } }; H.wrap(H.Series.prototype, 'animate', deferAnimate); H.wrap(H.seriesTypes.column.prototype, 'animate', deferAnimate); H.wrap(H.seriesTypes.pie.prototype, 'animate', deferAnimate); }(Highcharts));