Google图表无法绘制/显示

时间:2015-08-23 04:37:39

标签: javascript jquery html parse-platform google-visualization

我正在开发Parse.com和jQuery单页应用程序。 我希望能够绘制历史数据图表,但我不会得到它。

我不确定是怎么回事。我感谢任何帮助。

<html>
  <head>
    <title>Demo</title>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="http://www.parsecdn.com/js/parse-1.5.0.min.js"></script>    
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
        <script src="https://www.google.com/jsapi?autoload={
            'modules':[{
              'name':'visualization',
              'version':'1',
              'packages':['corechart']
            }]
          }"></script>
    <style>
    body { font-family: Helvetica, Arial, sans-serif; }

    h1 { font-size: 30px; margin: 0; }
    p { margin: 40px 0; }
    em { font-family: monospace; }
    a { color: #5298fc; text-decoration: none; }
    </style>
  </head>
  <body>
    <ul>
        <li><a href="#CurrentStatus">Current Status</a></li>
        <li><a href="#Map">Map</a></li>
        <li><a href="#Details">Details</a></li>
    </ul>

    <div id = "CurrentStatus" class = "page">
        <div class = "content">
            <h1>Current Status</h1>
        </div>
        <div> <b> Temperature: </b> </div>
        <div id="temp_div" style="width:25%; height:5%">
            <h5> Temperature </h5> </div>
    </div>

    <div id = "Map" class = "page">
        <div id="map_d" style="width:100%; height:70%">
        <div class = "content">
            <h1>Map</h1>
        </div>
    </div>

    <div id = "Details" class = "page">
        <div id="piechart" style="width:100%; height:70%"></div>
        <div class = "content">
            <h1>Details</h1>
        </div>
    </div>


    <script>
        Parse.initialize("key", "key");
        function showPage(pageId){
            $(".page").hide();
            $(pageId).show();
            $(pageId).trigger("show:query");
        }

        $(window).on("hashchange", function(event){
            showPage(location.hash);
            });

        $("#CurrentStatus").on("show:query", function(event){
            setInterval(init, 30000);           
            });
        $("#Map").on("show:query", function(event){
                setTimeout(map, 5);
        });
        $("#Details").on("show:query", function(event){
                console.log("precallback - details") //this appears on console
                google.setOnLoadCallback(drawChart);
        });

        location.hash = "";
        location.hash = "#CurrentStatus"

        function init() { 
                var Parent = Parse.Object.extend("Parent");
                var query = new Parse.Query(Parent);
                query.descending("updatedAt");
                query.first({
                    success: function(object) {
                        temp = document.createTextNode(object.get('Temp')),
                        dtemp = document.getElementById('temp_div');
                        dtemp.textContent = temp.data;
                        },
                    error: function(error) {
                        alert("Error: " + error.code + " " + error.message);
                        }
                    }); 
                }

        function map() { 
                var Parent = Parse.Object.extend("Parent");
                var query = new Parse.Query(Parent);
                query.descending("updatedAt");
                query.first({
                    success: function(object) {
                        var mapCanvas = document.getElementById('map_d');
                        var mapOptions = {
                            center: new google.maps.LatLng(object.get('Latitude'), (object.get('Longitude'))),
                            zoom: 15,
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                            }
                        var map = new google.maps.Map(mapCanvas, mapOptions);
                        var lati = Number(object.get('Latitude'));
                        var longi = Number(object.get('Longitude'));
                        var myLatLng = {lat:lati, lng:longi};
                        var marker = new google.maps.Marker({
                                                    position: myLatLng,
                                                    map: map,
                                                    title: 'Location!'
                                                    });
                        },
                    error: function(error) {
                        alert("Error: " + error.code + " " + error.message);
                        }
                    }); 
                }
        function drawChart() {
        console.log("draw"); //this is not showing on console
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work',     11],
          ['Eat',      2],
          ['Commute',  2],
          ['Watch TV', 2],
          ['Sleep',    7]
        ]);

        var options = {
          title: 'My Daily Activities'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }

    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

有两个问题:

  1. </div>的结束#map_d缺失
  2. 以下部分不会产生预期效果:

    $("#Details").on("show:query", function(event){
            console.log("precallback - details") //this appears on console
            google.setOnLoadCallback(drawChart);
    });
    
  3. 提供给google.setOnLoadCallback的回调将在加载所有库后立即执行一次。当您自动加载可视化库时,当您致电setOnloadCallback时已经发生这种情况,drawChart永远不会被执行。

    不同的方法:

    不要自动加载可视化,只加载JSAPI:

    <!--
    
    <script src="https://www.google.com/jsapi?autoload={
            'modules':[{
              'name':'visualization',
              'version':'1',
              'packages':['corechart']
            }]
          }"></script>
    
    -->
    <script src="https://www.google.com/jsapi"></script>
    

    要加载可视化库并使用$.one()

    绘制图表
        /*
        $("#Details").on("show:query", function(event){
                console.log("precallback - details") //this appears on console
                google.setOnLoadCallback(drawChart);
        });
        */
    
        $("#Details").one("show:query", function(event){
            google.load("visualization", 
                        "1", 
                        { callback : drawChart,
                          packages : ['corechart']});
        });