chart.js饼图动画,在页面垂直滚动到某个位置时,只播放一次

3
我有一个简单的饼图,它可以在加载时通过 chart.js 自带的动画效果展示出来 -- 我正在尝试让动画在长网页的某个滚动点处排队 -- 想法是以下代码将会执行一次,只有当新用户滚动或到达页面位置时 -- 动画将会排队一次,就这样。这引起了很多麻烦 -- 因为我已经可以通过断点和窗口滚动在页面位置上显示/隐藏元素,但此动画不会内联执行,而是无论我如何尝试实现这一点,都会使动画刷新和重新播放每一次浏览器滚动条被微调。它只会刷新,滚动 > 动画刷新 > 滚动 > 动画刷新。有什么提示吗?chart.js 文档在这方面并不是很有用,因为大多数演示均在 onDomready 中。我已经发现了使用断点执行 jQuery 的方法,正如您可以在下面的注释代码中看到的那样,但其中的实际动画会忽略并在每次滚动调整时触发。
另外,这里是主要的外部 chart.js 文件,支持下面的操作。 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();
                            }
                        }
                    }
                }
            }
        });
    }
});
1个回答

0

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

<script src="http://rawgit.com/morr/jquery.appear/master/jquery.appear.js"></script>

__

/**
 * 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));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接