annotate lib/redmine/helpers/gantt.rb @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children cca12e1c1fd4
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module Redmine
Chris@0 19 module Helpers
Chris@0 20 # Simple class to handle gantt chart data
Chris@0 21 class Gantt
Chris@0 22 attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :events
Chris@0 23
Chris@0 24 def initialize(options={})
Chris@0 25 options = options.dup
Chris@0 26 @events = []
Chris@0 27
Chris@0 28 if options[:year] && options[:year].to_i >0
Chris@0 29 @year_from = options[:year].to_i
Chris@0 30 if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12
Chris@0 31 @month_from = options[:month].to_i
Chris@0 32 else
Chris@0 33 @month_from = 1
Chris@0 34 end
Chris@0 35 else
Chris@0 36 @month_from ||= Date.today.month
Chris@0 37 @year_from ||= Date.today.year
Chris@0 38 end
Chris@0 39
Chris@0 40 zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i
Chris@0 41 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
Chris@0 42 months = (options[:months] || User.current.pref[:gantt_months]).to_i
Chris@0 43 @months = (months > 0 && months < 25) ? months : 6
Chris@0 44
Chris@0 45 # Save gantt parameters as user preference (zoom and months count)
Chris@0 46 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
Chris@0 47 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
Chris@0 48 User.current.preference.save
Chris@0 49 end
Chris@0 50
Chris@0 51 @date_from = Date.civil(@year_from, @month_from, 1)
Chris@0 52 @date_to = (@date_from >> @months) - 1
Chris@0 53 end
Chris@0 54
Chris@0 55
Chris@0 56 def events=(e)
Chris@0 57 @events = e
Chris@0 58 # Adds all ancestors
Chris@0 59 root_ids = e.select {|i| i.is_a?(Issue) && i.parent_id? }.collect(&:root_id).uniq
Chris@0 60 if root_ids.any?
Chris@0 61 # Retrieves all nodes
Chris@0 62 parents = Issue.find_all_by_root_id(root_ids, :conditions => ["rgt - lft > 1"])
Chris@0 63 # Only add ancestors
Chris@0 64 @events += parents.select {|p| @events.detect {|i| i.is_a?(Issue) && p.is_ancestor_of?(i)}}
Chris@0 65 end
Chris@0 66 @events.uniq!
Chris@0 67 # Sort issues by hierarchy and start dates
Chris@0 68 @events.sort! {|x,y|
Chris@0 69 if x.is_a?(Issue) && y.is_a?(Issue)
Chris@0 70 gantt_issue_compare(x, y, @events)
Chris@0 71 else
Chris@0 72 gantt_start_compare(x, y)
Chris@0 73 end
Chris@0 74 }
Chris@0 75 # Removes issues that have no start or end date
Chris@0 76 @events.reject! {|i| i.is_a?(Issue) && (i.start_date.nil? || i.due_before.nil?) }
Chris@0 77 @events
Chris@0 78 end
Chris@0 79
Chris@0 80 def params
Chris@0 81 { :zoom => zoom, :year => year_from, :month => month_from, :months => months }
Chris@0 82 end
Chris@0 83
Chris@0 84 def params_previous
Chris@0 85 { :year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months }
Chris@0 86 end
Chris@0 87
Chris@0 88 def params_next
Chris@0 89 { :year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months }
Chris@0 90 end
Chris@0 91
Chris@0 92 # Generates a gantt image
Chris@0 93 # Only defined if RMagick is avalaible
Chris@0 94 def to_image(format='PNG')
Chris@0 95 date_to = (@date_from >> @months)-1
Chris@0 96 show_weeks = @zoom > 1
Chris@0 97 show_days = @zoom > 2
Chris@0 98
Chris@0 99 subject_width = 320
Chris@0 100 header_heigth = 18
Chris@0 101 # width of one day in pixels
Chris@0 102 zoom = @zoom*2
Chris@0 103 g_width = (@date_to - @date_from + 1)*zoom
Chris@0 104 g_height = 20 * events.length + 20
Chris@0 105 headers_heigth = (show_weeks ? 2*header_heigth : header_heigth)
Chris@0 106 height = g_height + headers_heigth
Chris@0 107
Chris@0 108 imgl = Magick::ImageList.new
Chris@0 109 imgl.new_image(subject_width+g_width+1, height)
Chris@0 110 gc = Magick::Draw.new
Chris@0 111
Chris@0 112 # Subjects
Chris@0 113 top = headers_heigth + 20
Chris@0 114 gc.fill('black')
Chris@0 115 gc.stroke('transparent')
Chris@0 116 gc.stroke_width(1)
Chris@0 117 events.each do |i|
Chris@0 118 gc.text(4, top + 2, (i.is_a?(Issue) ? i.subject : i.name))
Chris@0 119 top = top + 20
Chris@0 120 end
Chris@0 121
Chris@0 122 # Months headers
Chris@0 123 month_f = @date_from
Chris@0 124 left = subject_width
Chris@0 125 @months.times do
Chris@0 126 width = ((month_f >> 1) - month_f) * zoom
Chris@0 127 gc.fill('white')
Chris@0 128 gc.stroke('grey')
Chris@0 129 gc.stroke_width(1)
Chris@0 130 gc.rectangle(left, 0, left + width, height)
Chris@0 131 gc.fill('black')
Chris@0 132 gc.stroke('transparent')
Chris@0 133 gc.stroke_width(1)
Chris@0 134 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
Chris@0 135 left = left + width
Chris@0 136 month_f = month_f >> 1
Chris@0 137 end
Chris@0 138
Chris@0 139 # Weeks headers
Chris@0 140 if show_weeks
Chris@0 141 left = subject_width
Chris@0 142 height = header_heigth
Chris@0 143 if @date_from.cwday == 1
Chris@0 144 # date_from is monday
Chris@0 145 week_f = date_from
Chris@0 146 else
Chris@0 147 # find next monday after date_from
Chris@0 148 week_f = @date_from + (7 - @date_from.cwday + 1)
Chris@0 149 width = (7 - @date_from.cwday + 1) * zoom
Chris@0 150 gc.fill('white')
Chris@0 151 gc.stroke('grey')
Chris@0 152 gc.stroke_width(1)
Chris@0 153 gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1)
Chris@0 154 left = left + width
Chris@0 155 end
Chris@0 156 while week_f <= date_to
Chris@0 157 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
Chris@0 158 gc.fill('white')
Chris@0 159 gc.stroke('grey')
Chris@0 160 gc.stroke_width(1)
Chris@0 161 gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1)
Chris@0 162 gc.fill('black')
Chris@0 163 gc.stroke('transparent')
Chris@0 164 gc.stroke_width(1)
Chris@0 165 gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s)
Chris@0 166 left = left + width
Chris@0 167 week_f = week_f+7
Chris@0 168 end
Chris@0 169 end
Chris@0 170
Chris@0 171 # Days details (week-end in grey)
Chris@0 172 if show_days
Chris@0 173 left = subject_width
Chris@0 174 height = g_height + header_heigth - 1
Chris@0 175 wday = @date_from.cwday
Chris@0 176 (date_to - @date_from + 1).to_i.times do
Chris@0 177 width = zoom
Chris@0 178 gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white')
Chris@0 179 gc.stroke('grey')
Chris@0 180 gc.stroke_width(1)
Chris@0 181 gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1)
Chris@0 182 left = left + width
Chris@0 183 wday = wday + 1
Chris@0 184 wday = 1 if wday > 7
Chris@0 185 end
Chris@0 186 end
Chris@0 187
Chris@0 188 # border
Chris@0 189 gc.fill('transparent')
Chris@0 190 gc.stroke('grey')
Chris@0 191 gc.stroke_width(1)
Chris@0 192 gc.rectangle(0, 0, subject_width+g_width, headers_heigth)
Chris@0 193 gc.stroke('black')
Chris@0 194 gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1)
Chris@0 195
Chris@0 196 # content
Chris@0 197 top = headers_heigth + 20
Chris@0 198 gc.stroke('transparent')
Chris@0 199 events.each do |i|
Chris@0 200 if i.is_a?(Issue)
Chris@0 201 i_start_date = (i.start_date >= @date_from ? i.start_date : @date_from )
Chris@0 202 i_end_date = (i.due_before <= date_to ? i.due_before : date_to )
Chris@0 203 i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor
Chris@0 204 i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date )
Chris@0 205 i_done_date = (i_done_date >= date_to ? date_to : i_done_date )
Chris@0 206 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
Chris@0 207
Chris@0 208 i_left = subject_width + ((i_start_date - @date_from)*zoom).floor
Chris@0 209 i_width = ((i_end_date - i_start_date + 1)*zoom).floor # total width of the issue
Chris@0 210 d_width = ((i_done_date - i_start_date)*zoom).floor # done width
Chris@0 211 l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor : 0 # delay width
Chris@0 212
Chris@0 213 gc.fill('grey')
Chris@0 214 gc.rectangle(i_left, top, i_left + i_width, top - 6)
Chris@0 215 gc.fill('red')
Chris@0 216 gc.rectangle(i_left, top, i_left + l_width, top - 6) if l_width > 0
Chris@0 217 gc.fill('blue')
Chris@0 218 gc.rectangle(i_left, top, i_left + d_width, top - 6) if d_width > 0
Chris@0 219 gc.fill('black')
Chris@0 220 gc.text(i_left + i_width + 5,top + 1, "#{i.status.name} #{i.done_ratio}%")
Chris@0 221 else
Chris@0 222 i_left = subject_width + ((i.start_date - @date_from)*zoom).floor
Chris@0 223 gc.fill('green')
Chris@0 224 gc.rectangle(i_left, top, i_left + 6, top - 6)
Chris@0 225 gc.fill('black')
Chris@0 226 gc.text(i_left + 11, top + 1, i.name)
Chris@0 227 end
Chris@0 228 top = top + 20
Chris@0 229 end
Chris@0 230
Chris@0 231 # today red line
Chris@0 232 if Date.today >= @date_from and Date.today <= date_to
Chris@0 233 gc.stroke('red')
Chris@0 234 x = (Date.today-@date_from+1)*zoom + subject_width
Chris@0 235 gc.line(x, headers_heigth, x, headers_heigth + g_height-1)
Chris@0 236 end
Chris@0 237
Chris@0 238 gc.draw(imgl)
Chris@0 239 imgl.format = format
Chris@0 240 imgl.to_blob
Chris@0 241 end if Object.const_defined?(:Magick)
Chris@0 242
Chris@0 243 private
Chris@0 244
Chris@0 245 def gantt_issue_compare(x, y, issues)
Chris@0 246 if x.parent_id == y.parent_id
Chris@0 247 gantt_start_compare(x, y)
Chris@0 248 elsif x.is_ancestor_of?(y)
Chris@0 249 -1
Chris@0 250 elsif y.is_ancestor_of?(x)
Chris@0 251 1
Chris@0 252 else
Chris@0 253 ax = issues.select {|i| i.is_a?(Issue) && i.is_ancestor_of?(x) && !i.is_ancestor_of?(y) }.sort_by(&:lft).first
Chris@0 254 ay = issues.select {|i| i.is_a?(Issue) && i.is_ancestor_of?(y) && !i.is_ancestor_of?(x) }.sort_by(&:lft).first
Chris@0 255 if ax.nil? && ay.nil?
Chris@0 256 gantt_start_compare(x, y)
Chris@0 257 else
Chris@0 258 gantt_issue_compare(ax || x, ay || y, issues)
Chris@0 259 end
Chris@0 260 end
Chris@0 261 end
Chris@0 262
Chris@0 263 def gantt_start_compare(x, y)
Chris@0 264 if x.start_date.nil?
Chris@0 265 -1
Chris@0 266 elsif y.start_date.nil?
Chris@0 267 1
Chris@0 268 else
Chris@0 269 x.start_date <=> y.start_date
Chris@0 270 end
Chris@0 271 end
Chris@0 272 end
Chris@0 273 end
Chris@0 274 end