From 9acd9cbfe9692f9aed44bf6d45a8b96059388b09 Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Sat, 20 Oct 2012 10:18:48 +0200 Subject: [PATCH] repo charts refactored --- lib/git_stats/stats_view/charts/chart.rb | 48 ++++++++++++++----- .../stats_view/charts/repo_charts.rb | 48 ++++++++----------- 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/lib/git_stats/stats_view/charts/chart.rb b/lib/git_stats/stats_view/charts/chart.rb index 36db680de..f45d5bc7e 100644 --- a/lib/git_stats/stats_view/charts/chart.rb +++ b/lib/git_stats/stats_view/charts/chart.rb @@ -12,6 +12,38 @@ module GitStats yield self if block_given? end + def column_hash_chart(params) + common_params(params) + type "column" + x_categories params[:data].keys + series(name: params[:title], data: params[:data].values) + end + + def day_chart(params) + common_params(params) + type "datetime" + series( + type: "area", + name: "commits", + pointInterval: 1.day * 1000, + pointStart: params[:start_day].to_i * 1000, + data: params[:data] + ) + end + + def default_legend + legend( + layout: 'vertical', + backgroundColor: '#FFFFFF', + align: 'left', + verticalAlign: 'top', + x: 100, + y: 70, + floating: true, + shadow: true + ) + end + def type(type) @chart.chart(type: type) end @@ -28,18 +60,12 @@ module GitStats @chart.title(text: title) end - def default_legend - legend( - layout: 'vertical', - backgroundColor: '#FFFFFF', - align: 'left', - verticalAlign: 'top', - x: 100, - y: 70, - floating: true, - shadow: true - ) + private + def common_params(params) + title params[:title] + y_text params[:y_text] end + end end end diff --git a/lib/git_stats/stats_view/charts/repo_charts.rb b/lib/git_stats/stats_view/charts/repo_charts.rb index 282c034ed..59dc9a7ec 100644 --- a/lib/git_stats/stats_view/charts/repo_charts.rb +++ b/lib/git_stats/stats_view/charts/repo_charts.rb @@ -8,50 +8,42 @@ module GitStats def files_by_extension Chart.new do |f| - f.type "column" - f.title "files by extension" - f.x_categories @repo.files_by_extension_count.keys - f.y_text 'Commits' - f.series(name: "commits", data: @repo.files_by_extension_count.values) + f.column_hash_chart( + data: @repo.files_by_extension_count, + title: "files_by_extension", + y_text: "files" + ) end end def lines_by_extension Chart.new do |f| - f.type "column" - f.title "lines by extension" - f.x_categories @repo.lines_by_extension.keys - f.y_text 'Commits' - f.series(name: "commits", data: @repo.lines_by_extension.values) + f.column_hash_chart( + data: @repo.lines_by_extension, + title: "lines_by_extension", + y_text: "lines" + ) end end def files_by_date Chart.new do |f| - f.title "Files" - f.type "datetime" - f.y_text 'Commits' - f.series( - type: "area", - name: "commits", - pointInterval: 1.day * 1000, - pointStart: @repo.commits.first.date.to_i * 1000, - data: @repo.files_count_each_day + f.day_chart( + data: @repo.files_count_each_day, + start_day: @repo.commits.first.date, + title: "files_by_date", + y_text: "files" ) end end def lines_by_date Chart.new do |f| - f.title "Files" - f.type "datetime" - f.y_text 'Commits' - f.series( - type: "area", - name: "commits", - pointInterval: 1.day * 1000, - pointStart: @repo.commits.first.date.to_i * 1000, - data: @repo.lines_count_each_day + f.day_chart( + data: @repo.lines_count_each_day, + start_day: @repo.commits.first.date, + title: "lines_by_date", + y_text: "lines" ) end end