javascript - 谷歌图表 : "Cannot read property ' toLowerCase' of undefined"

标签 javascript google-visualization

我尝试生成谷歌图表。它在图表区域中返回“无法读取未定义的属性'toLowerCase'”,但在控制台中没有错误。

function drawChart(chart_data, chartType, chartOptions){

           console.log(chartOptions);

            var data = new google.visualization.DataTable(chart_data);

            var chart = new google.visualization.ChartWrapper({

                chartType: chartType,
                dataTable: data,
                options: chartOptions,
                containerId: 'chart'

            });

            chart.draw();
        }

console.log 的结果是一个如下所示的字符串:

{"title":"test","vAxis":{"minValue":"0"},"hAxis":{"showTextEvery":"1"},"chartArea":{"width":"70%","height":"90%"},"pointSize":"7","height":"500","intervals":{"lineWidth":2,"barWidth":0.5,"color":"#000000"}} 

如果我使用(复制粘贴)console.log 的结果而不是使用 chartOptions 作为选项,则会显示图表完美。

我在这里缺少什么?

最佳答案

我能想到的唯一解释(考虑到复制粘贴效果很好),是在一个实例中,您发送一个字符串,当您将其复制粘贴回来时,您就会得到一个对象。

enter image description here

因此,如果您查看控制台日志,它应该类似于上图中的第一个日志。如果它看起来像第二个,你需要这样做

chartOptions = JSON.parse(chartOptions);

var chart = new google.visualization.ChartWrapper({
              chartType: chartType,
              dataTable: data,
              options: chartOptions,
              containerId: 'chart'
            });

关于javascript - 谷歌图表 : "Cannot read property ' toLowerCase' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26620723/

相关文章:

javascript - 是否可以在同一网站的多个页面上使用 Google Maps API?

javascript - 带label标签的文件拖拽输入框

javascript - 如何将第二个图表添加到谷歌图表的仪表板中?

javascript - 语法 var names = string.split ("\n"), make\r behind element of string field

javascript - 如何在 $.each 数组中查找下一个索引项

javascript - 如何在 Google Cloud Functions 中获取 HTTP 请求正文大小?

google-visualization - 根据 Google 散点图的值更改点颜色

javascript - Google 图表 HTML 从右到左问题

javascript - 在 Google Charts 直方图中指定列高

php - 使用示例数据但不使用自定义数据的 Google 图表