其他分享
首页 > 其他分享> > 给定DataTable时Google Charts draw()方法类型错误

给定DataTable时Google Charts draw()方法类型错误

作者:互联网

我正在尝试使用HTMLJavascript在Google Charts中显示带有范围过滤器的折线图,但是每当我运行draw()函数时,代码就会告诉我为draw()参数使用了错误的数据数据类型(它应该是一个数据表).但是,我使用arrayToDataTable()构造了数据,根据Google Charts API documentation,该数据创建了一个DataTable.这是我的完整代码:

<html>
    <head>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
            //Chart data
            google.charts.load('current',{'packages':['controls']});
            google.charts.setOnLoadCallback(drawDashboard);

            function drawDashboard(){
                var dataSet = google.visualization.arrayToDataTable([
                    [{label:'date',type:'datetime'},{label:'Bytes from network:Bytes to network',type:'number'}],
                    [new Date(2016,01,23,00,00),1.0],
                    [new Date(2016,01,23,01,00),1.0075187969924813],
                    [new Date(2016,01,23,03,00),1.126865671641791],
                    [new Date(2016,01,24,22,00),0.987012987012987],
                    [new Date(2016,01,25,01,00),1.0],
                    [new Date(2016,01,25,02,00),0.9166666666666666],
                    [new Date(2016,01,25,10,00),1.0],
                    [new Date(2016,01,26,12,00),1.0],
                    [new Date(2016,01,27,17,00),0.9864864864864865],
                    [new Date(2016,01,28,22,00),0.03125],
                    [new Date(2016,02,01,22,00),0.97],
                ]);

                var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));

                var dateRangeFilter = new google.visualization.ControlWrapper({'controlType':'ChartRangeFilter','containerId':'filter_div','options': {'filterColumnLabel':'date'}});

                var lineChart = new google.visualization.ChartWrapper({'chartType':'LineChart','containerId':'curve_chart','options':{'title': 'Total Bytes from the Source Network to Total Bytes To the Source Network per Day','curveType': 'function','legend': {'postition':'bottom'}}});

                dashboard.bind(dateRangeFilter,lineChart);

                dashboard.draw(dataSet);
            }
        </script>
    </head>
    <body>
        <!--draw charts using corresponding div tags-->
        <div id="dashboard_div">
            <div id="filter_div"></div>
            <div id="curve_chart" style="width:900px;height:500px"></div>
        </div>
    </body>
</html>

可以看到,我使用google.visualization.arrayToDataTable()创建了一个名为dataSet的变量,该变量返回一个DataTable.然后,在drawDashboard()方法的底部,我调用dashboard.draw(dataSet),它将引发错误.

You called the draw() method with the wrong type of data rather than a DataTable or DataView

我不确定为什么会引发此错误,因为dataSet显然是DataTable类的实例.我曾尝试多次清除缓存和Cookie,但无济于事.

解决方法:

尝试删除对http://www.google.com/jsapi的引用

您只需要loader.js

我还发现在使用loader.js时,除了“控件”外,还需要包含一些软件包

google.charts.load('current', {
    packages: ['controls', 'corechart'],
    callback: drawDashboard
});

function drawDashboard() {
    var dataSet = google.visualization.arrayToDataTable([
        [{label:'date',type:'datetime'},{label:'Bytes from network:Bytes to network',type:'number'}],
        [new Date(2016,01,23,00,00),1.0],
        [new Date(2016,01,23,01,00),1.0075187969924813],
        [new Date(2016,01,23,03,00),1.126865671641791],
        [new Date(2016,01,24,22,00),0.987012987012987],
        [new Date(2016,01,25,01,00),1.0],
        [new Date(2016,01,25,02,00),0.9166666666666666],
        [new Date(2016,01,25,10,00),1.0],
        [new Date(2016,01,26,12,00),1.0],
        [new Date(2016,01,27,17,00),0.9864864864864865],
        [new Date(2016,01,28,22,00),0.03125],
        [new Date(2016,02,01,22,00),0.97],
    ]);

    var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));

    var dateRangeFilter = new google.visualization.ControlWrapper({'controlType':'ChartRangeFilter','containerId':'filter_div','options': {'filterColumnLabel':'date'}});

    var lineChart = new google.visualization.ChartWrapper({'chartType':'LineChart','containerId':'curve_chart','options':{'title': 'Total Bytes from the Source Network to Total Bytes To the Source Network per Day','curveType': 'function','legend': {'postition':'bottom'}}});

    dashboard.bind(dateRangeFilter,lineChart);

    dashboard.draw(dataSet);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="dashboard_div">
    <div id="filter_div"></div>
    <div id="curve_chart" style="width:900px;height:500px"></div>
</div>

标签:datatable,google-api,google-visualization,javascript
来源: https://codeday.me/bug/20191119/2032639.html