Chris@0: # Redmine - project management software Chris@0: # Copyright (C) 2006-2008 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module Redmine Chris@0: module Helpers Chris@0: # Simple class to handle gantt chart data Chris@0: class Gantt chris@22: include ERB::Util chris@22: include Redmine::I18n chris@22: chris@22: # :nodoc: chris@22: # Some utility methods for the PDF export chris@22: class PDF chris@22: MaxCharactorsForSubject = 45 chris@22: TotalWidth = 280 chris@22: LeftPaneWidth = 100 chris@22: chris@22: def self.right_pane_width chris@22: TotalWidth - LeftPaneWidth chris@22: end chris@22: end chris@22: chris@22: attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months chris@22: attr_accessor :query chris@22: attr_accessor :project chris@22: attr_accessor :view chris@22: Chris@0: def initialize(options={}) Chris@0: options = options.dup Chris@0: Chris@0: if options[:year] && options[:year].to_i >0 Chris@0: @year_from = options[:year].to_i Chris@0: if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12 Chris@0: @month_from = options[:month].to_i Chris@0: else Chris@0: @month_from = 1 Chris@0: end Chris@0: else Chris@0: @month_from ||= Date.today.month Chris@0: @year_from ||= Date.today.year Chris@0: end Chris@0: Chris@0: zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i Chris@0: @zoom = (zoom > 0 && zoom < 5) ? zoom : 2 Chris@0: months = (options[:months] || User.current.pref[:gantt_months]).to_i Chris@0: @months = (months > 0 && months < 25) ? months : 6 Chris@0: Chris@0: # Save gantt parameters as user preference (zoom and months count) Chris@0: if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months])) Chris@0: User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months Chris@0: User.current.preference.save Chris@0: end Chris@0: Chris@0: @date_from = Date.civil(@year_from, @month_from, 1) Chris@0: @date_to = (@date_from >> @months) - 1 Chris@0: end chris@22: chris@22: def common_params chris@22: { :controller => 'gantts', :action => 'show', :project_id => @project } Chris@0: end Chris@0: Chris@0: def params chris@22: common_params.merge({ :zoom => zoom, :year => year_from, :month => month_from, :months => months }) Chris@0: end Chris@0: Chris@0: def params_previous chris@22: common_params.merge({:year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months }) Chris@0: end Chris@0: Chris@0: def params_next chris@22: common_params.merge({:year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months }) Chris@0: end chris@22: chris@22: ### Extracted from the HTML view/helpers chris@22: # Returns the number of rows that will be rendered on the Gantt chart chris@22: def number_of_rows chris@22: if @project chris@22: return number_of_rows_on_project(@project) chris@22: else chris@22: Project.roots.inject(0) do |total, project| chris@22: total += number_of_rows_on_project(project) chris@22: end chris@22: end chris@22: end chris@22: chris@22: # Returns the number of rows that will be used to list a project on chris@22: # the Gantt chart. This will recurse for each subproject. chris@22: def number_of_rows_on_project(project) chris@22: # Remove the project requirement for Versions because it will chris@22: # restrict issues to only be on the current project. This chris@22: # ends up missing issues which are assigned to shared versions. chris@22: @query.project = nil if @query.project chris@22: chris@22: # One Root project chris@22: count = 1 chris@22: # Issues without a Version chris@22: count += project.issues.for_gantt.without_version.with_query(@query).count chris@22: chris@22: # Versions chris@22: count += project.versions.count chris@22: chris@22: # Issues on the Versions chris@22: project.versions.each do |version| chris@22: count += version.fixed_issues.for_gantt.with_query(@query).count chris@22: end chris@22: chris@22: # Subprojects chris@22: project.children.each do |subproject| chris@22: count += number_of_rows_on_project(subproject) chris@22: end chris@22: chris@22: count chris@22: end chris@22: chris@22: # Renders the subjects of the Gantt chart, the left side. chris@22: def subjects(options={}) chris@22: options = {:indent => 4, :render => :subject, :format => :html}.merge(options) chris@22: chris@22: output = '' chris@22: if @project chris@22: output << render_project(@project, options) chris@22: else chris@22: Project.roots.each do |project| chris@22: output << render_project(project, options) chris@22: end chris@22: end chris@22: chris@22: output chris@22: end chris@22: chris@22: # Renders the lines of the Gantt chart, the right side chris@22: def lines(options={}) chris@22: options = {:indent => 4, :render => :line, :format => :html}.merge(options) chris@22: output = '' chris@22: chris@22: if @project chris@22: output << render_project(@project, options) chris@22: else chris@22: Project.roots.each do |project| chris@22: output << render_project(project, options) chris@22: end chris@22: end chris@22: chris@22: output chris@22: end chris@22: chris@22: def render_project(project, options={}) chris@22: options[:top] = 0 unless options.key? :top chris@22: options[:indent_increment] = 20 unless options.key? :indent_increment chris@22: options[:top_increment] = 20 unless options.key? :top_increment chris@22: chris@22: output = '' chris@22: # Project Header chris@22: project_header = if options[:render] == :subject chris@22: subject_for_project(project, options) chris@22: else chris@22: # :line chris@22: line_for_project(project, options) chris@22: end chris@22: output << project_header if options[:format] == :html chris@22: chris@22: options[:top] += options[:top_increment] chris@22: options[:indent] += options[:indent_increment] chris@22: chris@22: # Second, Issues without a version chris@22: issues = project.issues.for_gantt.without_version.with_query(@query) chris@22: if issues chris@22: issue_rendering = render_issues(issues, options) chris@22: output << issue_rendering if options[:format] == :html chris@22: end chris@22: chris@22: # Third, Versions chris@22: project.versions.sort.each do |version| chris@22: version_rendering = render_version(version, options) chris@22: output << version_rendering if options[:format] == :html chris@22: end chris@22: chris@22: # Fourth, subprojects chris@22: project.children.each do |project| chris@22: subproject_rendering = render_project(project, options) chris@22: output << subproject_rendering if options[:format] == :html chris@22: end chris@22: chris@22: # Remove indent to hit the next sibling chris@22: options[:indent] -= options[:indent_increment] chris@22: chris@22: output chris@22: end chris@22: chris@22: def render_issues(issues, options={}) chris@22: output = '' chris@22: issues.each do |i| chris@22: issue_rendering = if options[:render] == :subject chris@22: subject_for_issue(i, options) chris@22: else chris@22: # :line chris@22: line_for_issue(i, options) chris@22: end chris@22: output << issue_rendering if options[:format] == :html chris@22: options[:top] += options[:top_increment] chris@22: end chris@22: output chris@22: end chris@22: chris@22: def render_version(version, options={}) chris@22: output = '' chris@22: # Version header chris@22: version_rendering = if options[:render] == :subject chris@22: subject_for_version(version, options) chris@22: else chris@22: # :line chris@22: line_for_version(version, options) chris@22: end chris@22: chris@22: output << version_rendering if options[:format] == :html chris@22: chris@22: options[:top] += options[:top_increment] chris@22: chris@22: # Remove the project requirement for Versions because it will chris@22: # restrict issues to only be on the current project. This chris@22: # ends up missing issues which are assigned to shared versions. chris@22: @query.project = nil if @query.project chris@22: chris@22: issues = version.fixed_issues.for_gantt.with_query(@query) chris@22: if issues chris@22: # Indent issues chris@22: options[:indent] += options[:indent_increment] chris@22: output << render_issues(issues, options) chris@22: options[:indent] -= options[:indent_increment] chris@22: end chris@22: chris@22: output chris@22: end chris@22: chris@22: def subject_for_project(project, options) chris@22: case options[:format] chris@22: when :html chris@22: output = '' chris@22: chris@22: output << "
" chris@22: if project.is_a? Project chris@22: output << "" chris@22: output << view.link_to_project(project) chris@22: output << '' chris@22: else chris@22: ActiveRecord::Base.logger.debug "Gantt#subject_for_project was not given a project" chris@22: '' chris@22: end chris@22: output << "
" chris@22: chris@22: output chris@22: when :image chris@22: chris@22: options[:image].fill('black') chris@22: options[:image].stroke('transparent') chris@22: options[:image].stroke_width(1) chris@22: options[:image].text(options[:indent], options[:top] + 2, project.name) chris@22: when :pdf chris@22: options[:pdf].SetY(options[:top]) chris@22: options[:pdf].SetX(15) chris@22: chris@22: char_limit = PDF::MaxCharactorsForSubject - options[:indent] chris@22: options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{project.name}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR") chris@22: chris@22: options[:pdf].SetY(options[:top]) chris@22: options[:pdf].SetX(options[:subject_width]) chris@22: options[:pdf].Cell(options[:g_width], 5, "", "LR") chris@22: end chris@22: end chris@22: chris@22: def line_for_project(project, options) chris@37: # Skip versions that don't have a start_date or due date chris@37: if project.is_a?(Project) && project.start_date && project.due_date chris@22: options[:zoom] ||= 1 chris@22: options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom] chris@22: chris@22: chris@22: case options[:format] chris@22: when :html chris@22: output = '' chris@22: i_left = ((project.start_date - self.date_from)*options[:zoom]).floor chris@22: chris@22: start_date = project.start_date chris@22: start_date ||= self.date_from chris@22: start_left = ((start_date - self.date_from)*options[:zoom]).floor chris@22: chris@22: i_end_date = ((project.due_date <= self.date_to) ? project.due_date : self.date_to ) chris@22: i_done_date = start_date + ((project.due_date - start_date+1)* project.completed_percent(:include_subprojects => true)/100).floor chris@22: i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date ) chris@22: i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date ) chris@22: chris@22: i_late_date = [i_end_date, Date.today].min if start_date < Date.today chris@22: i_end = ((i_end_date - self.date_from) * options[:zoom]).floor chris@22: chris@22: i_width = (i_end - i_left + 1).floor - 2 # total width of the issue (- 2 for left and right borders) chris@22: d_width = ((i_done_date - start_date)*options[:zoom]).floor - 2 # done width chris@22: l_width = i_late_date ? ((i_late_date - start_date+1)*options[:zoom]).floor - 2 : 0 # delay width chris@22: chris@22: # Bar graphic chris@22: chris@22: # Make sure that negative i_left and i_width don't chris@22: # overflow the subject chris@22: if i_end > 0 && i_left <= options[:g_width] chris@22: output << "
 
" chris@22: end chris@22: chris@22: if l_width > 0 && i_left <= options[:g_width] chris@22: output << "
 
" chris@22: end chris@22: if d_width > 0 && i_left <= options[:g_width] chris@22: output<< "
 
" chris@22: end chris@22: chris@22: chris@22: # Starting diamond chris@22: if start_left <= options[:g_width] && start_left > 0 chris@22: output << "
 
" chris@22: output << "
" chris@22: output << "
" chris@22: end chris@22: chris@22: # Ending diamond chris@22: # Don't show items too far ahead chris@22: if i_end <= options[:g_width] && i_end > 0 chris@22: output << "
 
" chris@22: end chris@22: chris@22: # DIsplay the Project name and % chris@22: if i_end <= options[:g_width] chris@22: # Display the status even if it's floated off to the left chris@22: status_px = i_end + 12 # 12px for the diamond chris@22: status_px = 0 if status_px <= 0 chris@22: chris@22: output << "
" chris@22: output << "#{h project } #{h project.completed_percent(:include_subprojects => true).to_i.to_s}%" chris@22: output << "
" chris@22: end chris@22: chris@22: output chris@22: when :image chris@22: options[:image].stroke('transparent') chris@22: i_left = options[:subject_width] + ((project.due_date - self.date_from)*options[:zoom]).floor chris@22: chris@22: # Make sure negative i_left doesn't overflow the subject chris@22: if i_left > options[:subject_width] chris@22: options[:image].fill('blue') chris@22: options[:image].rectangle(i_left, options[:top], i_left + 6, options[:top] - 6) chris@22: options[:image].fill('black') chris@22: options[:image].text(i_left + 11, options[:top] + 1, project.name) chris@22: end chris@22: when :pdf chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: i_left = ((project.due_date - @date_from)*options[:zoom]) chris@22: chris@22: # Make sure negative i_left doesn't overflow the subject chris@22: if i_left > 0 chris@22: options[:pdf].SetX(options[:subject_width] + i_left) chris@22: options[:pdf].SetFillColor(50,50,200) chris@22: options[:pdf].Cell(2, 2, "", 0, 0, "", 1) chris@22: chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: options[:pdf].SetX(options[:subject_width] + i_left + 3) chris@22: options[:pdf].Cell(30, 2, "#{project.name}") chris@22: end chris@22: end chris@22: else chris@22: ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date" chris@22: '' chris@22: end chris@22: end chris@22: chris@22: def subject_for_version(version, options) chris@22: case options[:format] chris@22: when :html chris@22: output = '' chris@22: output << "
" chris@22: if version.is_a? Version chris@22: output << "" chris@22: output << view.link_to_version(version) chris@22: output << '' chris@22: else chris@22: ActiveRecord::Base.logger.debug "Gantt#subject_for_version was not given a version" chris@22: '' chris@22: end chris@22: output << "
" chris@22: chris@22: output chris@22: when :image chris@22: options[:image].fill('black') chris@22: options[:image].stroke('transparent') chris@22: options[:image].stroke_width(1) chris@22: options[:image].text(options[:indent], options[:top] + 2, version.to_s_with_project) chris@22: when :pdf chris@22: options[:pdf].SetY(options[:top]) chris@22: options[:pdf].SetX(15) chris@22: chris@22: char_limit = PDF::MaxCharactorsForSubject - options[:indent] chris@22: options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{version.to_s_with_project}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR") chris@22: chris@22: options[:pdf].SetY(options[:top]) chris@22: options[:pdf].SetX(options[:subject_width]) chris@22: options[:pdf].Cell(options[:g_width], 5, "", "LR") chris@22: end chris@22: end chris@22: chris@22: def line_for_version(version, options) chris@22: # Skip versions that don't have a start_date chris@37: if version.is_a?(Version) && version.start_date && version.due_date chris@22: options[:zoom] ||= 1 chris@22: options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom] chris@22: chris@22: case options[:format] chris@22: when :html chris@22: output = '' chris@22: i_left = ((version.start_date - self.date_from)*options[:zoom]).floor chris@22: # TODO: or version.fixed_issues.collect(&:start_date).min chris@22: start_date = version.fixed_issues.minimum('start_date') if version.fixed_issues.present? chris@22: start_date ||= self.date_from chris@22: start_left = ((start_date - self.date_from)*options[:zoom]).floor chris@22: chris@22: i_end_date = ((version.due_date <= self.date_to) ? version.due_date : self.date_to ) chris@22: i_done_date = start_date + ((version.due_date - start_date+1)* version.completed_pourcent/100).floor chris@22: i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date ) chris@22: i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date ) chris@22: chris@22: i_late_date = [i_end_date, Date.today].min if start_date < Date.today chris@22: chris@22: i_width = (i_left - start_left + 1).floor - 2 # total width of the issue (- 2 for left and right borders) chris@22: d_width = ((i_done_date - start_date)*options[:zoom]).floor - 2 # done width chris@22: l_width = i_late_date ? ((i_late_date - start_date+1)*options[:zoom]).floor - 2 : 0 # delay width chris@22: chris@22: i_end = ((i_end_date - self.date_from) * options[:zoom]).floor # Ending pixel chris@22: chris@22: # Bar graphic chris@22: chris@22: # Make sure that negative i_left and i_width don't chris@22: # overflow the subject chris@22: if i_width > 0 && i_left <= options[:g_width] chris@22: output << "
 
" chris@22: end chris@22: if l_width > 0 && i_left <= options[:g_width] chris@22: output << "
 
" chris@22: end chris@22: if d_width > 0 && i_left <= options[:g_width] chris@22: output<< "
 
" chris@22: end chris@22: chris@22: chris@22: # Starting diamond chris@22: if start_left <= options[:g_width] && start_left > 0 chris@22: output << "
 
" chris@22: output << "
" chris@22: output << "
" chris@22: end chris@22: chris@22: # Ending diamond chris@22: # Don't show items too far ahead chris@22: if i_left <= options[:g_width] && i_end > 0 chris@22: output << "
 
" chris@22: end chris@22: chris@22: # Display the Version name and % chris@22: if i_end <= options[:g_width] chris@22: # Display the status even if it's floated off to the left chris@22: status_px = i_end + 12 # 12px for the diamond chris@22: status_px = 0 if status_px <= 0 chris@22: chris@22: output << "
" chris@22: output << h("#{version.project} -") unless @project && @project == version.project chris@22: output << "#{h version } #{h version.completed_pourcent.to_i.to_s}%" chris@22: output << "
" chris@22: end chris@22: chris@22: output chris@22: when :image chris@22: options[:image].stroke('transparent') chris@22: i_left = options[:subject_width] + ((version.start_date - @date_from)*options[:zoom]).floor chris@22: chris@22: # Make sure negative i_left doesn't overflow the subject chris@22: if i_left > options[:subject_width] chris@22: options[:image].fill('green') chris@22: options[:image].rectangle(i_left, options[:top], i_left + 6, options[:top] - 6) chris@22: options[:image].fill('black') chris@22: options[:image].text(i_left + 11, options[:top] + 1, version.name) chris@22: end chris@22: when :pdf chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: i_left = ((version.start_date - @date_from)*options[:zoom]) chris@22: chris@22: # Make sure negative i_left doesn't overflow the subject chris@22: if i_left > 0 chris@22: options[:pdf].SetX(options[:subject_width] + i_left) chris@22: options[:pdf].SetFillColor(50,200,50) chris@22: options[:pdf].Cell(2, 2, "", 0, 0, "", 1) chris@22: chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: options[:pdf].SetX(options[:subject_width] + i_left + 3) chris@22: options[:pdf].Cell(30, 2, "#{version.name}") chris@22: end chris@22: end chris@22: else chris@22: ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date" chris@22: '' chris@22: end chris@22: end chris@22: chris@22: def subject_for_issue(issue, options) chris@22: case options[:format] chris@22: when :html chris@22: output = '' chris@22: output << "
" chris@22: output << "
" chris@22: if issue.is_a? Issue chris@22: css_classes = [] chris@22: css_classes << 'issue-overdue' if issue.overdue? chris@22: css_classes << 'issue-behind-schedule' if issue.behind_schedule? chris@22: css_classes << 'icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to chris@22: chris@22: if issue.assigned_to.present? chris@22: assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name chris@22: output << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string) chris@22: end chris@22: output << "" chris@22: output << view.link_to_issue(issue) chris@22: output << '' chris@22: else chris@22: ActiveRecord::Base.logger.debug "Gantt#subject_for_issue was not given an issue" chris@22: '' chris@22: end chris@22: output << "
" chris@22: chris@22: # Tooltip chris@22: if issue.is_a? Issue chris@22: output << "" chris@22: output << view.render_issue_tooltip(issue) chris@22: output << "" chris@22: end chris@22: chris@22: output << "
" chris@22: output chris@22: when :image chris@22: options[:image].fill('black') chris@22: options[:image].stroke('transparent') chris@22: options[:image].stroke_width(1) chris@22: options[:image].text(options[:indent], options[:top] + 2, issue.subject) chris@22: when :pdf chris@22: options[:pdf].SetY(options[:top]) chris@22: options[:pdf].SetX(15) chris@22: chris@22: char_limit = PDF::MaxCharactorsForSubject - options[:indent] chris@22: options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{issue.tracker} #{issue.id}: #{issue.subject}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR") chris@22: chris@22: options[:pdf].SetY(options[:top]) chris@22: options[:pdf].SetX(options[:subject_width]) chris@22: options[:pdf].Cell(options[:g_width], 5, "", "LR") chris@22: end chris@22: end chris@22: chris@22: def line_for_issue(issue, options) chris@22: # Skip issues that don't have a due_before (due_date or version's due_date) chris@22: if issue.is_a?(Issue) && issue.due_before chris@22: case options[:format] chris@22: when :html chris@22: output = '' chris@22: # Handle nil start_dates, rare but can happen. chris@22: i_start_date = if issue.start_date && issue.start_date >= self.date_from chris@22: issue.start_date chris@22: else chris@22: self.date_from chris@22: end chris@22: chris@22: i_end_date = ((issue.due_before && issue.due_before <= self.date_to) ? issue.due_before : self.date_to ) chris@22: i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor chris@22: i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date ) chris@22: i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date ) chris@22: chris@22: i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today chris@22: chris@22: i_left = ((i_start_date - self.date_from)*options[:zoom]).floor chris@22: i_width = ((i_end_date - i_start_date + 1)*options[:zoom]).floor - 2 # total width of the issue (- 2 for left and right borders) chris@22: d_width = ((i_done_date - i_start_date)*options[:zoom]).floor - 2 # done width chris@22: l_width = i_late_date ? ((i_late_date - i_start_date+1)*options[:zoom]).floor - 2 : 0 # delay width chris@22: css = "task " + (issue.leaf? ? 'leaf' : 'parent') chris@22: chris@22: # Make sure that negative i_left and i_width don't chris@22: # overflow the subject chris@22: if i_width > 0 chris@22: output << "
 
" chris@22: end chris@22: if l_width > 0 chris@22: output << "
 
" chris@22: end chris@22: if d_width > 0 chris@22: output<< "
 
" chris@22: end chris@22: chris@22: # Display the status even if it's floated off to the left chris@22: status_px = i_left + i_width + 5 chris@22: status_px = 5 if status_px <= 0 chris@22: chris@22: output << "
" chris@22: output << issue.status.name chris@22: output << ' ' chris@22: output << (issue.done_ratio).to_i.to_s chris@22: output << "%" chris@22: output << "
" chris@22: chris@22: output << "
" chris@22: output << '' chris@22: output << view.render_issue_tooltip(issue) chris@22: output << "
" chris@22: output chris@22: chris@22: when :image chris@22: # Handle nil start_dates, rare but can happen. chris@22: i_start_date = if issue.start_date && issue.start_date >= @date_from chris@22: issue.start_date chris@22: else chris@22: @date_from chris@22: end chris@22: chris@22: i_end_date = (issue.due_before <= date_to ? issue.due_before : date_to ) chris@22: i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor chris@22: i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date ) chris@22: i_done_date = (i_done_date >= date_to ? date_to : i_done_date ) chris@22: i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today chris@22: chris@22: i_left = options[:subject_width] + ((i_start_date - @date_from)*options[:zoom]).floor chris@22: i_width = ((i_end_date - i_start_date + 1)*options[:zoom]).floor # total width of the issue chris@22: d_width = ((i_done_date - i_start_date)*options[:zoom]).floor # done width chris@22: l_width = i_late_date ? ((i_late_date - i_start_date+1)*options[:zoom]).floor : 0 # delay width chris@22: chris@22: chris@22: # Make sure that negative i_left and i_width don't chris@22: # overflow the subject chris@22: if i_width > 0 chris@22: options[:image].fill('grey') chris@22: options[:image].rectangle(i_left, options[:top], i_left + i_width, options[:top] - 6) chris@22: options[:image].fill('red') chris@22: options[:image].rectangle(i_left, options[:top], i_left + l_width, options[:top] - 6) if l_width > 0 chris@22: options[:image].fill('blue') chris@22: options[:image].rectangle(i_left, options[:top], i_left + d_width, options[:top] - 6) if d_width > 0 chris@22: end chris@22: chris@22: # Show the status and % done next to the subject if it overflows chris@22: options[:image].fill('black') chris@22: if i_width > 0 chris@22: options[:image].text(i_left + i_width + 5,options[:top] + 1, "#{issue.status.name} #{issue.done_ratio}%") chris@22: else chris@22: options[:image].text(options[:subject_width] + 5,options[:top] + 1, "#{issue.status.name} #{issue.done_ratio}%") chris@22: end chris@22: chris@22: when :pdf chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: # Handle nil start_dates, rare but can happen. chris@22: i_start_date = if issue.start_date && issue.start_date >= @date_from chris@22: issue.start_date chris@22: else chris@22: @date_from chris@22: end chris@22: chris@22: i_end_date = (issue.due_before <= @date_to ? issue.due_before : @date_to ) chris@22: chris@22: i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor chris@22: i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date ) chris@22: i_done_date = (i_done_date >= @date_to ? @date_to : i_done_date ) chris@22: chris@22: i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today chris@22: chris@22: i_left = ((i_start_date - @date_from)*options[:zoom]) chris@22: i_width = ((i_end_date - i_start_date + 1)*options[:zoom]) chris@22: d_width = ((i_done_date - i_start_date)*options[:zoom]) chris@22: l_width = ((i_late_date - i_start_date+1)*options[:zoom]) if i_late_date chris@22: l_width ||= 0 chris@22: chris@22: # Make sure that negative i_left and i_width don't chris@22: # overflow the subject chris@22: if i_width > 0 chris@22: options[:pdf].SetX(options[:subject_width] + i_left) chris@22: options[:pdf].SetFillColor(200,200,200) chris@22: options[:pdf].Cell(i_width, 2, "", 0, 0, "", 1) chris@22: end chris@22: chris@22: if l_width > 0 chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: options[:pdf].SetX(options[:subject_width] + i_left) chris@22: options[:pdf].SetFillColor(255,100,100) chris@22: options[:pdf].Cell(l_width, 2, "", 0, 0, "", 1) chris@22: end chris@22: if d_width > 0 chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: options[:pdf].SetX(options[:subject_width] + i_left) chris@22: options[:pdf].SetFillColor(100,100,255) chris@22: options[:pdf].Cell(d_width, 2, "", 0, 0, "", 1) chris@22: end chris@22: chris@22: options[:pdf].SetY(options[:top]+1.5) chris@22: chris@22: # Make sure that negative i_left and i_width don't chris@22: # overflow the subject chris@22: if (i_left + i_width) >= 0 chris@22: options[:pdf].SetX(options[:subject_width] + i_left + i_width) chris@22: else chris@22: options[:pdf].SetX(options[:subject_width]) chris@22: end chris@22: options[:pdf].Cell(30, 2, "#{issue.status} #{issue.done_ratio}%") chris@22: end chris@22: else chris@22: ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before" chris@22: '' chris@22: end chris@22: end chris@22: Chris@0: # Generates a gantt image Chris@0: # Only defined if RMagick is avalaible chris@22: def to_image(format='PNG') Chris@0: date_to = (@date_from >> @months)-1 Chris@0: show_weeks = @zoom > 1 Chris@0: show_days = @zoom > 2 Chris@0: Chris@1: subject_width = 400 Chris@0: header_heigth = 18 Chris@0: # width of one day in pixels Chris@0: zoom = @zoom*2 Chris@0: g_width = (@date_to - @date_from + 1)*zoom chris@22: g_height = 20 * number_of_rows + 30 Chris@0: headers_heigth = (show_weeks ? 2*header_heigth : header_heigth) Chris@0: height = g_height + headers_heigth Chris@0: Chris@0: imgl = Magick::ImageList.new Chris@0: imgl.new_image(subject_width+g_width+1, height) Chris@0: gc = Magick::Draw.new Chris@0: Chris@0: # Subjects chris@22: subjects(:image => gc, :top => (headers_heigth + 20), :indent => 4, :format => :image) Chris@0: Chris@0: # Months headers Chris@0: month_f = @date_from Chris@0: left = subject_width Chris@0: @months.times do Chris@0: width = ((month_f >> 1) - month_f) * zoom Chris@0: gc.fill('white') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left, 0, left + width, height) Chris@0: gc.fill('black') Chris@0: gc.stroke('transparent') Chris@0: gc.stroke_width(1) Chris@0: gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}") Chris@0: left = left + width Chris@0: month_f = month_f >> 1 Chris@0: end Chris@0: Chris@0: # Weeks headers Chris@0: if show_weeks Chris@0: left = subject_width Chris@0: height = header_heigth Chris@0: if @date_from.cwday == 1 Chris@0: # date_from is monday Chris@0: week_f = date_from Chris@0: else Chris@0: # find next monday after date_from Chris@0: week_f = @date_from + (7 - @date_from.cwday + 1) Chris@0: width = (7 - @date_from.cwday + 1) * zoom Chris@0: gc.fill('white') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1) Chris@0: left = left + width Chris@0: end Chris@0: while week_f <= date_to Chris@0: width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom Chris@0: gc.fill('white') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1) Chris@0: gc.fill('black') Chris@0: gc.stroke('transparent') Chris@0: gc.stroke_width(1) Chris@0: gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s) Chris@0: left = left + width Chris@0: week_f = week_f+7 Chris@0: end Chris@0: end Chris@0: Chris@0: # Days details (week-end in grey) Chris@0: if show_days Chris@0: left = subject_width Chris@0: height = g_height + header_heigth - 1 Chris@0: wday = @date_from.cwday Chris@0: (date_to - @date_from + 1).to_i.times do Chris@0: width = zoom Chris@0: gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1) Chris@0: left = left + width Chris@0: wday = wday + 1 Chris@0: wday = 1 if wday > 7 Chris@0: end Chris@0: end Chris@0: Chris@0: # border Chris@0: gc.fill('transparent') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(0, 0, subject_width+g_width, headers_heigth) Chris@0: gc.stroke('black') Chris@0: gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1) Chris@0: Chris@0: # content Chris@0: top = headers_heigth + 20 chris@22: chris@22: lines(:image => gc, :top => top, :zoom => zoom, :subject_width => subject_width, :format => :image) Chris@0: Chris@0: # today red line Chris@0: if Date.today >= @date_from and Date.today <= date_to Chris@0: gc.stroke('red') Chris@0: x = (Date.today-@date_from+1)*zoom + subject_width Chris@0: gc.line(x, headers_heigth, x, headers_heigth + g_height-1) Chris@0: end Chris@0: Chris@0: gc.draw(imgl) Chris@0: imgl.format = format Chris@0: imgl.to_blob Chris@0: end if Object.const_defined?(:Magick) chris@22: chris@22: def to_pdf chris@22: pdf = ::Redmine::Export::PDF::IFPDF.new(current_language) chris@22: pdf.SetTitle("#{l(:label_gantt)} #{project}") chris@22: pdf.AliasNbPages chris@22: pdf.footer_date = format_date(Date.today) chris@22: pdf.AddPage("L") chris@22: pdf.SetFontStyle('B',12) chris@22: pdf.SetX(15) chris@22: pdf.Cell(PDF::LeftPaneWidth, 20, project.to_s) chris@22: pdf.Ln chris@22: pdf.SetFontStyle('B',9) chris@22: chris@22: subject_width = PDF::LeftPaneWidth chris@22: header_heigth = 5 chris@22: chris@22: headers_heigth = header_heigth chris@22: show_weeks = false chris@22: show_days = false chris@22: chris@22: if self.months < 7 chris@22: show_weeks = true chris@22: headers_heigth = 2*header_heigth chris@22: if self.months < 3 chris@22: show_days = true chris@22: headers_heigth = 3*header_heigth chris@22: end chris@22: end chris@22: chris@22: g_width = PDF.right_pane_width chris@22: zoom = (g_width) / (self.date_to - self.date_from + 1) chris@22: g_height = 120 chris@22: t_height = g_height + headers_heigth chris@22: chris@22: y_start = pdf.GetY chris@22: chris@22: # Months headers chris@22: month_f = self.date_from chris@22: left = subject_width chris@22: height = header_heigth chris@22: self.months.times do chris@22: width = ((month_f >> 1) - month_f) * zoom chris@22: pdf.SetY(y_start) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C") chris@22: left = left + width chris@22: month_f = month_f >> 1 chris@22: end chris@22: chris@22: # Weeks headers chris@22: if show_weeks chris@22: left = subject_width chris@22: height = header_heigth chris@22: if self.date_from.cwday == 1 chris@22: # self.date_from is monday chris@22: week_f = self.date_from chris@22: else chris@22: # find next monday after self.date_from chris@22: week_f = self.date_from + (7 - self.date_from.cwday + 1) chris@22: width = (7 - self.date_from.cwday + 1) * zoom-1 chris@22: pdf.SetY(y_start + header_heigth) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width + 1, height, "", "LTR") chris@22: left = left + width+1 chris@22: end chris@22: while week_f <= self.date_to chris@22: width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom chris@22: pdf.SetY(y_start + header_heigth) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C") chris@22: left = left + width chris@22: week_f = week_f+7 chris@22: end chris@22: end chris@22: chris@22: # Days headers chris@22: if show_days chris@22: left = subject_width chris@22: height = header_heigth chris@22: wday = self.date_from.cwday chris@22: pdf.SetFontStyle('B',7) chris@22: (self.date_to - self.date_from + 1).to_i.times do chris@22: width = zoom chris@22: pdf.SetY(y_start + 2 * header_heigth) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C") chris@22: left = left + width chris@22: wday = wday + 1 chris@22: wday = 1 if wday > 7 chris@22: end chris@22: end chris@22: chris@22: pdf.SetY(y_start) chris@22: pdf.SetX(15) chris@22: pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1) chris@22: chris@22: # Tasks chris@22: top = headers_heigth + y_start chris@22: pdf_subjects_and_lines(pdf, { chris@22: :top => top, chris@22: :zoom => zoom, chris@22: :subject_width => subject_width, chris@22: :g_width => g_width chris@22: }) chris@22: chris@22: chris@22: pdf.Line(15, top, subject_width+g_width, top) chris@22: pdf.Output chris@22: chris@22: chris@22: end Chris@0: Chris@0: private chris@22: chris@22: # Renders both the subjects and lines of the Gantt chart for the chris@22: # PDF format chris@22: def pdf_subjects_and_lines(pdf, options = {}) chris@22: subject_options = {:indent => 0, :indent_increment => 5, :top_increment => 3, :render => :subject, :format => :pdf, :pdf => pdf}.merge(options) chris@22: line_options = {:indent => 0, :indent_increment => 5, :top_increment => 3, :render => :line, :format => :pdf, :pdf => pdf}.merge(options) chris@22: chris@22: if @project chris@22: render_project(@project, subject_options) chris@22: render_project(@project, line_options) Chris@0: else chris@22: Project.roots.each do |project| chris@22: render_project(project, subject_options) chris@22: render_project(project, line_options) Chris@0: end Chris@0: end Chris@0: end chris@22: Chris@0: end Chris@0: end Chris@0: end