从谷歌图表动画柱形图

时间:2013-12-12 21:00:31

标签: javascript titanium google-visualization appcelerator

当我使用滑块前后移动时,我无法想出一个能够为我的图表内的单个栏栏设置动画的功能。我在Google图表上阅读了动画文档API文档,但我很难理解我需要做什么。

到目前为止,这是我的代码。我将从哪里开始找出如何使用我用钛制作的滑块来设置我的一个条形图?我使用evalJS函数从app.js文件中调用函数updateChart()。我已经验证了它的工作原理,当我的滑块来回移动时,通过执行console.log。我似乎无法绕过它如何应用它来动画单个栏栏。任何想法都表示赞赏。

在我的html页面上设置Google Charts。

    <script type="text/javascript" src ="https://www.google.com/jsapi" > </script>
    <script type="text/javascript ">
        google.load("visualization", "1", {packages:["corechart"]});
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Importance');
            data.addColumn('number', 'Earning');
            data.addColumn({type: 'string', role: 'style'});

            data.addRows([['',5,'#000000'], ['',5,'#ffffff'],['',5,'#666666'],['', 5,'#cccccc']]); 

        var options = 
        {
            width: 200, 
            height: 240, 
            legend: {
                position: 'none'
            }, 
            chartArea: {
                backgroundColor: 'none'
            }, 
            bar: {
                groupWidth: '100%'
            },
            animation: {
                duration: 1000,
                easing: 'out'
            }
        };

        function updateChart() {

        }

        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
            chart.draw(data, options);
        }

编辑代码:

    <script type="text/javascript" src ="https://www.google.com/jsapi" > </script>
    <script type="text/javascript ">
        google.load("visualization", "1", {packages:["corechart"]});
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Importance');
            data.addColumn('number', 'Earning');
            data.addColumn({type: 'string', role: 'style'});

            data.addRows([['',5,'#000000'], ['',5,'#ffffff'],['',5,'#666666'],['', 5,'#cccccc']]); 

                var options = {
                width: 200, 
                height: 240, 
                legend: {
                    position: 'none'
                }, 
                chartArea: {
                    backgroundColor: 'none'
                }, 
                bar: {
                    groupWidth: '100%'
                },
                animation: {
                    duration: 1000,
                    easing: 'out'
                }
        };

        function updateChart(value) {
            data.setValue(0, 1, value);
            chart.draw(data, options);
        }

        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
            chart.draw(data, options);
        }

Slider Hook Code在单独的文件(app.js)Titanium Platform

var sliderBarOne = Titanium.UI.createSlider({
    top: '310',
    left: '610',
    min: '0',
    max: '10',
    width: '37%',
    value: '5',
    backgroundImage: 'assets/sliderBar.png'
});
sliderBarOne.addEventListener('change', function(e) {
    chartView.evalJS("updateChart('" + e.value + "');");
});

1 个答案:

答案 0 :(得分:0)

您需要为调用updateChart函数的滑块挂钩事件侦听器。 updateChart函数应该从滑块获取值并更改DataTable中的值,然后调用图表的draw方法。如何将事件侦听器连接到滑块将取决于您用于滑块的库。下面是一些示例代码,假设您的滑块对象具有change方法来设置更改事件处理程序,并使用`getValue方法来获取滑块的值:

[编辑:添加到您的滑条代码中]

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Importance');
    data.addColumn('number', 'Earning');
    data.addColumn({type: 'string', role: 'style'});

    data.addRows([
        ['',5,'#000000'],
        ['',5,'#ffffff'],
        ['',5,'#666666'],
        ['', 5,'#cccccc']
    ]); 

    var options = {
        width: 200, 
        height: 240, 
        legend: {
            position: 'none'
        }, 
        chartArea: {
            backgroundColor: 'none'
        }, 
        bar: {
            groupWidth: '100%'
        },
        animation: {
            duration: 1000,
            easing: 'out'
        }
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));

    function updateChart(value) {
        data.setValue(0, 1, value);
        chart.draw(data, options);
    }

    // create slider
    var sliderBarOne = Titanium.UI.createSlider({
        top: '310',
        left: '610',
        min: '0',
        max: '10',
        width: '37%',
        value: '5',
        backgroundImage: 'assets/sliderBar.png'
    });

    sliderBarOne.addEventListener('change', function(e) {
        updateChart(e.value);
    });

    chart.draw(data, options);
}