Skip to content

Commit 8fa12bf

Browse files
committed
Chart of dojo number
1 parent 04a50a2 commit 8fa12bf

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

app/controllers/static_pages_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def stats
3232
EventHistory.where(evented_at:
3333
Time.zone.local(year).beginning_of_year..Time.zone.local(year).end_of_year).sum(:participants)
3434
end
35+
36+
@annual_dojos_chart = HighChartsBuilder.build_annual_dojos
3537
end
3638

3739
def letsencrypt

app/models/high_charts_builder.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class HighChartsBuilder
2+
class << self
3+
def build_annual_dojos
4+
source = Dojo.where('created_at < ?', Time.current.beginning_of_year).group("DATE_TRUNC('year', created_at)").count
5+
data = annual_chart_data_from(source)
6+
7+
LazyHighCharts::HighChart.new('graph') do |f|
8+
f.title(text: '全国の道場数の推移')
9+
f.xAxis(categories: data[:years])
10+
f.series(type: 'column', name: '増加数', yAxis: 0, data: data[:increase_nums])
11+
f.series(type: 'line', name: '累積合計', yAxis: 1, data: data[:cumulative_sums])
12+
f.yAxis [
13+
{ title: { text: '増加数' } },
14+
{ title: { text: '累積合計' }, opposite: true }
15+
]
16+
f.chart(width: 600)
17+
f.colors(["#8085e9", "#f7a35c"])
18+
end
19+
end
20+
21+
private
22+
23+
def annual_chart_data_from(source)
24+
sorted_list = source.each.with_object({}) {|(k, v), h| h[k.year] = v }.sort
25+
years = sorted_list.map(&:first)
26+
increase_nums = sorted_list.map(&:last)
27+
cumulative_sums = increase_nums.size.times.map {|i| increase_nums[0..i].inject(:+) }
28+
29+
{
30+
years: years,
31+
increase_nums: increase_nums,
32+
cumulative_sums: cumulative_sums
33+
}
34+
end
35+
end
36+
end

app/views/static_pages/stats.html.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
- @range.each do |year|
6161
%td= @participants[year]
6262
%td= @participants.values.inject(:+)
63+
= high_chart("annual_dojos", @annual_dojos_chart)
6364

6465
%h3 関連リンク
6566
%ul{:style => "list-style: none; margin-left: -40px;"}

0 commit comments

Comments
 (0)