2

我正在尝试通过添加行和刷新图表来使用过渡动画来实现图表的加载动画。但它的行为与我的预期完全不同。这是小提琴http://jsfiddle.net/jATUX/ 一些代码:

google.load('visualization', '1', { packages: ['corechart'], callback: function() {
        var data = new google.visualization.DataTable();
        data.addRows(5);

        data.addColumn('string', '');
        data.addColumn('number', 'Sales');

        var options = {
            title: 'Sales by months for 2013 year', curveType: 'function',
            "vAxis": { "minValue": "0" }, "hAxis": { "slantedTextAngle": "45", "slantedText": "true" }, "legend": { "position": "top" }, "pointSize": "5",
            animation: { duration: 1000 }
        };
        var chart = new google.visualization.LineChart(document.getElementById('test'));

        var index = 0;
        var chartData = [
            ['Jan', 5 ], ['Feb', 1], ['Mar', 4], ['Apr', 2], ['May', 3]
        ]
        var drawChart = function() {
            console.log('drawChart index ' + index);
            if (index < chartData.length) {
                data.addRows([chartData[index++]]);
                chart.draw(data, options);
            }
        }

        google.visualization.events.addListener(chart, 'ready', drawChart);
        chart.draw(data, options);
    }});
4

1 回答 1

6

好的,我已经设法解决了。这完全是使用“animationfinish”的问题现在看起来像http://jsfiddle.net/HDu8H/

google.load('visualization', '1', { packages: ['corechart'], callback: function() {
        var data = new google.visualization.DataTable();
        data.addRows(5);

        data.addColumn('string', '');
        data.addColumn('number', 'Sales');
        data.addRows(5);
        data.setValue(0, 0, 'Jan');
        data.setValue(1, 0, 'Feb');
        data.setValue(2, 0, 'Mar');
        data.setValue(3, 0, 'Apr');
        data.setValue(4, 0, 'May');

        var options = {
            title: 'Sales by months for 2013 year', curveType: 'function',
            "vAxis": { "minValue": "0", "maxValue": 6 }, "hAxis": { "slantedTextAngle": "45", "slantedText": "true" }, "legend": { "position": "top" }, "pointSize": "5",
            animation: { duration: 250 }
        };
        var chart = new google.visualization.LineChart(document.getElementById('test'));

        var index = 0;
        var chartData = [ 5, 1, 4, 2, 3 ]
        var drawChart = function() {
            console.log('drawChart index ' + index);
            if (index < chartData.length) {
                data.setValue(index, 1, chartData[index++]);
                chart.draw(data, options);
            }
        }

        google.visualization.events.addListener(chart, 'animationfinish', drawChart);
        chart.draw(data, options);
        drawChart();
    }});
于 2013-05-31T12:32:58.067 回答