database - Rails chart.js data coffee script -
i new rails. want populate chart.js chart data database table. managed set chart static data.
i need replace static data data table sales. sales.month should represent x-axis , sales.amount should represent y-axis value.
my app.js.coffee looks this:
sales = sale.select(month, sum(amount) sum_of_amount) months = sales.collect(month) amts = sales.collect(sum_of_amount) jquery -> data = { labels : months, datasets : [ { fillcolor : "rgba(151,187,205,0.5)", strokecolor : "rgba(151,187,205,1)", pointcolor : "rgba(151,187,205,1)", pointstrokecolor : "#fff", data : amts } ] }
my index.html.haml looks , chart displayed static vaulues.
%canvas#canvas{:height => "400", :width => "600"}
how proceed here?
thanks.
ok, problem there no data passed coffeescript.
i managed getting done using gem 'gon'. here did:
my app.js.coffee file looks this:
jquery -> months = $.map( gon.sales, ( val, ) -> val.month ); amts = $.map( gon.sales, ( val, ) -> val.amount ); data = { labels : months, datasets : [ { fillcolor : "rgba(151,187,205,0.5)", strokecolor : "rgba(151,187,205,1)", pointcolor : "rgba(151,187,205,1)", pointstrokecolor : "#fff", data : amts } ] }
in sales_controller.rb i've added following:
def index @sales = sale.all gon.sales = @sales end
in application.html.haml layout file:
= include_gon
and ofcourse in gemfile:
gem 'gon'
finally in index.html.haml:
%canvas#canvas{:height => "400", :width => "600"}
and chart in dynamically populated data sales table.