Pull datasets from DB and manipulate into separate arrays

Posted by dresdin on Stack Overflow See other posts from Stack Overflow or by dresdin
Published on 2013-06-30T08:28:11Z Indexed on 2013/06/30 10:21 UTC
Read the original article Hit count: 271

My DB has four fields, date:date, morning:integer, afternoon:integer, evening:integer.

Google charts needs the data split into one array per dataset (example below). What's the best way to do this?

  ['Day',     'Morning', 'Afternoon', 'Evening'],
  ['06/27/13',  1000,       400           234],
  ['06/28/13',  1170,       460           275],
  ['06/29/13',  660,        1120          377],
  ['06/30/13',  1030,       540           934]

I've tried this without much luck:

var data = google.visualization.arrayToDataTable([
  <% @todo.each do |t| %>
  ['Day',                                   'Morning',        'Afternoon',        'Evening'],
  [<%= t.date.strftime("%m/%d/%y") %>,  <%= t.morning %>, <%= t.afternoon %>, <%= t.evening %>]
  <% end %>
]);

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about arrays