annotate lib/redmine/helpers/gantt.rb @ 905:a97ce6b60fde yuya

Close obsolete branch yuya
author Chris Cannam
date Sat, 18 Sep 2010 17:38:00 +0100
parents cca12e1c1fd4
children 40f7cfd4df19
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@1 94 def to_image(project, 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@1 99 subject_width = 400
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@1 118 text = ""
Chris@1 119 if i.is_a? Issue
Chris@1 120 text = "#{i.tracker} #{i.id}: #{i.subject}"
Chris@1 121 else
Chris@1 122 text = i.name
Chris@1 123 end
Chris@1 124 text = "#{i.project} - #{text}" unless project && project == i.project
Chris@1 125 gc.text(4, top + 2, text)
Chris@0 126 top = top + 20
Chris@0 127 end
Chris@0 128
Chris@0 129 # Months headers
Chris@0 130 month_f = @date_from
Chris@0 131 left = subject_width
Chris@0 132 @months.times do
Chris@0 133 width = ((month_f >> 1) - month_f) * zoom
Chris@0 134 gc.fill('white')
Chris@0 135 gc.stroke('grey')
Chris@0 136 gc.stroke_width(1)
Chris@0 137 gc.rectangle(left, 0, left + width, height)
Chris@0 138 gc.fill('black')
Chris@0 139 gc.stroke('transparent')
Chris@0 140 gc.stroke_width(1)
Chris@0 141 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
Chris@0 142 left = left + width
Chris@0 143 month_f = month_f >> 1
Chris@0 144 end
Chris@0 145
Chris@0 146 # Weeks headers
Chris@0 147 if show_weeks
Chris@0 148 left = subject_width
Chris@0 149 height = header_heigth
Chris@0 150 if @date_from.cwday == 1
Chris@0 151 # date_from is monday
Chris@0 152 week_f = date_from
Chris@0 153 else
Chris@0 154 # find next monday after date_from
Chris@0 155 week_f = @date_from + (7 - @date_from.cwday + 1)
Chris@0 156 width = (7 - @date_from.cwday + 1) * zoom
Chris@0 157 gc.fill('white')
Chris@0 158 gc.stroke('grey')
Chris@0 159 gc.stroke_width(1)
Chris@0 160 gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1)
Chris@0 161 left = left + width
Chris@0 162 end
Chris@0 163 while week_f <= date_to
Chris@0 164 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
Chris@0 165 gc.fill('white')
Chris@0 166 gc.stroke('grey')
Chris@0 167 gc.stroke_width(1)
Chris@0 168 gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1)
Chris@0 169 gc.fill('black')
Chris@0 170 gc.stroke('transparent')
Chris@0 171 gc.stroke_width(1)
Chris@0 172 gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s)
Chris@0 173 left = left + width
Chris@0 174 week_f = week_f+7
Chris@0 175 end
Chris@0 176 end
Chris@0 177
Chris@0 178 # Days details (week-end in grey)
Chris@0 179 if show_days
Chris@0 180 left = subject_width
Chris@0 181 height = g_height + header_heigth - 1
Chris@0 182 wday = @date_from.cwday
Chris@0 183 (date_to - @date_from + 1).to_i.times do
Chris@0 184 width = zoom
Chris@0 185 gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white')
Chris@0 186 gc.stroke('grey')
Chris@0 187 gc.stroke_width(1)
Chris@0 188 gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1)
Chris@0 189 left = left + width
Chris@0 190 wday = wday + 1
Chris@0 191 wday = 1 if wday > 7
Chris@0 192 end
Chris@0 193 end
Chris@0 194
Chris@0 195 # border
Chris@0 196 gc.fill('transparent')
Chris@0 197 gc.stroke('grey')
Chris@0 198 gc.stroke_width(1)
Chris@0 199 gc.rectangle(0, 0, subject_width+g_width, headers_heigth)
Chris@0 200 gc.stroke('black')
Chris@0 201 gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1)
Chris@0 202
Chris@0 203 # content
Chris@0 204 top = headers_heigth + 20
Chris@0 205 gc.stroke('transparent')
Chris@0 206 events.each do |i|
Chris@0 207 if i.is_a?(Issue)
Chris@0 208 i_start_date = (i.start_date >= @date_from ? i.start_date : @date_from )
Chris@0 209 i_end_date = (i.due_before <= date_to ? i.due_before : date_to )
Chris@0 210 i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor
Chris@0 211 i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date )
Chris@0 212 i_done_date = (i_done_date >= date_to ? date_to : i_done_date )
Chris@0 213 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
Chris@0 214
Chris@0 215 i_left = subject_width + ((i_start_date - @date_from)*zoom).floor
Chris@0 216 i_width = ((i_end_date - i_start_date + 1)*zoom).floor # total width of the issue
Chris@0 217 d_width = ((i_done_date - i_start_date)*zoom).floor # done width
Chris@0 218 l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor : 0 # delay width
Chris@0 219
Chris@0 220 gc.fill('grey')
Chris@0 221 gc.rectangle(i_left, top, i_left + i_width, top - 6)
Chris@0 222 gc.fill('red')
Chris@0 223 gc.rectangle(i_left, top, i_left + l_width, top - 6) if l_width > 0
Chris@0 224 gc.fill('blue')
Chris@0 225 gc.rectangle(i_left, top, i_left + d_width, top - 6) if d_width > 0
Chris@0 226 gc.fill('black')
Chris@0 227 gc.text(i_left + i_width + 5,top + 1, "#{i.status.name} #{i.done_ratio}%")
Chris@0 228 else
Chris@0 229 i_left = subject_width + ((i.start_date - @date_from)*zoom).floor
Chris@0 230 gc.fill('green')
Chris@0 231 gc.rectangle(i_left, top, i_left + 6, top - 6)
Chris@0 232 gc.fill('black')
Chris@0 233 gc.text(i_left + 11, top + 1, i.name)
Chris@0 234 end
Chris@0 235 top = top + 20
Chris@0 236 end
Chris@0 237
Chris@0 238 # today red line
Chris@0 239 if Date.today >= @date_from and Date.today <= date_to
Chris@0 240 gc.stroke('red')
Chris@0 241 x = (Date.today-@date_from+1)*zoom + subject_width
Chris@0 242 gc.line(x, headers_heigth, x, headers_heigth + g_height-1)
Chris@0 243 end
Chris@0 244
Chris@0 245 gc.draw(imgl)
Chris@0 246 imgl.format = format
Chris@0 247 imgl.to_blob
Chris@0 248 end if Object.const_defined?(:Magick)
Chris@0 249
Chris@0 250 private
Chris@0 251
Chris@0 252 def gantt_issue_compare(x, y, issues)
Chris@0 253 if x.parent_id == y.parent_id
Chris@0 254 gantt_start_compare(x, y)
Chris@0 255 elsif x.is_ancestor_of?(y)
Chris@0 256 -1
Chris@0 257 elsif y.is_ancestor_of?(x)
Chris@0 258 1
Chris@0 259 else
Chris@0 260 ax = issues.select {|i| i.is_a?(Issue) && i.is_ancestor_of?(x) && !i.is_ancestor_of?(y) }.sort_by(&:lft).first
Chris@0 261 ay = issues.select {|i| i.is_a?(Issue) && i.is_ancestor_of?(y) && !i.is_ancestor_of?(x) }.sort_by(&:lft).first
Chris@0 262 if ax.nil? && ay.nil?
Chris@0 263 gantt_start_compare(x, y)
Chris@0 264 else
Chris@0 265 gantt_issue_compare(ax || x, ay || y, issues)
Chris@0 266 end
Chris@0 267 end
Chris@0 268 end
Chris@0 269
Chris@0 270 def gantt_start_compare(x, y)
Chris@0 271 if x.start_date.nil?
Chris@0 272 -1
Chris@0 273 elsif y.start_date.nil?
Chris@0 274 1
Chris@0 275 else
Chris@0 276 x.start_date <=> y.start_date
Chris@0 277 end
Chris@0 278 end
Chris@0 279 end
Chris@0 280 end
Chris@0 281 end