2016-07-27 3 views
0

私の会社のインターンプロジェクトの線図を作成したいと思います。 yAxisで私は命令を欲しい。 xAxisでは月が必要です(1月〜12月)。 xAxisには12個のラベルがあります。私の問題は、最初の12の注文だけがデータから選択されるということです。これをどうすれば解決できますか?同じ問題は1ヶ月です。私はここに説明するためのリンクを置く。 http://imgur.com/a/VJBvj そしてここでは私のコードです:Chart.jsデータ/ラベルスケール

<canvas id="orderChartCurrentMonth" width="400" height="400"></canvas> 
<script> 
    <%= foo = ((Date.today).beginning_of_month) %> 
    var ctx = document.getElementById("orderChartCurrentMonth"); 
    var orderChartCurrentMonth = new Chart(ctx, { 
    type: 'line', 
    data: { 
     labels: [ 
     "<%=foo %>", "<%=foo + 1.week%>", "<%=foo + 2.week %>","<%=foo + 3.week%>", "<%=foo + 4.week%>" 
     ], //x-Achse 
     datasets: [{ 
     label: 'Graph Orders last week', 
     data: <%= (foo..foo.end_of_month).map { |date| Company.graph_order(date).to_f}.inspect %> //y-Achse 
     }]}, 
    options:{ 
     legend:{ 
     display: false 
     }, 
     scales: { 
     xAxes: [{ 
      ticks: { 
      stepSize: 10 
     }}]}}}) 
</script> 

挨拶

答えて

0

あなたは、月(または週)でグループ化された集計データを提供してクエリを使用する必要があります。あなたのデータベースの日付機能でそれを行うことができますが、頻繁に必要とする場合に備えてプロセスを合理化するGroupdate gemを使用することをお勧めします。あなたは何かのようにすることができます。

# this should probably be done in a controller action 
<% @data = Order.where(date: 1.years.ago.beginning_of_month..Time.now).group_by_month(:date).count %> 


# get an array of monthly labels in js file 
... 
labels: <%= @data.map{ |month, order_count| month } %> 
... 

# get the values for those months 
... 
data: <%= @data.map{ |month, order_count| order_count } %> 
... 

もちろん、この例では月間の注文数を示しています。他の値が必要な場合は、クエリを調整する必要があります。さらに、Groupdateはこのデータをグループ化するための多くのオプションを提供しています。ドキュメントを必ずお読みください:https://github.com/ankane/groupdate