highstock date chart

This commit is contained in:
Tomasz Gieniusz 2012-10-21 13:19:37 +02:00
parent 99921c44ea
commit 4a590ddd40
13 changed files with 75 additions and 73 deletions

View file

@ -2,3 +2,5 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in git_stats.gemspec # Specify your gem's dependencies in git_stats.gemspec
gemspec gemspec
gem 'lazy_high_charts', git: 'git://github.com/tomgi/lazy_high_charts.git', branch: 'deep_merge'

View file

@ -15,4 +15,6 @@ en:
files_by_date: Files by date files_by_date: Files by date
lines_by_date: Lines by date lines_by_date: Lines by date
files_by_extension: Files by extension files_by_extension: Files by extension
lines_by_extension: Lines by extension lines_by_extension: Lines by extension
hour_of_day: Hour of day
hour: Hour

View file

@ -22,7 +22,7 @@ Gem::Specification.new do |gem|
gem.add_dependency('tilt') gem.add_dependency('tilt')
gem.add_dependency('haml') gem.add_dependency('haml')
gem.add_dependency('launchy') gem.add_dependency('launchy')
gem.add_dependency('lazy_high_charts') #gem.add_dependency('lazy_high_charts')
gem.add_dependency('i18n') gem.add_dependency('i18n')
gem.add_development_dependency('rake') gem.add_development_dependency('rake')

View file

@ -6,7 +6,10 @@ require 'action_pack'
require 'action_view' require 'action_view'
require 'fileutils' require 'fileutils'
require 'tilt' require 'tilt'
require 'lazy_high_charts' #require 'lazy_high_charts'
require 'launchy' require 'launchy'
require 'i18n' require 'i18n'
require 'bundler'
Bundler.require(:default)
Dir['lib/**/*.rb'].each { |r| require File.expand_path(r) } Dir['lib/**/*.rb'].each { |r| require File.expand_path(r) }

View file

@ -45,28 +45,16 @@ module GitStats
Hash[authors.map { |author| [author, author.lines_deleted] }] Hash[authors.map { |author| [author, author.lines_deleted] }]
end end
def files_count_each_day def files_count_by_date
@files_count_each_day ||= commits_period_range.map { |day| @files_count_each_day ||= Hash[commits.map { |commit|
files_count_at day [commit.date, commit.files_count]
} }]
end end
def files_count_at(day) def lines_count_by_date
last_commit_at(day).try(:files_count) || 0 @lines_count_each_day ||= Hash[commits.map { |commit|
end [commit.date, commit.lines_count]
}]
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 }
end end
def last_commit def last_commit
@ -138,11 +126,6 @@ module GitStats
command_observers.each { |o| o.call(command, result) } command_observers.each { |o| o.call(command, result) }
end end
def commits_period_range
period = commits_period.map(&:midnight)
period.first.upto (period.last + 1.day)
end
end end
end end
end end

View file

@ -11,6 +11,7 @@ module GitStats
f.simple_column_chart( f.simple_column_chart(
title: :commits_by_hour.t, title: :commits_by_hour.t,
y_text: :commits.t, y_text: :commits.t,
x_text: :hour.t,
data_x: (0..23), data_x: (0..23),
data_y: @activity.by_hour.to_key_indexed_array data_y: @activity.by_hour.to_key_indexed_array
) )

View file

@ -26,21 +26,19 @@ module GitStats
def column_hash_chart(params) def column_hash_chart(params)
simple_column_chart(params.merge( simple_column_chart(params.merge(
data_x: params[:data].keys, data_x: params[:data].keys,
data_y: params[:data].values data_y: params[:data].values
) )
) )
end end
def day_chart(params) def date_chart(params)
common_params(params) common_options(params)
xAxis(type: "datetime") rangeSelector(selected: 1)
series( series(
type: "area", name: params[:title],
name: "commits", type: "spline",
pointInterval: 1.day * 1000, data: params[:data].map {|date, value| [date.to_i * 1000, value]}
pointStart: params[:start_day].to_i * 1000,
data: params[:data]
) )
end end
@ -57,30 +55,42 @@ module GitStats
) )
end end
def no_legend
legend(
enabled: false
)
end
def type(type) def type(type)
@chart.chart(type: type) @chart.chart!(type: type)
end end
def x_categories(categories) def x_categories(categories)
@chart.xAxis(categories: categories) @chart.xAxis!(categories: categories)
end
def x_text(text)
@chart.xAxis!(title: {text: text})
end end
def y_text(text) def y_text(text)
@chart.yAxis(min: 0, title: {text: text}) @chart.yAxis!(title: {text: text})
end end
def title(title) def title(title)
@chart.title(text: title) @chart.title!(text: title)
end end
private private
def common_params(params) def common_options(params)
title params[:title] no_legend
title ""
y_text params[:y_text] y_text params[:y_text]
x_text params[:x_text]
end end
def column_chart(params) def column_chart(params)
common_params(params) common_options(params)
type "column" type "column"
x_categories params[:data_x] x_categories params[:data_x]
end end

View file

@ -28,9 +28,8 @@ module GitStats
def files_by_date def files_by_date
Chart.new do |f| Chart.new do |f|
f.day_chart( f.date_chart(
data: @repo.files_count_each_day, data: @repo.files_count_by_date,
start_day: @repo.commits.first.date,
title: :files_by_date.t, title: :files_by_date.t,
y_text: :files.t y_text: :files.t
) )
@ -39,9 +38,8 @@ module GitStats
def lines_by_date def lines_by_date
Chart.new do |f| Chart.new do |f|
f.day_chart( f.date_chart(
data: @repo.lines_count_each_day, data: @repo.lines_count_by_date,
start_day: @repo.commits.first.date,
title: :lines_by_date.t, title: :lines_by_date.t,
y_text: :lines.t y_text: :lines.t
) )

View file

@ -2,6 +2,18 @@ require 'spec_helper'
describe GitStats::GitData::Repo do describe GitStats::GitData::Repo do
let(:repo) { build(:test_repo, last_commit_sha: '872955c') } 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 it 'should gather all authors' do
repo.authors.should =~ [ repo.authors.should =~ [
@ -31,11 +43,13 @@ describe GitStats::GitData::Repo do
end end
it 'should count files by date' do 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 end
it 'should count lines by date' do 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 end
it 'should count all lines in repo' do it 'should count all lines in repo' do

View file

@ -7,13 +7,6 @@ shared_examples_for "column_chart" do
end end
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 describe GitStats::StatsView::Charts::RepoCharts do
let(:charts) { GitStats::StatsView::Charts::All.new(repo) } let(:charts) { GitStats::StatsView::Charts::All.new(repo) }
@ -44,28 +37,22 @@ describe GitStats::StatsView::Charts::RepoCharts do
end end
context 'files_by_date chart' do 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 } let(:chart) { charts.files_by_date }
it_behaves_like "datetime_chart"
it 'should have 1 data series with files_by_date' do it 'should have 1 data series with files_by_date' do
chart.should have(1).data chart.should have(1).data
chart.data[0][:data].should == [10, 15, 12, 20] chart.data[0][:data].should == [[1000, 10], [2000, 15], [3000, 12], [5000, 20]]
chart.data[0][:pointStart].should == 5000
end end
end end
context 'lines_by_date chart' do 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 } let(:chart) { charts.lines_by_date }
it_behaves_like "datetime_chart"
it 'should have 1 data series with lines_by_date' do it 'should have 1 data series with lines_by_date' do
chart.should have(1).data chart.should have(1).data
chart.data[0][:data].should == [100, 150, 120, 200] chart.data[0][:data].should == [[1000, 100], [2000, 150], [3000, 120], [5000, 200]]
chart.data[0][:pointStart].should == 6000
end end
end end

View file

@ -1 +1,3 @@
.page-header
%h1.pagination-centered= :hour_of_day.t
= high_chart("activity_by_hour", charts.activity_by_hour) = high_chart("activity_by_hour", charts.activity_by_hour)

View file

@ -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) = high_chart("files_by_extension", charts.files_by_extension)

View file

@ -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) = high_chart("lines_by_extension", charts.lines_by_extension)