admin管理员组

文章数量:1353653

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.

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

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:

(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>

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.

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

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:

(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>
Share Improve this question edited Dec 15, 2013 at 21:56 GEOCHET 21.3k15 gold badges77 silver badges99 bronze badges asked Jul 16, 2012 at 23:23 Harley SugarmanHarley Sugarman 2931 gold badge3 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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!

本文标签: javascriptGoogle Chart Data column(s) for axis 0 cannot be of type stringStack Overflow