From 4a590ddd400605226d29e8b8be29333b435a9b06 Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Sun, 21 Oct 2012 13:19:37 +0200 Subject: [PATCH] highstock date chart --- Gemfile | 2 + config/locales/en.yml | 4 +- git_stats.gemspec | 2 +- lib/git_stats.rb | 5 +- lib/git_stats/git_data/repo.rb | 33 ++++--------- .../stats_view/charts/activity_charts.rb | 1 + lib/git_stats/stats_view/charts/chart.rb | 46 +++++++++++-------- .../stats_view/charts/repo_charts.rb | 10 ++-- spec/integration/repo_spec.rb | 18 +++++++- spec/stats_view/charts/repo_charts_spec.rb | 21 ++------- templates/activity.haml | 2 + templates/files.haml | 2 +- templates/lines.haml | 2 +- 13 files changed, 75 insertions(+), 73 deletions(-) diff --git a/Gemfile b/Gemfile index e5df66504..385af1ab0 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,5 @@ source 'https://rubygems.org' # Specify your gem's dependencies in git_stats.gemspec gemspec + +gem 'lazy_high_charts', git: 'git://github.com/tomgi/lazy_high_charts.git', branch: 'deep_merge' \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 89178e50b..98cef7e45 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -15,4 +15,6 @@ en: files_by_date: Files by date lines_by_date: Lines by date files_by_extension: Files by extension - lines_by_extension: Lines by extension \ No newline at end of file + lines_by_extension: Lines by extension + hour_of_day: Hour of day + hour: Hour \ No newline at end of file diff --git a/git_stats.gemspec b/git_stats.gemspec index 039a31a62..637f8f0fd 100644 --- a/git_stats.gemspec +++ b/git_stats.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |gem| gem.add_dependency('tilt') gem.add_dependency('haml') gem.add_dependency('launchy') - gem.add_dependency('lazy_high_charts') + #gem.add_dependency('lazy_high_charts') gem.add_dependency('i18n') gem.add_development_dependency('rake') diff --git a/lib/git_stats.rb b/lib/git_stats.rb index c93ddf383..c5bc56a48 100644 --- a/lib/git_stats.rb +++ b/lib/git_stats.rb @@ -6,7 +6,10 @@ require 'action_pack' require 'action_view' require 'fileutils' require 'tilt' -require 'lazy_high_charts' +#require 'lazy_high_charts' require 'launchy' require 'i18n' + +require 'bundler' +Bundler.require(:default) Dir['lib/**/*.rb'].each { |r| require File.expand_path(r) } \ No newline at end of file diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index c800f6b21..4dd94a238 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -45,28 +45,16 @@ module GitStats Hash[authors.map { |author| [author, author.lines_deleted] }] end - def files_count_each_day - @files_count_each_day ||= commits_period_range.map { |day| - files_count_at day - } + def files_count_by_date + @files_count_each_day ||= Hash[commits.map { |commit| + [commit.date, commit.files_count] + }] end - def files_count_at(day) - last_commit_at(day).try(:files_count) || 0 - end - - def lines_count_each_day - @lines_count_each_day ||= commits_period_range.map { |day| - lines_count_at day - } - end - - def lines_count_at(day) - last_commit_at(day).try(:lines_count) || 0 - end - - def last_commit_at(day) - commits.reverse.find { |c| c.date < day } + def lines_count_by_date + @lines_count_each_day ||= Hash[commits.map { |commit| + [commit.date, commit.lines_count] + }] end def last_commit @@ -138,11 +126,6 @@ module GitStats command_observers.each { |o| o.call(command, result) } end - def commits_period_range - period = commits_period.map(&:midnight) - period.first.upto (period.last + 1.day) - end - end end end \ No newline at end of file diff --git a/lib/git_stats/stats_view/charts/activity_charts.rb b/lib/git_stats/stats_view/charts/activity_charts.rb index 447a66c82..d98d1762e 100644 --- a/lib/git_stats/stats_view/charts/activity_charts.rb +++ b/lib/git_stats/stats_view/charts/activity_charts.rb @@ -11,6 +11,7 @@ module GitStats f.simple_column_chart( title: :commits_by_hour.t, y_text: :commits.t, + x_text: :hour.t, data_x: (0..23), data_y: @activity.by_hour.to_key_indexed_array ) diff --git a/lib/git_stats/stats_view/charts/chart.rb b/lib/git_stats/stats_view/charts/chart.rb index 36c158fc8..01d286ae9 100644 --- a/lib/git_stats/stats_view/charts/chart.rb +++ b/lib/git_stats/stats_view/charts/chart.rb @@ -26,21 +26,19 @@ module GitStats def column_hash_chart(params) simple_column_chart(params.merge( - data_x: params[:data].keys, - data_y: params[:data].values - ) + data_x: params[:data].keys, + data_y: params[:data].values + ) ) end - def day_chart(params) - common_params(params) - xAxis(type: "datetime") + def date_chart(params) + common_options(params) + rangeSelector(selected: 1) series( - type: "area", - name: "commits", - pointInterval: 1.day * 1000, - pointStart: params[:start_day].to_i * 1000, - data: params[:data] + name: params[:title], + type: "spline", + data: params[:data].map {|date, value| [date.to_i * 1000, value]} ) end @@ -57,30 +55,42 @@ module GitStats ) end + def no_legend + legend( + enabled: false + ) + end + def type(type) - @chart.chart(type: type) + @chart.chart!(type: type) end def x_categories(categories) - @chart.xAxis(categories: categories) + @chart.xAxis!(categories: categories) + end + + def x_text(text) + @chart.xAxis!(title: {text: text}) end def y_text(text) - @chart.yAxis(min: 0, title: {text: text}) + @chart.yAxis!(title: {text: text}) end def title(title) - @chart.title(text: title) + @chart.title!(text: title) end private - def common_params(params) - title params[:title] + def common_options(params) + no_legend + title "" y_text params[:y_text] + x_text params[:x_text] end def column_chart(params) - common_params(params) + common_options(params) type "column" x_categories params[:data_x] end diff --git a/lib/git_stats/stats_view/charts/repo_charts.rb b/lib/git_stats/stats_view/charts/repo_charts.rb index 625f043b5..95ddcabf2 100644 --- a/lib/git_stats/stats_view/charts/repo_charts.rb +++ b/lib/git_stats/stats_view/charts/repo_charts.rb @@ -28,9 +28,8 @@ module GitStats def files_by_date Chart.new do |f| - f.day_chart( - data: @repo.files_count_each_day, - start_day: @repo.commits.first.date, + f.date_chart( + data: @repo.files_count_by_date, title: :files_by_date.t, y_text: :files.t ) @@ -39,9 +38,8 @@ module GitStats def lines_by_date Chart.new do |f| - f.day_chart( - data: @repo.lines_count_each_day, - start_day: @repo.commits.first.date, + f.date_chart( + data: @repo.lines_count_by_date, title: :lines_by_date.t, y_text: :lines.t ) diff --git a/spec/integration/repo_spec.rb b/spec/integration/repo_spec.rb index c61935363..39534bc21 100644 --- a/spec/integration/repo_spec.rb +++ b/spec/integration/repo_spec.rb @@ -2,6 +2,18 @@ require 'spec_helper' describe GitStats::GitData::Repo do let(:repo) { build(:test_repo, last_commit_sha: '872955c') } + let(:commit_dates) { [ + DateTime.parse('2012-10-19 10:44:34 +0200'), + DateTime.parse('2012-10-19 10:46:10 +0200'), + DateTime.parse('2012-10-19 10:46:56 +0200'), + DateTime.parse('2012-10-19 10:47:35 +0200'), + DateTime.parse('2012-10-20 12:49:02 +0200'), + DateTime.parse('2012-10-21 12:49:02 +0200'), + DateTime.parse('2012-10-21 12:54:02 +0200'), + DateTime.parse('2012-10-21 13:20:00 +0200'), + DateTime.parse('2012-10-24 15:49:02 +0200'), + DateTime.parse('2012-10-26 17:05:25 +0200'), + ] } it 'should gather all authors' do repo.authors.should =~ [ @@ -31,11 +43,13 @@ describe GitStats::GitData::Repo do end it 'should count files by date' do - repo.files_count_each_day.should == [0, 3, 3, 5, 5, 5, 6, 6, 6] + repo.files_count_by_date.keys.should == commit_dates + repo.files_count_by_date.values.should == [1, 2, 2, 3, 3, 4, 5, 5, 6, 6] end it 'should count lines by date' do - repo.lines_count_each_day.should == [0, 11, 11, 1014, 1014, 1014, 1114, 1114, 1114] + repo.lines_count_by_date.keys.should == commit_dates + repo.files_count_by_date.values.should == [1, 2, 2, 3, 3, 4, 5, 5, 6, 6] end it 'should count all lines in repo' do diff --git a/spec/stats_view/charts/repo_charts_spec.rb b/spec/stats_view/charts/repo_charts_spec.rb index 584dc7ced..1404a21cb 100644 --- a/spec/stats_view/charts/repo_charts_spec.rb +++ b/spec/stats_view/charts/repo_charts_spec.rb @@ -7,13 +7,6 @@ shared_examples_for "column_chart" do end end -shared_examples_for "datetime_chart" do - it 'should be a datetime chart' do - chart.should be_a GitStats::StatsView::Charts::Chart - chart.options[:xAxis][:type].should == "datetime" - end -end - describe GitStats::StatsView::Charts::RepoCharts do let(:charts) { GitStats::StatsView::Charts::All.new(repo) } @@ -44,28 +37,22 @@ describe GitStats::StatsView::Charts::RepoCharts do end context 'files_by_date chart' do - let(:repo) { double(commits: [double(date: 5)], files_count_each_day: [10, 15, 12, 20]) } + let(:repo) { double(commits: [double(date: 5)], files_count_by_date: {1 => 10, 2 => 15, 3 => 12, 5 => 20}) } let(:chart) { charts.files_by_date } - it_behaves_like "datetime_chart" - it 'should have 1 data series with files_by_date' do chart.should have(1).data - chart.data[0][:data].should == [10, 15, 12, 20] - chart.data[0][:pointStart].should == 5000 + chart.data[0][:data].should == [[1000, 10], [2000, 15], [3000, 12], [5000, 20]] end end context 'lines_by_date chart' do - let(:repo) { double(commits: [double(date: 6)], lines_count_each_day: [100, 150, 120, 200]) } + let(:repo) { double(commits: [double(date: 6)], lines_count_by_date: {1 => 100, 2 => 150, 3 => 120, 5 => 200}) } let(:chart) { charts.lines_by_date } - it_behaves_like "datetime_chart" - it 'should have 1 data series with lines_by_date' do chart.should have(1).data - chart.data[0][:data].should == [100, 150, 120, 200] - chart.data[0][:pointStart].should == 6000 + chart.data[0][:data].should == [[1000, 100], [2000, 150], [3000, 120], [5000, 200]] end end diff --git a/templates/activity.haml b/templates/activity.haml index 4c6c58c50..a30742195 100644 --- a/templates/activity.haml +++ b/templates/activity.haml @@ -1 +1,3 @@ +.page-header + %h1.pagination-centered= :hour_of_day.t = high_chart("activity_by_hour", charts.activity_by_hour) \ No newline at end of file diff --git a/templates/files.haml b/templates/files.haml index cec18a46a..e4711fa02 100644 --- a/templates/files.haml +++ b/templates/files.haml @@ -1,2 +1,2 @@ -= high_chart("files_by_date", charts.files_by_date) += high_stock("files_by_date", charts.files_by_date) = high_chart("files_by_extension", charts.files_by_extension) \ No newline at end of file diff --git a/templates/lines.haml b/templates/lines.haml index 29103a853..68950d444 100644 --- a/templates/lines.haml +++ b/templates/lines.haml @@ -1,2 +1,2 @@ -= high_chart("lines_by_date", charts.lines_by_date) += high_stock("lines_by_date", charts.lines_by_date) = high_chart("lines_by_extension", charts.lines_by_extension) \ No newline at end of file