Ruby - Generate array of comma separated arrays -
i'm trying create google chart using googlevisualr.
this input works:
data_table.add_rows([ ['04/14', 1], ['04/15', 2], ['04/16', 3], ['04/17', 4], ['04/18', 5], ['04/19', 1], ['04/20', 12], ['04/21', 13], ['04/24', 14], ['04/14', 15], ['04/24', 16], ['04/22', 17], ['04/14', 18], ['04/4', 19], ])
i using:
product.find(:all, :order => "created_at asc").each |p| data_table.add_rows([ [p.created_at.strftime("%m/%d"), p.rating] ])
which returns:
01/13 2 01/20 3 02/22 2 03/14 2 03/19 2 04/14 1 04/15 2 04/17 2 05/14 2 05/14 2 05/14 2 05/14 2...
how can format array match googlevisualr requires:
[ [data, value], [date, value]...]
no need use loop, use map
:
rows = product.all.order("created_at asc").map |p| [p.created_at.strftime("%m/%d"), p.rating] end data_table.add_rows(rows)
Comments
Post a Comment