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