Google Chart API 错误“给定轴上的所有系列必须具有相同的数据类型" [英] Google Chart API error "All series on a given axis must be of the same data type"

查看:17
本文介绍了Google Chart API 错误“给定轴上的所有系列必须具有相同的数据类型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 googlechart 的 LineGraph.这个图是用下面的代码创建的

I have LineGraph using googlechart. This graph is created with the following code

function drawChart() {
    var data = google.visualization.arrayToDataTable([ 

      ['Categegories', 'R1' , 'R2' , 'R3' , 'R4' , 'R5' , 'R6' ],

      ['A',          1,     4,       2,    4,       1,  null],

      ['D',          3,  null,    null,    7,    null,     1],

      ['G',          null,  null,    null,       8,    null,  null],

    ]);

    var options = {
      title: 'Graph',
      pointSize: 6,
      vAxis: {minValue:0, maxValue:10,gridlines:{count:6}},
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

现在令人惊讶的是,如果我删除数据行 ['D', 3, null, null, 7, null, 1],

Now surprisingly if I remove a data row ['D', 3, null, null, 7, null, 1],

它产生一个错误,指出给定轴上的所有系列必须具有相同的数据类型

It produces an error saying that All series on a given axis must be of the same data type

我已将代码缩减为一行,但发现 null 值存在问题

I have reduce my code to just one line and I found that there is problem with null values

例如

['Category', 'R1' , 'R2' , 'R3' ],
['A',       2,  1,  1]

它生成图形,而如果我在数据中的任何位置添加 null 值,即在 (2,1,1) 的位置,它不会..

It generates the graph while if I add null value any where in the data i.e in the place of (2,1,1) it does not..

正在等待一些关于设置某种选项以处理 null 值的专家指导......很奇怪,有时空值有效而有时无效...... :(

Waiting for some expert guidance about setting some kind option for handling null values... It is very strange that some time null values works and some time not.. :(

推荐答案

我的问题终于有了解决方案.我希望这会帮助许多像我一样的人.问题基本上是 arrayToDataTable 方法的限制.数据行在所有列中都必须具有正确的类型.由于我们在行中有空值,API 要么假设该列的数据类型为空,要么抛出关于未获得有效数据类型的错误.要解决此问题,您必须切换到标准的 DataTable 构造函数

Finally, I have got a solution to my problem. I hope this will help many others like me. The problem is basically a limitation of arrayToDataTable method. The row of data must have the correct type in all columns. Since we have null values in rows, the API is either assuming that the data type of that column in null or it is throwing an error about not getting a valid data type. To fix this, you will have to switch to the a standard DataTable constructor

var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Health (P)');
data.addColumn('number', 'Health (A)');
data.addColumn('number', 'Gender (P)');
data.addColumn('number', 'Gender (A)');
data.addColumn('number', 'Education (P)');
data.addColumn('number', 'Education (A)');
data.addColumn('number', 'Agriculture (P)');
data.addColumn('number', 'Agriculture (A)');
data.addColumn('number', 'Social protection (P)');
data.addColumn('number', 'Social protection (A)');
data.addColumn('number', 'Environment (P)');
data.addColumn('number', 'Environment (A)');
data.addColumn('number', 'Water/sanitation (P)');
data.addColumn('number', 'Water/sanitation (A)');
data.addRows([

    ['2008',81.06,null,1.32,null,94.68,0,13.41,null,30.36,null,19.78,null,36.87,null],
    ['2009',27.13,null,22.34,null,33.6,null,79.92,null,1.34,null,89.77,0,15.68,null],
    ['2010',104.89,0,14.61,null,33.46,null,30.29,null,22.28,null,107.81,null,1.39,null],
    ['2011',55,0,110.69,null,1.3,null,106.24,0,14.45,null,27.34,null,30.71,null],
    ['2012',29.96,null,27.88,null,44.77,null,133.83,null,1.31,null,105.01,null,17.83,null],
    ['2013',0,null,0,null,0,null,0,null,0,null,0,null,0,null]
]);

复制自以下链接https://groups.google.com/forum/?fromgroups=#!topic/google-visualization-api/2Jafk8PyjV4

或者,您可以使用 arrayToDataTable() 在标题行中指定类型.有关更多信息,请参阅文档 https://developers.google.com/chart/interactive/docs/datatables_dataviews#arraytodatatable

Alternatively, with arrayToDataTable() you can specify the type in the header row. See the docs for more https://developers.google.com/chart/interactive/docs/datatables_dataviews#arraytodatatable

var data = google.visualization.arrayToDataTable([
      ['Categegories', {label: 'R1', type: 'number'} , {label: 'R2', type: 'number'} , {label: 'R3', type: 'number'} , {label: 'R4', type: 'number'} , {label: 'R5', type: 'number'} , {label: 'R6', type: 'number'} ],

      ['A',          1,     4,       2,    4,       1,  null],

      ['D',          3,  null,    null,    7,    null,     1],

      ['G',          null,  null,    null,       8,    null,  null],

    ]);

这篇关于Google Chart API 错误“给定轴上的所有系列必须具有相同的数据类型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆