简体   繁体   English

Google图表中的动画柱形图

[英]Animating column chart from google charts

I'm having trouble coming up with a function that will animate a single column bar inside of my chart when I use a slider going back and forth..left and right.. I have read the animation docs on the Google Charts API documentation, but I am having a hard time understanding what I need to do. 当我使用来回滑动滑块..左右的动画时,我想不出一个功能来为图表中的单个列栏设置动画。我已经阅读了Google Charts API文档中的动画文档,但是我很难理解我需要做什么。

Here is my code so far. 到目前为止,这是我的代码。 Where would I start in figuring out how to animate just one of my bars using a slider I have made in titanium? 我应该从哪里开始,如何使用我在钛合金中制成的滑块为一根杆制作动画? I call the function updateChart() from my app.js file using the evalJS function. 我使用evalJS函数从我的app.js文件中调用了updateChart()函数。 I have verified it works, by doing a console.log when my slider goes back and forth. 我已经验证了它的工作原理,方法是在滑块来回移动时执行console.log。 I just can't seem to wrap my head around it how to apply this to animating a single column bar. 我只是似乎无法解决这个问题,如何将其应用于动画单个栏。 Any thoughts are appreciated. 任何想法表示赞赏。

Set up Google Charts on my html page. 在我的HTML页面上设置Google图表。

    <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);
        }

EDITED CODE: 编辑代码:

    <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 in separate file (app.js) Titanium Platform 单独文件(app.js)中的Slider Hook代码Titanium平台

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 + "');");
});

You need to hook up an event listener for your slider that calls the updateChart function. 您需要为调用updateChart函数的滑块连接事件侦听器。 The updateChart function should get the value from the slider and change the value in the DataTable, then cal the chart's draw method. updateChart函数应从滑块获取值并更改DataTable中的值,然后计算图表的draw方法。 How you hook up an event listener to the slider is going to depend on the library you are using for the slider. 如何将事件侦听器连接到滑块,将取决于您用于滑块的库。 Here's some example code that assumes your slider object has a change method to set a change event handler and a `getValue method to get the value of the slider: 这是一些示例代码,假定您的滑块对象具有用于设置更改事件处理程序的change方法和用于获取滑块值的`getValue方法:

[edit: added in your slider code] [编辑:添加到您的滑块代码中]

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM