Skip to content

Commit 2505fcf

Browse files
committed
refactor: 道場数グラフを新規開設数表示に対応
HighChartsBuilderを更新して、道場数の「開設数」(新規開設数)を 正しく表示するように変更。 Changes: - annual_dojos_chart_data_from: 新形式のデータに対応 - active_dojosとnew_dojosを含むハッシュを処理 - 新規開設数を「開設数」として表示 - 後方互換性のため旧形式もサポート - ラベルを「増加数」→「開設数」に変更(日本語) - 英語版は「Change」→「New Dojos」に変更 これにより、グラフが道場の実際の新規開設数を表示し、 閉鎖による負の値が表示されなくなる。
1 parent 646d318 commit 2505fcf

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

app/models/high_charts_builder.rb

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ def global_options
99
end
1010

1111
def build_annual_dojos(source, lang = 'ja')
12-
data = annual_chart_data_from(source)
12+
data = annual_dojos_chart_data_from(source)
1313
title_text = lang == 'en' ? 'Number of Dojos' : '道場数の推移'
1414

1515
LazyHighCharts::HighChart.new('graph') do |f|
1616
f.title(text: title_text)
1717
f.xAxis(categories: data[:years])
18-
f.series(type: 'column', name: lang == 'en' ? 'New' : '増加数', yAxis: 0, data: data[:increase_nums])
18+
f.series(type: 'column', name: lang == 'en' ? 'New Dojos' : '開設数', yAxis: 0, data: data[:increase_nums])
1919
f.series(type: 'line', name: lang == 'en' ? 'Total' : '累積合計', yAxis: 1, data: data[:cumulative_sums])
2020
f.yAxis [
21-
{ title: { text: lang == 'en' ? 'New' : '増加数' }, tickInterval: 15, max: 75 },
21+
{ title: { text: lang == 'en' ? 'New Dojos' : '開設数' }, tickInterval: 15, max: 75 },
2222
{ title: { text: lang == 'en' ? 'Total' : '累積合計' }, tickInterval: 50, max: 250, opposite: true }
2323
]
2424
f.chart(width: HIGH_CHARTS_WIDTH, alignTicks: false)
@@ -71,14 +71,44 @@ def annual_chart_data_from(source)
7171
years = source_array.map(&:first)
7272
counts = source_array.map(&:last)
7373

74-
# 増加数を計算(前年との差分)
75-
increase_nums = counts.each_with_index.map do |count, i|
76-
i == 0 ? count : count - counts[i - 1]
77-
end
74+
# 年間の値として扱う(イベント回数や参加者数用)
75+
increase_nums = counts
7876

79-
# annual_dojos_with_historical_dataからの値は既にその時点での総数
80-
# (累積値として扱う)
81-
cumulative_sums = counts
77+
# 累積合計を計算
78+
cumulative_sums = counts.size.times.map {|i| counts[0..i].sum }
79+
80+
{
81+
years: years,
82+
increase_nums: increase_nums,
83+
cumulative_sums: cumulative_sums
84+
}
85+
end
86+
87+
# 道場数の推移用の特別なデータ処理
88+
# 新規開設数と累積数を表示
89+
def annual_dojos_chart_data_from(source)
90+
# sourceが新しい形式(active_dojosとnew_dojosを含む)の場合
91+
if source.is_a?(Hash) && source.key?(:active_dojos) && source.key?(:new_dojos)
92+
active_array = source[:active_dojos].to_a
93+
new_array = source[:new_dojos].to_a
94+
95+
years = active_array.map(&:first)
96+
cumulative_sums = active_array.map(&:last)
97+
increase_nums = new_array.map(&:last) # 新規開設数を使用
98+
else
99+
# 後方互換性のため、古い形式もサポート
100+
source_array = source.is_a?(Hash) ? source.to_a : source
101+
102+
years = source_array.map(&:first)
103+
counts = source_array.map(&:last)
104+
105+
# 増減数を計算(前年との差分)- 後方互換性のため
106+
increase_nums = counts.each_with_index.map do |count, i|
107+
i == 0 ? count : count - counts[i - 1]
108+
end
109+
110+
cumulative_sums = counts
111+
end
82112

83113
{
84114
years: years,

0 commit comments

Comments
 (0)