Javascript Google 图表:轴 #0 的数据列不能是字符串类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11514015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:06:14  来源:igfitidea点击:

Google Chart: Data column(s) for axis #0 cannot be of type string

javascriptruby-on-railscross-browsergoogle-visualization

提问by Harley Sugarman

I'm having a really weird problem with a Google Chart that I'm displaying on my webpage. When I run the code locally on my Mac, everything works, and the graph displays with no error.

我在我的网页上显示的 Google 图表遇到了一个非常奇怪的问题。当我在 Mac 上本地运行代码时,一切正常,图形显示没有错误。

However, when I run the same app on the AWS server I'm testing with, I get the following error message and no graph:

但是,当我在测试的 AWS 服务器上运行相同的应用程序时,我收到以下错误消息,但没有图表:

Data column(s) for axis #0 cannot be of type string

The app is written in ruby-on-rails, and the relevant code is pasted below:

该应用程序是用ruby-on-rails编写的,相关代码粘贴如下:

(In the page's controller)

(在页面的控制器中)

relevantEntries = getDataFromDB
@graphData = Array.new
@graphData << ["Date", "Value"]
relevantEntries.each do |entry|
  @graphData << [entry.created.strftime(format="%-m/%-d"), entry.value]
end

(In the page's view)

(在页面视图中)

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

  function ChartMaker(chartDiv) {
    // Create the data table.
    var data = google.visualization.arrayToDataTable(<%= @graphData.to_json.html_safe %>);
    // Set chart options
    var options = {/* some options */};
    // Instantiate and draw chart, passing in the options.
    var chart = new google.visualization.LineChart(document.getElementById(chartDiv));
    chart.draw(data, options);
  }

</script>

回答by Harley Sugarman

Never mind - I solved it. The problem was that I had wiped the production database a few days ago, so when I went to get the plot data, it wouldn't fetch anything. This caused the error, since the array would then only look like:

没关系 - 我解决了它。问题是我前几天擦了生产数据库,所以当我去获取绘图数据时,它什么也获取不到。这导致了错误,因为数组只会看起来像:

["Date", "Value"]

Hence the string error.

因此字符串错误。

Sorry!

对不起!