Commit 0ef114e0 authored by Jean-Philippe Lang's avatar Jean-Philippe Lang

added simple svn statistics graphs, rendered using SVG::Graph

git-svn-id: http://redmine.rubyforge.org/svn/trunk@380 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 887f1143
......@@ -15,10 +15,15 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'SVG/Graph/Bar'
require 'SVG/Graph/BarHorizontal'
class RepositoriesController < ApplicationController
layout 'base'
before_filter :find_project, :authorize
before_filter :find_project
before_filter :authorize, :except => [:stats, :graph]
before_filter :check_project_privacy, :only => [:stats, :graph]
def show
# get entries for the browse frame
@entries = @repository.scm.entries('')
......@@ -64,6 +69,25 @@ class RepositoriesController < ApplicationController
show_error and return unless @diff
end
def stats
end
def graph
data = nil
case params[:graph]
when "commits_per_month"
data = graph_commits_per_month(@repository)
when "commits_per_author"
data = graph_commits_per_author(@repository)
end
if data
headers["Content-Type"] = "image/svg+xml"
send_data(data, :type => "image/svg+xml", :disposition => "inline")
else
render_404
end
end
private
def find_project
@project = Project.find(params[:id])
......@@ -80,4 +104,85 @@ private
flash.now[:notice] = l(:notice_scm_error)
render :nothing => true, :layout => true
end
def graph_commits_per_month(repository)
@date_to = Date.today
@date_from = @date_to << 12
commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
commits_by_month = [0] * 12
commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
changes_by_day = repository.changes.count(:all, :group => :commit_date)
changes_by_month = [0] * 12
changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
fields = []
month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
graph = SVG::Graph::Bar.new(
:height => 300,
:width => 500,
:fields => fields.reverse,
:stack => :side,
:scale_integers => true,
:step_x_labels => 2,
:show_data_values => false,
:graph_title => l(:label_commits_per_month),
:show_graph_title => true
)
graph.add_data(
:data => commits_by_month[0..11].reverse,
:title => l(:label_revision_plural)
)
graph.add_data(
:data => changes_by_month[0..11].reverse,
:title => l(:label_change_plural)
)
graph.burn
end
def graph_commits_per_author(repository)
commits_by_author = repository.changesets.count(:all, :group => :committer)
commits_by_author.sort! {|x, y| x.last <=> y.last}
fields = commits_by_author.collect {|r| r.first}
data = commits_by_author.collect {|r| r.last}
fields = fields + [""]*(10 - fields.length) if fields.length<10
data = data + [0]*(10 - data.length) if data.length<10
graph = SVG::Graph::BarHorizontal.new(
:height => 300,
:width => 500,
:fields => fields,
:stack => :side,
:scale_integers => true,
:show_data_values => false,
:rotate_y_labels => false,
:graph_title => l(:label_commits_per_author),
:show_graph_title => true
)
graph.add_data(
:data => data,
:title => l(:label_revision_plural)
)
graph.burn
end
end
class Date
def months_ago(date = Date.today)
(date.year - self.year)*12 + (date.month - self.month)
end
def weeks_ago(date = Date.today)
(date.year - self.year)*52 + (date.cweek - self.cweek)
end
end
\ No newline at end of file
......@@ -19,7 +19,12 @@ class Changeset < ActiveRecord::Base
belongs_to :repository
has_many :changes, :dependent => :delete_all
validates_presence_of :repository_id, :revision, :committed_on
validates_presence_of :repository_id, :revision, :committed_on, :commit_date
validates_numericality_of :revision, :only_integer => true
validates_uniqueness_of :revision, :scope => :repository_id
def committed_on=(date)
self.commit_date = date
super
end
end
......@@ -18,6 +18,7 @@
class Repository < ActiveRecord::Base
belongs_to :project
has_many :changesets, :dependent => :destroy, :order => 'revision DESC'
has_many :changes, :through => :changesets
has_one :latest_changeset, :class_name => 'Changeset', :foreign_key => :repository_id, :order => 'revision DESC'
attr_protected :root_url
......
<div class="contextual">
<%= link_to l(:label_statistics), {:action => 'stats', :id => @project}, :class => 'icon icon-stats' %>
</div>
<h2><%= l(:label_repository) %></h2>
<h3><%= l(:label_browse) %></h3>
......
<h2><%= l(:label_statistics) %></h2>
<table width="100%">
<tr><td>
<%= tag("embed", :width => 500, :height => 300, :type => "image/svg+xml", :src => url_for(:controller => 'repositories', :action => 'graph', :id => @project, :graph => "commits_per_month")) %>
</td><td>
<%= tag("embed", :width => 500, :height => 300, :type => "image/svg+xml", :src => url_for(:controller => 'repositories', :action => 'graph', :id => @project, :graph => "commits_per_author")) %>
</td></tr>
</table>
<br />
<p><%= link_to l(:button_back), :action => 'show', :id => @project %></p>
\ No newline at end of file
class AddChangesetCommitDate < ActiveRecord::Migration
def self.up
add_column :changesets, :commit_date, :date, :null => false
Changeset.update_all "commit_date = committed_on"
end
def self.down
remove_column :changesets, :commit_date
end
end
......@@ -344,6 +344,10 @@ label_spent_time: Spent time
label_f_hour: %.2f hour
label_f_hour_plural: %.2f hours
label_time_tracking: Time tracking
label_change_plural: Changes
label_statistics: Statistics
label_commits_per_month: Commits per month
label_commits_per_author: Commits per author
button_login: Einloggen
button_submit: OK
......
......@@ -344,6 +344,10 @@ label_spent_time: Spent time
label_f_hour: %.2f hour
label_f_hour_plural: %.2f hours
label_time_tracking: Time tracking
label_change_plural: Changes
label_statistics: Statistics
label_commits_per_month: Commits per month
label_commits_per_author: Commits per author
button_login: Login
button_submit: Submit
......
......@@ -344,6 +344,10 @@ label_spent_time: Spent time
label_f_hour: %.2f hour
label_f_hour_plural: %.2f hours
label_time_tracking: Time tracking
label_change_plural: Changes
label_statistics: Statistics
label_commits_per_month: Commits per month
label_commits_per_author: Commits per author
button_login: Conexión
button_submit: Someter
......
......@@ -344,6 +344,10 @@ label_spent_time: Temps passé
label_f_hour: %.2f heure
label_f_hour_plural: %.2f heures
label_time_tracking: Suivi du temps
label_change_plural: Changements
label_statistics: Statistiques
label_commits_per_month: Commits par mois
label_commits_per_author: Commits par auteur
button_login: Connexion
button_submit: Soumettre
......
......@@ -344,6 +344,10 @@ label_spent_time: Spent time
label_f_hour: %.2f hour
label_f_hour_plural: %.2f hours
label_time_tracking: Time tracking
label_change_plural: Changes
label_statistics: Statistics
label_commits_per_month: Commits per month
label_commits_per_author: Commits per author
button_login: Login
button_submit: Invia
......
......@@ -345,6 +345,10 @@ label_spent_time: Spent time
label_f_hour: %.2f hour
label_f_hour_plural: %.2f hours
label_time_tracking: Time tracking
label_change_plural: Changes
label_statistics: Statistics
label_commits_per_month: Commits per month
label_commits_per_author: Commits per author
button_login: ログイン
button_submit: 変更
......
This diff is collapsed.
require 'rexml/document'
require 'SVG/Graph/Graph'
require 'SVG/Graph/BarBase'
module SVG
module Graph
# === Create presentation quality SVG bar graphs easily
#
# = Synopsis
#
# require 'SVG/Graph/Bar'
#
# fields = %w(Jan Feb Mar);
# data_sales_02 = [12, 45, 21]
#
# graph = SVG::Graph::Bar.new(
# :height => 500,
# :width => 300,
# :fields => fields
# )
#
# graph.add_data(
# :data => data_sales_02,
# :title => 'Sales 2002'
# )
#
# print "Content-type: image/svg+xml\r\n\r\n"
# print graph.burn
#
# = Description
#
# This object aims to allow you to easily create high quality
# SVG[http://www.w3c.org/tr/svg bar graphs. You can either use the default
# style sheet or supply your own. Either way there are many options which
# can be configured to give you control over how the graph is generated -
# with or without a key, data elements at each point, title, subtitle etc.
#
# = Notes
#
# The default stylesheet handles upto 12 data sets, if you
# use more you must create your own stylesheet and add the
# additional settings for the extra data sets. You will know
# if you go over 12 data sets as they will have no style and
# be in black.
#
# = Examples
#
# * http://germane-software.com/repositories/public/SVG/test/test.rb
#
# = See also
#
# * SVG::Graph::Graph
# * SVG::Graph::BarHorizontal
# * SVG::Graph::Line
# * SVG::Graph::Pie
# * SVG::Graph::Plot
# * SVG::Graph::TimeSeries
class Bar < BarBase
include REXML
# See Graph::initialize and BarBase::set_defaults
def set_defaults
super
self.top_align = self.top_font = 1
end
protected
def get_x_labels
@config[:fields]
end
def get_y_labels
maxvalue = max_value
minvalue = min_value
range = maxvalue - minvalue
top_pad = range == 0 ? 10 : range / 20.0
scale_range = (maxvalue + top_pad) - minvalue
scale_division = scale_divisions || (scale_range / 10.0)
if scale_integers
scale_division = scale_division < 1 ? 1 : scale_division.round
end
rv = []
maxvalue = maxvalue%scale_division == 0 ?
maxvalue : maxvalue + scale_division
minvalue.step( maxvalue, scale_division ) {|v| rv << v}
return rv
end
def x_label_offset( width )
width / 2.0
end
def draw_data
fieldwidth = field_width
maxvalue = max_value
minvalue = min_value
fieldheight = (@graph_height.to_f - font_size*2*top_font) /
(get_y_labels.max - get_y_labels.min)
bargap = bar_gap ? (fieldwidth < 10 ? fieldwidth / 2 : 10) : 0
subbar_width = fieldwidth - bargap
subbar_width /= @data.length if stack == :side
x_mod = (@graph_width-bargap)/2 - (stack==:side ? subbar_width/2 : 0)
# Y1
p2 = @graph_height
# to X2
field_count = 0
@config[:fields].each_index { |i|
dataset_count = 0
for dataset in @data
# X1
p1 = (fieldwidth * field_count)
# to Y2
p3 = @graph_height - ((dataset[:data][i] - minvalue) * fieldheight)
p1 += subbar_width * dataset_count if stack == :side
@graph.add_element( "path", {
"class" => "fill#{dataset_count+1}",
"d" => "M#{p1} #{p2} V#{p3} h#{subbar_width} V#{p2} Z"
})
make_datapoint_text(
p1 + subbar_width/2.0,
p3 - 6,
dataset[:data][i].to_s)
dataset_count += 1
end
field_count += 1
}
end
end
end
end
require 'rexml/document'
require 'SVG/Graph/Graph'
module SVG
module Graph
# = Synopsis
#
# A superclass for bar-style graphs. Do not attempt to instantiate
# directly; use one of the subclasses instead.
#
# = Author
#
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
#
# Copyright 2004 Sean E. Russell
# This software is available under the Ruby license[LICENSE.txt]
#
class BarBase < SVG::Graph::Graph
# Ensures that :fields are provided in the configuration.
def initialize config
raise "fields was not supplied or is empty" unless config[:fields] &&
config[:fields].kind_of?(Array) &&
config[:fields].length > 0
super
end
# In addition to the defaults set in Graph::initialize, sets
# [bar_gap] true
# [stack] :overlap
def set_defaults
init_with( :bar_gap => true, :stack => :overlap )
end
# Whether to have a gap between the bars or not, default
# is true, set to false if you don't want gaps.
attr_accessor :bar_gap
# How to stack data sets. :overlap overlaps bars with
# transparent colors, :top stacks bars on top of one another,
# :side stacks the bars side-by-side. Defaults to :overlap.
attr_accessor :stack
protected
def max_value
return @data.collect{|x| x[:data].max}.max
end
def min_value
min = 0
if (min_scale_value.nil? == false) then
min = min_scale_value
else
min = @data.collect{|x| x[:data].min}.min
end
return min
end
def get_css
return <<EOL
/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
.key1,.fill1{
fill: #ff0000;
fill-opacity: 0.5;
stroke: none;
stroke-width: 0.5px;
}
.key2,.fill2{
fill: #0000ff;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key3,.fill3{
fill: #00ff00;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key4,.fill4{
fill: #ffcc00;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key5,.fill5{
fill: #00ccff;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key6,.fill6{
fill: #ff00ff;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key7,.fill7{
fill: #00ffff;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key8,.fill8{
fill: #ffff00;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key9,.fill9{
fill: #cc6666;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key10,.fill10{
fill: #663399;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key11,.fill11{
fill: #339900;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
.key12,.fill12{
fill: #9966FF;
fill-opacity: 0.5;
stroke: none;
stroke-width: 1px;
}
EOL
end
end
end
end
require 'rexml/document'
require 'SVG/Graph/BarBase'
module SVG
module Graph
# === Create presentation quality SVG horitonzal bar graphs easily
#
# = Synopsis
#
# require 'SVG/Graph/BarHorizontal'
#
# fields = %w(Jan Feb Mar)
# data_sales_02 = [12, 45, 21]
#
# graph = SVG::Graph::BarHorizontal.new({
# :height => 500,
# :width => 300,
# :fields => fields,
# })
#
# graph.add_data({
# :data => data_sales_02,
# :title => 'Sales 2002',
# })
#
# print "Content-type: image/svg+xml\r\n\r\n"
# print graph.burn
#
# = Description
#
# This object aims to allow you to easily create high quality
# SVG horitonzal bar graphs. You can either use the default style sheet
# or supply your own. Either way there are many options which can
# be configured to give you control over how the graph is
# generated - with or without a key, data elements at each point,
# title, subtitle etc.
#
# = Examples
#
# * http://germane-software.com/repositories/public/SVG/test/test.rb
#
# = See also
#
# * SVG::Graph::Graph
# * SVG::Graph::Bar
# * SVG::Graph::Line
# * SVG::Graph::Pie
# * SVG::Graph::Plot
# * SVG::Graph::TimeSeries
#
# == Author
#
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
#
# Copyright 2004 Sean E. Russell
# This software is available under the Ruby license[LICENSE.txt]
#
class BarHorizontal < BarBase
# In addition to the defaults set in BarBase::set_defaults, sets
# [rotate_y_labels] true
# [show_x_guidelines] true
# [show_y_guidelines] false
def set_defaults
super
init_with(
:rotate_y_labels => true,
:show_x_guidelines => true,
:show_y_guidelines => false
)
self.right_align = self.right_font = 1
end
protected
def get_x_labels
maxvalue = max_value
minvalue = min_value
range = maxvalue - minvalue
top_pad = range == 0 ? 10 : range / 20.0
scale_range = (maxvalue + top_pad) - minvalue
scale_division = scale_divisions || (scale_range / 10.0)
if scale_integers
scale_division = scale_division < 1 ? 1 : scale_division.round
end
rv = []
maxvalue = maxvalue%scale_division == 0 ?
maxvalue : maxvalue + scale_division
minvalue.step( maxvalue, scale_division ) {|v| rv << v}
return rv
end
def get_y_labels
@config[:fields]
end
def y_label_offset( height )
height / -2.0
end
def draw_data
minvalue = min_value
fieldheight = field_height
fieldwidth = (@graph_width.to_f - font_size*2*right_font ) /
(get_x_labels.max - get_x_labels.min )
bargap = bar_gap ? (fieldheight < 10 ? fieldheight / 2 : 10) : 0
subbar_height = fieldheight - bargap
subbar_height /= @data.length if stack == :side
field_count = 1
y_mod = (subbar_height / 2) + (font_size / 2)
@config[:fields].each_index { |i|
dataset_count = 0
for dataset in @data
y = @graph_height - (fieldheight * field_count)
y += (subbar_height * dataset_count) if stack == :side
x = (dataset[:data][i] - minvalue) * fieldwidth
@graph.add_element( "path", {
"d" => "M0 #{y} H#{x} v#{subbar_height} H0 Z",
"class" => "fill#{dataset_count+1}"
})
make_datapoint_text(
x+5, y+y_mod, dataset[:data][i], "text-anchor: start; "
)
dataset_count += 1
end
field_count += 1
}
end
end
end
end
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
require 'SVG/Graph/Plot'
require 'parsedate'
module SVG
module Graph
# === For creating SVG plots of scalar temporal data
#
# = Synopsis
#
# require 'SVG/Graph/TimeSeriess'
#
# # Data sets are x,y pairs
# data1 = ["6/17/72", 11, "1/11/72", 7, "4/13/04 17:31", 11,
# "9/11/01", 9, "9/1/85", 2, "9/1/88", 1, "1/15/95", 13]
# data2 = ["8/1/73", 18, "3/1/77", 15, "10/1/98", 4,
# "5/1/02", 14, "3/1/95", 6, "8/1/91", 12, "12/1/87", 6,
# "5/1/84", 17, "10/1/80", 12]
#
# graph = SVG::Graph::TimeSeries.new( {
# :width => 640,
# :height => 480,
# :graph_title => title,
# :show_graph_title => true,
# :no_css => true,
# :key => true,
# :scale_x_integers => true,
# :scale_y_integers => true,
# :min_x_value => 0,
# :min_y_value => 0,
# :show_data_labels => true,
# :show_x_guidelines => true,
# :show_x_title => true,
# :x_title => "Time",
# :show_y_title => true,
# :y_title => "Ice Cream Cones",
# :y_title_text_direction => :bt,
# :stagger_x_labels => true,
# :x_label_format => "%m/%d/%y",
# })
#
# graph.add_data({
# :data => projection
# :title => 'Projected',
# })
#
# graph.add_data({
# :data => actual,
# :title => 'Actual',
# })
#