From edba1f692b5a2648392718834a5cb8fb244b7ca6 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 26 Aug 2007 07:55:57 +0000 Subject: [PATCH] Gantt chart can now be exported to a graphic file (png). This functionality is only available if RMagick is present. git-svn-id: http://redmine.rubyforge.org/svn/trunk@666 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/projects_controller.rb | 9 +- app/helpers/projects_helper.rb | 150 +++++++++++++++++++++++++ app/views/projects/gantt.rhtml | 6 +- lib/redmine.rb | 6 + public/stylesheets/application.css | 1 + 5 files changed, 168 insertions(+), 4 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 5ee848ba9..86248e498 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -38,6 +38,7 @@ class ProjectsController < ApplicationController include QueriesHelper helper :repositories include RepositoriesHelper + include ProjectsHelper def index list @@ -614,10 +615,14 @@ class ProjectsController < ApplicationController @events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to]) @events.sort! {|x,y| x.start_date <=> y.start_date } - if params[:output]=='pdf' + if params[:format]=='pdf' @options_for_rfpdf ||= {} - @options_for_rfpdf[:file_name] = "gantt.pdf" + @options_for_rfpdf[:file_name] = "#{@project.identifier}-gantt.pdf" render :template => "projects/gantt.rfpdf", :layout => false + elsif params[:format]=='png' && respond_to?('gantt_image') + image = gantt_image(@events, @date_from, @months, @zoom) + image.format = 'PNG' + send_data(image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png") else render :template => "projects/gantt.rhtml" end diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 74805ba53..abf2bcf86 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -25,4 +25,154 @@ module ProjectsHelper :anchor => version.name }, options end + + # Generates a gantt image + # Only defined if RMagick is avalaible + def gantt_image(events, date_from, months, zoom) + date_to = (date_from >> months)-1 + show_weeks = zoom > 1 + show_days = zoom > 2 + + subject_width = 320 + header_heigth = 18 + # width of one day in pixels + zoom = zoom*2 + g_width = (date_to - date_from + 1)*zoom + g_height = 20 * events.length + 20 + headers_heigth = (show_weeks ? 2*header_heigth : header_heigth) + height = g_height + headers_heigth + + imgl = Magick::ImageList.new + imgl.new_image(subject_width+g_width+1, height) + gc = Magick::Draw.new + + # Subjects + top = headers_heigth + 20 + gc.fill('black') + gc.stroke('transparent') + gc.stroke_width(1) + events.each do |i| + gc.text(4, top + 2, (i.is_a?(Issue) ? i.subject : i.name)) + top = top + 20 + end + + # Months headers + month_f = date_from + left = subject_width + months.times do + width = ((month_f >> 1) - month_f) * zoom + gc.fill('white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left, 0, left + width, height) + gc.fill('black') + gc.stroke('transparent') + gc.stroke_width(1) + gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}") + left = left + width + month_f = month_f >> 1 + end + + # Weeks headers + if show_weeks + left = subject_width + height = header_heigth + if date_from.cwday == 1 + # date_from is monday + week_f = date_from + else + # find next monday after date_from + week_f = date_from + (7 - date_from.cwday + 1) + width = (7 - date_from.cwday + 1) * zoom + gc.fill('white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1) + left = left + width + end + while week_f <= date_to + width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom + gc.fill('white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1) + gc.fill('black') + gc.stroke('transparent') + gc.stroke_width(1) + gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s) + left = left + width + week_f = week_f+7 + end + end + + # Days details (week-end in grey) + if show_days + left = subject_width + height = g_height + header_heigth - 1 + wday = date_from.cwday + (date_to - date_from + 1).to_i.times do + width = zoom + gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1) + left = left + width + wday = wday + 1 + wday = 1 if wday > 7 + end + end + + # border + gc.fill('transparent') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(0, 0, subject_width+g_width, headers_heigth) + gc.stroke('black') + gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1) + + # content + top = headers_heigth + 20 + gc.stroke('transparent') + events.each do |i| + if i.is_a?(Issue) + i_start_date = (i.start_date >= date_from ? i.start_date : date_from ) + i_end_date = (i.due_date <= date_to ? i.due_date : date_to ) + i_done_date = i.start_date + ((i.due_date - i.start_date+1)*i.done_ratio/100).floor + i_done_date = (i_done_date <= date_from ? date_from : i_done_date ) + i_done_date = (i_done_date >= date_to ? date_to : i_done_date ) + i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today + + i_left = subject_width + ((i_start_date - date_from)*zoom).floor + i_width = ((i_end_date - i_start_date + 1)*zoom).floor # total width of the issue + d_width = ((i_done_date - i_start_date)*zoom).floor # done width + l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor : 0 # delay width + + gc.fill('grey') + gc.rectangle(i_left, top, i_left + i_width, top - 6) + gc.fill('red') + gc.rectangle(i_left, top, i_left + l_width, top - 6) if l_width > 0 + gc.fill('blue') + gc.rectangle(i_left, top, i_left + d_width, top - 6) if d_width > 0 + gc.fill('black') + gc.text(i_left + i_width + 5,top + 1, "#{i.status.name} #{i.done_ratio}%") + else + i_left = subject_width + ((i.start_date - date_from)*zoom).floor + gc.fill('green') + gc.rectangle(i_left, top, i_left + 6, top - 6) + gc.fill('black') + gc.text(i_left + 11, top + 1, i.name) + end + top = top + 20 + end + + # today red line + if Date.today >= @date_from and Date.today <= @date_to + gc.stroke('red') + x = (Date.today-@date_from+1)*zoom + subject_width + gc.line(x, headers_heigth, x, headers_heigth + g_height-1) + end + + gc.draw(imgl) + imgl + end if Object.const_defined?(:Magick) end diff --git a/app/views/projects/gantt.rhtml b/app/views/projects/gantt.rhtml index c46ef9f5f..1de8242ea 100644 --- a/app/views/projects/gantt.rhtml +++ b/app/views/projects/gantt.rhtml @@ -22,10 +22,10 @@ g_height = [(20 * @events.length + 6)+150, 206].max t_height = g_height + headers_height %> -<% cache(:year => @year_from, :month => @month_from, :months => @months, :zoom => @zoom, :tracker_ids => @selected_tracker_ids, :subprojects => params[:with_subprojects], :lang => current_language) do %>
<%= l(:label_export_to) %> -<%= link_to 'PDF', {:zoom => @zoom, :year => @year_from, :month => @month_from, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects], :output => 'pdf'}, :class => 'icon icon-pdf' %> +<%= link_to 'PDF', {:zoom => @zoom, :year => @year_from, :month => @month_from, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects], :format => 'pdf'}, :class => 'icon icon-pdf' %> +<%= link_to 'PNG', {:zoom => @zoom, :year => @year_from, :month => @month_from, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects], :format => 'png'}, :class => 'icon icon-image' if respond_to?('gantt_image') %>

<%= l(:label_gantt) %>

@@ -72,6 +72,8 @@ t_height = g_height + headers_height <% end %> +<% cache(:year => @year_from, :month => @month_from, :months => @months, :zoom => @zoom, :tracker_ids => @selected_tracker_ids, :subprojects => params[:with_subprojects], :lang => current_language) do %> +
diff --git a/lib/redmine.rb b/lib/redmine.rb index f65715899..9fc2a103b 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -2,4 +2,10 @@ require 'redmine/version' require 'redmine/mime_type' require 'redmine/acts_as_watchable/init' +begin + require_library_or_gem 'rmagick' unless Object.const_defined?(:Magick) +rescue LoadError + # RMagick is not available +end + REDMINE_SUPPORTED_SCM = %w( Subversion Darcs Mercurial Cvs ) diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 9f43b53c6..3ba9d03bd 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -142,6 +142,7 @@ vertical-align: middle; .icon-pdf { background-image: url(../images/pdf.png); } .icon-csv { background-image: url(../images/csv.png); } .icon-html { background-image: url(../images/html.png); } +.icon-image { background-image: url(../images/image.png); } .icon-txt { background-image: url(../images/txt.png); } .icon-file { background-image: url(../images/file.png); } .icon-folder { background-image: url(../images/folder.png); } -- 2.39.5