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