score:1

Accepted answer

ok, the problem was that there was no data passed into the coffeescript.

i managed getting this done using the gem 'gon'. here is what i did:

my app.js.coffee file looks like this:

jquery ->
months = $.map( gon.sales, ( val, i ) -> 
            val.month
          );
amts = $.map( gon.sales, ( val, i ) -> 
            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 my sales_controller.rb i've added the following:

def index
  @sales = sale.all
  gon.sales = @sales
end

in my application.html.haml layout file:

= include_gon

and ofcourse in the gemfile:

gem 'gon'

finally in index.html.haml:

%canvas#canvas{:height => "400", :width => "600"}

and now the chart in dynamically populated with data from my sales table.

score:0

you need to fetch sum of amount by grouping on created_at's month.

orders = order.select("date_trunc('month', created_at) as month, sum(amount) as sum_of_amount").group("month")
months = orders.collect(&:month)
amts = orders.collect(&:sum_of_amount)

now you just need to pass months in labels: and amts in data:.

good luck!


Related Query

More Query from same tag