Skip to content

Commit c3a2f79

Browse files
authored
Merge pull request #252 from coderdojo-japan/chart
Charts
2 parents 7e8c980 + 6c08ecf commit c3a2f79

File tree

6 files changed

+101
-13
lines changed

6 files changed

+101
-13
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ gem 'rack-attack'
4242

4343
gem 'google_drive'
4444

45+
gem 'lazy_high_charts'
46+
4547
group :development do
4648
gem 'web-console'
4749
gem 'spring'

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ GEM
125125
haml (>= 4.0.6, < 6.0)
126126
html2haml (>= 1.0.1)
127127
railties (>= 4.0.1)
128+
hash-deep-merge (0.1.1)
128129
highline (1.7.8)
129130
html2haml (2.2.0)
130131
erubis (~> 2.7.0)
@@ -155,6 +156,8 @@ GEM
155156
kramdown (1.15.0)
156157
launchy (2.4.3)
157158
addressable (~> 2.3)
159+
lazy_high_charts (1.5.8)
160+
hash-deep-merge
158161
listen (3.1.5)
159162
rb-fsevent (~> 0.9, >= 0.9.4)
160163
rb-inotify (~> 0.9, >= 0.9.7)
@@ -390,6 +393,7 @@ DEPENDENCIES
390393
jquery-rails
391394
koala
392395
kramdown
396+
lazy_high_charts
393397
listen
394398
minitest-retry
395399
pg

app/assets/javascripts/application.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
//= require scrivito_section_widgets
1717
//= require scrivito
1818
//= require bootstrap-sprockets
19+
//= require highcharts/highcharts
20+
//= require highcharts/highcharts-more
21+
//= require highcharts/highstock
1922
//= require_tree .

app/controllers/static_pages_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ 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
37+
@annual_event_histories_chart = HighChartsBuilder.build_annual_event_histories
38+
@annual_participants_chart = HighChartsBuilder.build_annual_participants
3539
end
3640

3741
def letsencrypt

app/models/high_charts_builder.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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(["#A0D3B5", "#505D6B"])
18+
end
19+
end
20+
21+
def build_annual_event_histories
22+
source = EventHistory.where('evented_at < ?', Time.current.beginning_of_year).group("DATE_TRUNC('year', evented_at)").count
23+
data = annual_chart_data_from(source)
24+
25+
LazyHighCharts::HighChart.new('graph') do |f|
26+
f.title(text: '開催回数の推移')
27+
f.xAxis(categories: data[:years])
28+
f.series(type: 'column', name: '開催回数', yAxis: 0, data: data[:increase_nums])
29+
f.series(type: 'line', name: '累積合計', yAxis: 1, data: data[:cumulative_sums])
30+
f.yAxis [
31+
{ title: { text: '開催回数' } },
32+
{ title: { text: '累積合計' }, opposite: true }
33+
]
34+
f.chart(width: 600)
35+
f.colors(["#F4C34F", "#BD2561"])
36+
end
37+
end
38+
39+
def build_annual_participants
40+
source = EventHistory.where('evented_at < ?', Time.current.beginning_of_year).group("DATE_TRUNC('year', evented_at)").sum(:participants)
41+
data = annual_chart_data_from(source)
42+
43+
LazyHighCharts::HighChart.new('graph') do |f|
44+
f.title(text: '参加者数の推移')
45+
f.xAxis(categories: data[:years])
46+
f.series(type: 'column', name: '参加者数', yAxis: 0, data: data[:increase_nums])
47+
f.series(type: 'line', name: '累積合計', yAxis: 1, data: data[:cumulative_sums])
48+
f.yAxis [
49+
{ title: { text: '参加者数' } },
50+
{ title: { text: '累積合計' }, opposite: true }
51+
]
52+
f.chart(width: 600)
53+
f.colors(["#EF685E", "#35637D"])
54+
end
55+
end
56+
57+
private
58+
59+
def annual_chart_data_from(source)
60+
sorted_list = source.each.with_object({}) {|(k, v), h| h[k.year] = v }.sort
61+
years = sorted_list.map(&:first)
62+
increase_nums = sorted_list.map(&:last)
63+
cumulative_sums = increase_nums.size.times.map {|i| increase_nums[0..i].inject(:+) }
64+
65+
{
66+
years: years,
67+
increase_nums: increase_nums,
68+
cumulative_sums: cumulative_sums
69+
}
70+
end
71+
end
72+
end

app/views/static_pages/stats.html.haml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,6 @@
99
%p 本ページでは全国の CoderDojo の統計情報をまとめています。
1010
CoderDojo の活動状況を把握する際などにご活用ください。
1111

12-
%h3 全国の道場
13-
#accordion.panel-group{"aria-multiselectable" => "true", :role => "tablist"}
14-
- @regions_and_dojos.each_with_index do |(region, dojos), index|
15-
.panel.panel-default
16-
.panel-heading{:id => "heading#{index}", :role => "tab"}
17-
%h4.panel-title
18-
%a{"data-parent" => "#accordion", "data-toggle" => "collapse", :href => "##{"collapse#{index}"}", :role => "button"}
19-
%i.fa.fa-chevron-right{"aria-hidden" => "true"}
20-
= region
21-
\- #{dojos.count} Dojos
22-
.panel-collapse.collapse{:id => "collapse#{index}", :role => "tabpanel"}
23-
.panel-body.grayscale-bg
24-
= render partial: 'dojo', collection: dojos
2512
%h3 開催回数
2613
= @sum_of_events
2714
@@ -60,6 +47,22 @@
6047
- @range.each do |year|
6148
%td= @participants[year]
6249
%td= @participants.values.inject(:+)
50+
= high_chart("annual_dojos", @annual_dojos_chart)
51+
= high_chart("annual_event_histories", @annual_event_histories_chart)
52+
= high_chart("annual_participants", @annual_participants_chart)
53+
%h3 全国の道場
54+
#accordion.panel-group{"aria-multiselectable" => "true", :role => "tablist"}
55+
- @regions_and_dojos.each_with_index do |(region, dojos), index|
56+
.panel.panel-default
57+
.panel-heading{:id => "heading#{index}", :role => "tab"}
58+
%h4.panel-title
59+
%a{"data-parent" => "#accordion", "data-toggle" => "collapse", :href => "##{"collapse#{index}"}", :role => "button"}
60+
%i.fa.fa-chevron-right{"aria-hidden" => "true"}
61+
= region
62+
\- #{dojos.count} Dojos
63+
.panel-collapse.collapse{:id => "collapse#{index}", :role => "tabpanel"}
64+
.panel-body.grayscale-bg
65+
= render partial: 'dojo', collection: dojos
6366

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

0 commit comments

Comments
 (0)