weixin_33744854 2015-05-19 11:26 采纳率: 0%
浏览 28

Google图表问题-

I got a problem with Google charts and this code. So, this code works perfectly if I put it in head section directly, but if I put it in external file, I receive an error:

Uncaught Error: Container is not defined

And when I inspect that error in console, it points me to this line:

 var chart = new google.visualization.LineChart($( "#curve_chart" ));

So there is error. Do you have any idea way? I tried using function getElementById too. And what is strange, this code works without error if placed in head between script tags. Once I put it in head but in external file, it throws this error!

google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var jsonData = $.ajax({
          url: "https://www.example.com/pay/process.php?action=load_graph",
          dataType:"json",
          async: false
          }).responseText;
      console.log(jsonData);
      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(eval("("+jsonData+")"));
      var options = {
      pointSize: 5,
      displayMode: 'auto', 
      colors: ['#b5e1fe','#ff9666'],
      pointShape: { type: 'circle' },
      legend: { position: 'none' },
      width: 720,
      height: 300,
      };


      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.LineChart($( "#curve_chart" ));
      chart.draw(data, options);
    };

And this is my jsonData, I used code sample from Google:

         {
          cols: [{id: 'A', label: 'NEW A', type: 'string'},
                 {id: 'B', label: 'B-label', type: 'number'},
                 {id: 'C', label: 'C-label', type: 'date'}
          ],
          rows: [{c:[{v: 'a'},
                     {v: 1.0, f: 'One'},
                     {v: new Date(2008, 1, 28, 0, 31, 26), f: '2/28/08 12:31 AM'}
                ]},
                 {c:[{v: 'b'},
                     {v: 2.0, f: 'Two'},
                     {v: new Date(2008, 2, 30, 0, 31, 26), f: '3/30/08 12:31 AM'}
                ]},
                 {c:[{v: 'c'},
                     {v: 3.0, f: 'Three'},
                     {v: new Date(2008, 3, 30, 0, 31, 26), f: '4/30/08 12:31 AM'}
                ]}
          ],
          p: {foo: 'hello', bar: 'world!'}
        }

Thank you very much!

  • 写回答

0条回答 默认 最新

    报告相同问题?