”的
我有一个web服务返回JSON数据,我想将它转换成DataTable。我得到的错误信息是“无法读取属性'的数据表'的未定义”
从这篇文章Google Visualization - TypeError: Cannot read property 'DataTable' of undefined [Chrome specific]据我所知,这可能是一个脚本加载问题,但我怎样才能控制谷歌图表脚本加载聚合物的方式?
我的自定义事件的聚合物低于
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="../../bower_components/google-chart/google-chart.html">
<dom-module id="dash-chart">
\t <template>
\t \t <style type="text/css">
\t
\t \t .google-chart-style {
\t \t \t width: 100%;
\t \t \t height: 90%;
\t \t }
\t \t .google-chart-class {
\t \t \t width:100%;
\t \t \t height:90%;
\t \t }
\t </style>
\t \t <iron-ajax
\t \t \t auto
\t \t \t url={{url}}
\t \t \t handle-as="json"
\t \t \t on-response="handleResponse">
\t \t </iron-ajax>
\t \t
\t \t <google-chart
\t \t \t \t class="google-chart-class"
\t \t \t \t id="myChart"
\t \t \t \t type={{type}}
\t \t \t \t options={{options}}>
\t \t </google-chart>
\t </template>
\t <script>
\t \t Polymer({
\t \t \t is: "dash-chart",
\t \t \t properties : {
\t \t \t \t url: String,
\t \t \t \t type: String,
\t \t \t \t options: Object,
\t \t \t \t data: Array,
\t \t \t \t xaxis: Array,
\t \t \t \t yaxis: Array,
\t \t \t \t fields: Array
\t \t \t },
\t \t \t
\t \t \t convertResultsToDatatable: function(response) {
\t \t \t \t console.log(response);
\t \t \t \t //DataTable object
\t \t \t \t var objDataTable = new google.visualization.DataTable();
\t \t \t \t objDataTable.addColumn(this.xaxis[1], this.xaxis[2]);
\t \t \t \t objDataTable.addColumn(this.yaxis[1], this.yaxis[2]);
\t \t \t \t var afterConversionToDatatable = [[]];
\t \t \t \t for(var index in response) {
\t \t \t \t var objVal = response[index];
\t \t \t \t var arr = []; \t \t \t \t
\t \t \t \t for(var key in objVal) {
\t \t \t \t \t if(key === this.xaxis[0]) {
\t \t \t \t \t \t if(this.xaxis[1] === 'number') {
\t \t \t \t \t \t \t arr.push(parseInt(objVal[key]));
\t \t \t \t \t \t } else {
\t \t \t \t \t \t \t arr.push(objVal[key]);
\t \t \t \t \t \t }
\t \t \t \t \t } else if (key === this.yaxis[0]) {
\t \t \t \t \t \t if(this.yaxis[1] === 'number') {
\t \t \t \t \t \t \t arr.push(parseInt(objVal[key]));
\t \t \t \t \t \t } else {
\t \t \t \t \t \t \t arr.push(objVal[key]);
\t \t \t \t \t \t }
\t \t \t \t \t }
\t \t \t \t }
\t \t \t \t
\t \t \t \t afterConversionToDatatable[index] = arr;
\t \t \t \t }
\t \t \t \t console.log(afterConversionToDatatable);
\t \t \t \t objDataTable.addRows(afterConversionToDatatable);
\t \t \t \t return objDataTable;
\t \t \t },
\t \t \t handleResponse: function(data) {
\t \t \t \t var rootKey;
\t \t \t \t var jsonResult = data.detail.response;
\t \t \t \t for(var prop in jsonResult) {
\t \t \t \t \t rootKey = prop;
\t \t \t \t }
\t \t \t \t //Done so that we do not need to hard code the root object (which changes based with every roxie query)
\t \t \t \t var resultNode = jsonResult[rootKey].Results.result_1.Row;
\t \t \t \t this.data = this.convertResultsToDatatable(resultNode);
\t
\t \t \t }
\t \t });
\t </script>
</dom-module>
@WhiteHat Polymer的google-chart元素在我们尝试使用它时内部加载loader.js文件。 – Dinesh