0

當我在同一頁面上加載Google Analytics Embed API和Google Charts時,似乎存在衝突,更具體地說,當我使用Embed API繪製圖表時也是如此。Google Analytics Embed API和Google Charts之間的衝突

爲了使它更有趣,只有當圖表類型不同時纔會出現問題,例如, Embed API:TABLE and Charts:LINE

嵌入API圖表首先被加載並按預期工作。 圖表線圖被加載第二和返回此錯誤:

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

我有一種感覺,嵌入API圖表覆蓋此功能:

google.charts.load('current', { 
    packages: ['corechart', 'line', 'table'] 
}); 

UPDATE

回答這個問題:Google Charts draw() method wrong type when given DataTable建議刪除對http://www.google.com/jsapi的引用,但隨後Google Embed API圖表停止工作,因爲window.google.load不再被定義,而是有window.google.charts.load這是谷歌圖表需要,但似乎不工作嵌入API圖表。

+0

的可能的複製[谷歌圖表繪製()方法錯誤類型時給出的數據表(HTTP:/ /stackoverflow.com/questions/35636075/google-charts-draw-method-wrong-type-when-given-datatable) – DaImTo

回答

1

我遇到了同樣的問題,並修復了腳本加載衝突。 解決方案是隻包含較新的圖表加載器。

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

然後,裝載谷歌圖表API之前,運行額外的代碼suggested by Daniel LaLiberte

//Handles differences in load functions 
google.load = google.load || google.charts.load; 
google.setOnLoadCallback = google.setOnLoadCallback || google.charts.setOnLoadCallback; 

//Loads the google analytics API 
(function (w, d, s, g, js, fs) { 
      //if ($('#googleCache').length > 0) return; 
      g = w.gapi || (w.gapi = {}); 
      g.analytics = { 
       q: [], 
       ready: function (f) { 
        this.q.push(f); 
       } 
      }; 
      js = d.createElement(s); 
      fs = d.getElementsByTagName(s)[0]; 
      js.src = 'https://apis.google.com/js/platform.js'; 
      js.id = "googleCache"; 
      fs.parentNode.insertBefore(js, fs); 
      js.onload = function() { 
       g.load('analytics'); 
      }; 
}(window, document, 'script')); 

//Load the charts you want 
google.charts.load('current', { packages: ['corechart', 'table', 'line', 'geochart'] });