##// END OF EJS Templates
Gantt: fixes position of line in pdf (#6348)....
Jean-Philippe Lang -
r4361:318bd10c7f4d
parent child
Show More
@@ -1,960 +1,967
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 include ERB::Util
23 23 include Redmine::I18n
24 24
25 25 # :nodoc:
26 26 # Some utility methods for the PDF export
27 27 class PDF
28 28 MaxCharactorsForSubject = 45
29 29 TotalWidth = 280
30 30 LeftPaneWidth = 100
31 31
32 32 def self.right_pane_width
33 33 TotalWidth - LeftPaneWidth
34 34 end
35 35 end
36 36
37 37 attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months
38 38 attr_accessor :query
39 39 attr_accessor :project
40 40 attr_accessor :view
41 41
42 42 def initialize(options={})
43 43 options = options.dup
44 44
45 45 if options[:year] && options[:year].to_i >0
46 46 @year_from = options[:year].to_i
47 47 if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12
48 48 @month_from = options[:month].to_i
49 49 else
50 50 @month_from = 1
51 51 end
52 52 else
53 53 @month_from ||= Date.today.month
54 54 @year_from ||= Date.today.year
55 55 end
56 56
57 57 zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i
58 58 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
59 59 months = (options[:months] || User.current.pref[:gantt_months]).to_i
60 60 @months = (months > 0 && months < 25) ? months : 6
61 61
62 62 # Save gantt parameters as user preference (zoom and months count)
63 63 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
64 64 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
65 65 User.current.preference.save
66 66 end
67 67
68 68 @date_from = Date.civil(@year_from, @month_from, 1)
69 69 @date_to = (@date_from >> @months) - 1
70 70
71 71 @subjects = ''
72 72 @lines = ''
73 73 end
74 74
75 75 def common_params
76 76 { :controller => 'gantts', :action => 'show', :project_id => @project }
77 77 end
78 78
79 79 def params
80 80 common_params.merge({ :zoom => zoom, :year => year_from, :month => month_from, :months => months })
81 81 end
82 82
83 83 def params_previous
84 84 common_params.merge({:year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months })
85 85 end
86 86
87 87 def params_next
88 88 common_params.merge({:year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months })
89 89 end
90 90
91 91 ### Extracted from the HTML view/helpers
92 92 # Returns the number of rows that will be rendered on the Gantt chart
93 93 def number_of_rows
94 94 if @project
95 95 return number_of_rows_on_project(@project)
96 96 else
97 97 Project.roots.visible.inject(0) do |total, project|
98 98 total += number_of_rows_on_project(project)
99 99 end
100 100 end
101 101 end
102 102
103 103 # Returns the number of rows that will be used to list a project on
104 104 # the Gantt chart. This will recurse for each subproject.
105 105 def number_of_rows_on_project(project)
106 106 # Remove the project requirement for Versions because it will
107 107 # restrict issues to only be on the current project. This
108 108 # ends up missing issues which are assigned to shared versions.
109 109 @query.project = nil if @query.project
110 110
111 111 # One Root project
112 112 count = 1
113 113 # Issues without a Version
114 114 count += project.issues.for_gantt.without_version.with_query(@query).count
115 115
116 116 # Versions
117 117 count += project.versions.count
118 118
119 119 # Issues on the Versions
120 120 project.versions.each do |version|
121 121 count += version.fixed_issues.for_gantt.with_query(@query).count
122 122 end
123 123
124 124 # Subprojects
125 125 project.children.visible.each do |subproject|
126 126 count += number_of_rows_on_project(subproject)
127 127 end
128 128
129 129 count
130 130 end
131 131
132 132 # Renders the subjects of the Gantt chart, the left side.
133 133 def subjects(options={})
134 134 render(options.merge(:only => :subjects)) unless @subjects_rendered
135 135 @subjects
136 136 end
137 137
138 138 # Renders the lines of the Gantt chart, the right side
139 139 def lines(options={})
140 140 render(options.merge(:only => :lines)) unless @lines_rendered
141 141 @lines
142 142 end
143 143
144 144 def render(options={})
145 145 options = {:indent => 4, :render => :subject, :format => :html}.merge(options)
146 146
147 147 @subjects = '' unless options[:only] == :lines
148 148 @lines = '' unless options[:only] == :subjects
149 149
150 150 if @project
151 151 render_project(@project, options)
152 152 else
153 153 Project.roots.visible.each do |project|
154 154 render_project(project, options)
155 155 end
156 156 end
157 157
158 158 @subjects_rendered = true unless options[:only] == :lines
159 159 @lines_rendered = true unless options[:only] == :subjects
160
161 render_end(options)
160 162 end
161 163
162 164 def render_project(project, options={})
163 165 options[:top] = 0 unless options.key? :top
164 166 options[:indent_increment] = 20 unless options.key? :indent_increment
165 167 options[:top_increment] = 20 unless options.key? :top_increment
166 168
167 169 subject_for_project(project, options) unless options[:only] == :lines
168 170 line_for_project(project, options) unless options[:only] == :subjects
169 171
170 172 options[:top] += options[:top_increment]
171 173 options[:indent] += options[:indent_increment]
172 174
173 175 # Second, Issues without a version
174 176 issues = project.issues.for_gantt.without_version.with_query(@query)
175 177 sort_issues!(issues)
176 178 if issues
177 179 render_issues(issues, options)
178 180 end
179 181
180 182 # Third, Versions
181 183 project.versions.sort.each do |version|
182 184 render_version(version, options)
183 185 end
184 186
185 187 # Fourth, subprojects
186 188 project.children.visible.each do |project|
187 189 render_project(project, options)
188 190 end
189 191
190 192 # Remove indent to hit the next sibling
191 193 options[:indent] -= options[:indent_increment]
192 194 end
193 195
194 196 def render_issues(issues, options={})
195 197 issues.each do |i|
196 198 subject_for_issue(i, options) unless options[:only] == :lines
197 199 line_for_issue(i, options) unless options[:only] == :subjects
198 200
199 201 options[:top] += options[:top_increment]
200 202 end
201 203 end
202 204
203 205 def render_version(version, options={})
204 206 # Version header
205 207 subject_for_version(version, options) unless options[:only] == :lines
206 208 line_for_version(version, options) unless options[:only] == :subjects
207 209
208 210 options[:top] += options[:top_increment]
209 211
210 212 # Remove the project requirement for Versions because it will
211 213 # restrict issues to only be on the current project. This
212 214 # ends up missing issues which are assigned to shared versions.
213 215 @query.project = nil if @query.project
214 216
215 217 issues = version.fixed_issues.for_gantt.with_query(@query)
216 218 if issues
217 219 sort_issues!(issues)
218 220 # Indent issues
219 221 options[:indent] += options[:indent_increment]
220 222 render_issues(issues, options)
221 223 options[:indent] -= options[:indent_increment]
222 224 end
223 225 end
226
227 def render_end(options={})
228 case options[:format]
229 when :pdf
230 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
231 end
232 end
224 233
225 234 def subject_for_project(project, options)
226 235 case options[:format]
227 236 when :html
228 237 output = ''
229 238
230 239 output << "<div class='project-name' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
231 240 if project.is_a? Project
232 241 output << "<span class='icon icon-projects #{project.overdue? ? 'project-overdue' : ''}'>"
233 242 output << view.link_to_project(project)
234 243 output << '</span>'
235 244 else
236 245 ActiveRecord::Base.logger.debug "Gantt#subject_for_project was not given a project"
237 246 ''
238 247 end
239 248 output << "</small></div>"
240 249 @subjects << output
241 250 output
242 251 when :image
243 252
244 253 options[:image].fill('black')
245 254 options[:image].stroke('transparent')
246 255 options[:image].stroke_width(1)
247 256 options[:image].text(options[:indent], options[:top] + 2, project.name)
248 257 when :pdf
249 258 pdf_new_page?(options)
250 259 options[:pdf].SetY(options[:top])
251 260 options[:pdf].SetX(15)
252 261
253 262 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
254 263 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{project.name}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
255 264
256 265 options[:pdf].SetY(options[:top])
257 266 options[:pdf].SetX(options[:subject_width])
258 267 options[:pdf].Cell(options[:g_width], 5, "", "LR")
259 268 end
260 269 end
261 270
262 271 def line_for_project(project, options)
263 272 # Skip versions that don't have a start_date or due date
264 273 if project.is_a?(Project) && project.start_date && project.due_date
265 274 options[:zoom] ||= 1
266 275 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
267 276
268 277
269 278 case options[:format]
270 279 when :html
271 280 output = ''
272 281 i_left = ((project.start_date - self.date_from)*options[:zoom]).floor
273 282
274 283 start_date = project.start_date
275 284 start_date ||= self.date_from
276 285 start_left = ((start_date - self.date_from)*options[:zoom]).floor
277 286
278 287 i_end_date = ((project.due_date <= self.date_to) ? project.due_date : self.date_to )
279 288 i_done_date = start_date + ((project.due_date - start_date+1)* project.completed_percent(:include_subprojects => true)/100).floor
280 289 i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date )
281 290 i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date )
282 291
283 292 i_late_date = [i_end_date, Date.today].min if start_date < Date.today
284 293 i_end = ((i_end_date - self.date_from) * options[:zoom]).floor
285 294
286 295 i_width = (i_end - i_left + 1).floor - 2 # total width of the issue (- 2 for left and right borders)
287 296 d_width = ((i_done_date - start_date)*options[:zoom]).floor - 2 # done width
288 297 l_width = i_late_date ? ((i_late_date - start_date+1)*options[:zoom]).floor - 2 : 0 # delay width
289 298
290 299 # Bar graphic
291 300
292 301 # Make sure that negative i_left and i_width don't
293 302 # overflow the subject
294 303 if i_end > 0 && i_left <= options[:g_width]
295 304 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ i_width }px;' class='task project_todo'>&nbsp;</div>"
296 305 end
297 306
298 307 if l_width > 0 && i_left <= options[:g_width]
299 308 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ l_width }px;' class='task project_late'>&nbsp;</div>"
300 309 end
301 310 if d_width > 0 && i_left <= options[:g_width]
302 311 output<< "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ d_width }px;' class='task project_done'>&nbsp;</div>"
303 312 end
304 313
305 314
306 315 # Starting diamond
307 316 if start_left <= options[:g_width] && start_left > 0
308 317 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:15px;' class='task project-line starting'>&nbsp;</div>"
309 318 output << "<div style='top:#{ options[:top] }px;left:#{ start_left + 12 }px;' class='task label'>"
310 319 output << "</div>"
311 320 end
312 321
313 322 # Ending diamond
314 323 # Don't show items too far ahead
315 324 if i_end <= options[:g_width] && i_end > 0
316 325 output << "<div style='top:#{ options[:top] }px;left:#{ i_end }px;width:15px;' class='task project-line ending'>&nbsp;</div>"
317 326 end
318 327
319 328 # DIsplay the Project name and %
320 329 if i_end <= options[:g_width]
321 330 # Display the status even if it's floated off to the left
322 331 status_px = i_end + 12 # 12px for the diamond
323 332 status_px = 0 if status_px <= 0
324 333
325 334 output << "<div style='top:#{ options[:top] }px;left:#{ status_px }px;' class='task label project-name'>"
326 335 output << "<strong>#{h project } #{h project.completed_percent(:include_subprojects => true).to_i.to_s}%</strong>"
327 336 output << "</div>"
328 337 end
329 338 @lines << output
330 339 output
331 340 when :image
332 341 options[:image].stroke('transparent')
333 342 i_left = options[:subject_width] + ((project.due_date - self.date_from)*options[:zoom]).floor
334 343
335 344 # Make sure negative i_left doesn't overflow the subject
336 345 if i_left > options[:subject_width]
337 346 options[:image].fill('blue')
338 347 options[:image].rectangle(i_left, options[:top], i_left + 6, options[:top] - 6)
339 348 options[:image].fill('black')
340 349 options[:image].text(i_left + 11, options[:top] + 1, project.name)
341 350 end
342 351 when :pdf
343 352 options[:pdf].SetY(options[:top]+1.5)
344 353 i_left = ((project.due_date - @date_from)*options[:zoom])
345 354
346 355 # Make sure negative i_left doesn't overflow the subject
347 356 if i_left > 0
348 357 options[:pdf].SetX(options[:subject_width] + i_left)
349 358 options[:pdf].SetFillColor(50,50,200)
350 359 options[:pdf].Cell(2, 2, "", 0, 0, "", 1)
351 360
352 361 options[:pdf].SetY(options[:top]+1.5)
353 362 options[:pdf].SetX(options[:subject_width] + i_left + 3)
354 363 options[:pdf].Cell(30, 2, "#{project.name}")
355 364 end
356 365 end
357 366 else
358 367 ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date"
359 368 ''
360 369 end
361 370 end
362 371
363 372 def subject_for_version(version, options)
364 373 case options[:format]
365 374 when :html
366 375 output = ''
367 376 output << "<div class='version-name' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
368 377 if version.is_a? Version
369 378 output << "<span class='icon icon-package #{version.behind_schedule? ? 'version-behind-schedule' : ''} #{version.overdue? ? 'version-overdue' : ''}'>"
370 379 output << view.link_to_version(version)
371 380 output << '</span>'
372 381 else
373 382 ActiveRecord::Base.logger.debug "Gantt#subject_for_version was not given a version"
374 383 ''
375 384 end
376 385 output << "</small></div>"
377 386 @subjects << output
378 387 output
379 388 when :image
380 389 options[:image].fill('black')
381 390 options[:image].stroke('transparent')
382 391 options[:image].stroke_width(1)
383 392 options[:image].text(options[:indent], options[:top] + 2, version.to_s_with_project)
384 393 when :pdf
385 394 pdf_new_page?(options)
386 395 options[:pdf].SetY(options[:top])
387 396 options[:pdf].SetX(15)
388 397
389 398 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
390 399 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{version.to_s_with_project}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
391 400
392 401 options[:pdf].SetY(options[:top])
393 402 options[:pdf].SetX(options[:subject_width])
394 403 options[:pdf].Cell(options[:g_width], 5, "", "LR")
395 404 end
396 405 end
397 406
398 407 def line_for_version(version, options)
399 408 # Skip versions that don't have a start_date
400 409 if version.is_a?(Version) && version.start_date && version.due_date
401 410 options[:zoom] ||= 1
402 411 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
403 412
404 413 case options[:format]
405 414 when :html
406 415 output = ''
407 416 i_left = ((version.start_date - self.date_from)*options[:zoom]).floor
408 417 # TODO: or version.fixed_issues.collect(&:start_date).min
409 418 start_date = version.fixed_issues.minimum('start_date') if version.fixed_issues.present?
410 419 start_date ||= self.date_from
411 420 start_left = ((start_date - self.date_from)*options[:zoom]).floor
412 421
413 422 i_end_date = ((version.due_date <= self.date_to) ? version.due_date : self.date_to )
414 423 i_done_date = start_date + ((version.due_date - start_date+1)* version.completed_pourcent/100).floor
415 424 i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date )
416 425 i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date )
417 426
418 427 i_late_date = [i_end_date, Date.today].min if start_date < Date.today
419 428
420 429 i_width = (i_left - start_left + 1).floor - 2 # total width of the issue (- 2 for left and right borders)
421 430 d_width = ((i_done_date - start_date)*options[:zoom]).floor - 2 # done width
422 431 l_width = i_late_date ? ((i_late_date - start_date+1)*options[:zoom]).floor - 2 : 0 # delay width
423 432
424 433 i_end = ((i_end_date - self.date_from) * options[:zoom]).floor # Ending pixel
425 434
426 435 # Bar graphic
427 436
428 437 # Make sure that negative i_left and i_width don't
429 438 # overflow the subject
430 439 if i_width > 0 && i_left <= options[:g_width]
431 440 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ i_width }px;' class='task milestone_todo'>&nbsp;</div>"
432 441 end
433 442 if l_width > 0 && i_left <= options[:g_width]
434 443 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ l_width }px;' class='task milestone_late'>&nbsp;</div>"
435 444 end
436 445 if d_width > 0 && i_left <= options[:g_width]
437 446 output<< "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ d_width }px;' class='task milestone_done'>&nbsp;</div>"
438 447 end
439 448
440 449
441 450 # Starting diamond
442 451 if start_left <= options[:g_width] && start_left > 0
443 452 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:15px;' class='task milestone starting'>&nbsp;</div>"
444 453 output << "<div style='top:#{ options[:top] }px;left:#{ start_left + 12 }px;background:#fff;' class='task'>"
445 454 output << "</div>"
446 455 end
447 456
448 457 # Ending diamond
449 458 # Don't show items too far ahead
450 459 if i_left <= options[:g_width] && i_end > 0
451 460 output << "<div style='top:#{ options[:top] }px;left:#{ i_end }px;width:15px;' class='task milestone ending'>&nbsp;</div>"
452 461 end
453 462
454 463 # Display the Version name and %
455 464 if i_end <= options[:g_width]
456 465 # Display the status even if it's floated off to the left
457 466 status_px = i_end + 12 # 12px for the diamond
458 467 status_px = 0 if status_px <= 0
459 468
460 469 output << "<div style='top:#{ options[:top] }px;left:#{ status_px }px;' class='task label version-name'>"
461 470 output << h("#{version.project} -") unless @project && @project == version.project
462 471 output << "<strong>#{h version } #{h version.completed_pourcent.to_i.to_s}%</strong>"
463 472 output << "</div>"
464 473 end
465 474 @lines << output
466 475 output
467 476 when :image
468 477 options[:image].stroke('transparent')
469 478 i_left = options[:subject_width] + ((version.start_date - @date_from)*options[:zoom]).floor
470 479
471 480 # Make sure negative i_left doesn't overflow the subject
472 481 if i_left > options[:subject_width]
473 482 options[:image].fill('green')
474 483 options[:image].rectangle(i_left, options[:top], i_left + 6, options[:top] - 6)
475 484 options[:image].fill('black')
476 485 options[:image].text(i_left + 11, options[:top] + 1, version.name)
477 486 end
478 487 when :pdf
479 488 options[:pdf].SetY(options[:top]+1.5)
480 489 i_left = ((version.start_date - @date_from)*options[:zoom])
481 490
482 491 # Make sure negative i_left doesn't overflow the subject
483 492 if i_left > 0
484 493 options[:pdf].SetX(options[:subject_width] + i_left)
485 494 options[:pdf].SetFillColor(50,200,50)
486 495 options[:pdf].Cell(2, 2, "", 0, 0, "", 1)
487 496
488 497 options[:pdf].SetY(options[:top]+1.5)
489 498 options[:pdf].SetX(options[:subject_width] + i_left + 3)
490 499 options[:pdf].Cell(30, 2, "#{version.name}")
491 500 end
492 501 end
493 502 else
494 503 ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date"
495 504 ''
496 505 end
497 506 end
498 507
499 508 def subject_for_issue(issue, options)
500 509 case options[:format]
501 510 when :html
502 511 output = ''
503 512 output << "<div class='tooltip'>"
504 513 output << "<div class='issue-subject' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
505 514 if issue.is_a? Issue
506 515 css_classes = []
507 516 css_classes << 'issue-overdue' if issue.overdue?
508 517 css_classes << 'issue-behind-schedule' if issue.behind_schedule?
509 518 css_classes << 'icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
510 519
511 520 if issue.assigned_to.present?
512 521 assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
513 522 output << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string)
514 523 end
515 524 output << "<span class='#{css_classes.join(' ')}'>"
516 525 output << view.link_to_issue(issue)
517 526 output << '</span>'
518 527 else
519 528 ActiveRecord::Base.logger.debug "Gantt#subject_for_issue was not given an issue"
520 529 ''
521 530 end
522 531 output << "</small></div>"
523 532
524 533 # Tooltip
525 534 if issue.is_a? Issue
526 535 output << "<span class='tip' style='position: absolute;top:#{ options[:top].to_i + 16 }px;left:#{ options[:indent].to_i + 20 }px;'>"
527 536 output << view.render_issue_tooltip(issue)
528 537 output << "</span>"
529 538 end
530 539
531 540 output << "</div>"
532 541 @subjects << output
533 542 output
534 543 when :image
535 544 options[:image].fill('black')
536 545 options[:image].stroke('transparent')
537 546 options[:image].stroke_width(1)
538 547 options[:image].text(options[:indent], options[:top] + 2, issue.subject)
539 548 when :pdf
540 549 pdf_new_page?(options)
541 550 options[:pdf].SetY(options[:top])
542 551 options[:pdf].SetX(15)
543 552
544 553 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
545 554 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{issue.tracker} #{issue.id}: #{issue.subject}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
546 555
547 556 options[:pdf].SetY(options[:top])
548 557 options[:pdf].SetX(options[:subject_width])
549 558 options[:pdf].Cell(options[:g_width], 5, "", "LR")
550 559 end
551 560 end
552 561
553 562 def line_for_issue(issue, options)
554 563 # Skip issues that don't have a due_before (due_date or version's due_date)
555 564 if issue.is_a?(Issue) && issue.due_before
556 565 case options[:format]
557 566 when :html
558 567 output = ''
559 568 # Handle nil start_dates, rare but can happen.
560 569 i_start_date = if issue.start_date && issue.start_date >= self.date_from
561 570 issue.start_date
562 571 else
563 572 self.date_from
564 573 end
565 574
566 575 i_end_date = ((issue.due_before && issue.due_before <= self.date_to) ? issue.due_before : self.date_to )
567 576 i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor
568 577 i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date )
569 578 i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date )
570 579
571 580 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
572 581
573 582 i_left = ((i_start_date - self.date_from)*options[:zoom]).floor
574 583 i_width = ((i_end_date - i_start_date + 1)*options[:zoom]).floor - 2 # total width of the issue (- 2 for left and right borders)
575 584 d_width = ((i_done_date - i_start_date)*options[:zoom]).floor - 2 # done width
576 585 l_width = i_late_date ? ((i_late_date - i_start_date+1)*options[:zoom]).floor - 2 : 0 # delay width
577 586 css = "task " + (issue.leaf? ? 'leaf' : 'parent')
578 587
579 588 # Make sure that negative i_left and i_width don't
580 589 # overflow the subject
581 590 if i_width > 0
582 591 output << "<div style='top:#{ options[:top] }px;left:#{ i_left }px;width:#{ i_width }px;' class='#{css} task_todo'>&nbsp;</div>"
583 592 end
584 593 if l_width > 0
585 594 output << "<div style='top:#{ options[:top] }px;left:#{ i_left }px;width:#{ l_width }px;' class='#{css} task_late'>&nbsp;</div>"
586 595 end
587 596 if d_width > 0
588 597 output<< "<div style='top:#{ options[:top] }px;left:#{ i_left }px;width:#{ d_width }px;' class='#{css} task_done'>&nbsp;</div>"
589 598 end
590 599
591 600 # Display the status even if it's floated off to the left
592 601 status_px = i_left + i_width + 5
593 602 status_px = 5 if status_px <= 0
594 603
595 604 output << "<div style='top:#{ options[:top] }px;left:#{ status_px }px;' class='#{css} label issue-name'>"
596 605 output << issue.status.name
597 606 output << ' '
598 607 output << (issue.done_ratio).to_i.to_s
599 608 output << "%"
600 609 output << "</div>"
601 610
602 611 output << "<div class='tooltip' style='position: absolute;top:#{ options[:top] }px;left:#{ i_left }px;width:#{ i_width }px;height:12px;'>"
603 612 output << '<span class="tip">'
604 613 output << view.render_issue_tooltip(issue)
605 614 output << "</span></div>"
606 615 @lines << output
607 616 output
608 617
609 618 when :image
610 619 # Handle nil start_dates, rare but can happen.
611 620 i_start_date = if issue.start_date && issue.start_date >= @date_from
612 621 issue.start_date
613 622 else
614 623 @date_from
615 624 end
616 625
617 626 i_end_date = (issue.due_before <= date_to ? issue.due_before : date_to )
618 627 i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor
619 628 i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date )
620 629 i_done_date = (i_done_date >= date_to ? date_to : i_done_date )
621 630 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
622 631
623 632 i_left = options[:subject_width] + ((i_start_date - @date_from)*options[:zoom]).floor
624 633 i_width = ((i_end_date - i_start_date + 1)*options[:zoom]).floor # total width of the issue
625 634 d_width = ((i_done_date - i_start_date)*options[:zoom]).floor # done width
626 635 l_width = i_late_date ? ((i_late_date - i_start_date+1)*options[:zoom]).floor : 0 # delay width
627 636
628 637
629 638 # Make sure that negative i_left and i_width don't
630 639 # overflow the subject
631 640 if i_width > 0
632 641 options[:image].fill('grey')
633 642 options[:image].rectangle(i_left, options[:top], i_left + i_width, options[:top] - 6)
634 643 options[:image].fill('red')
635 644 options[:image].rectangle(i_left, options[:top], i_left + l_width, options[:top] - 6) if l_width > 0
636 645 options[:image].fill('blue')
637 646 options[:image].rectangle(i_left, options[:top], i_left + d_width, options[:top] - 6) if d_width > 0
638 647 end
639 648
640 649 # Show the status and % done next to the subject if it overflows
641 650 options[:image].fill('black')
642 651 if i_width > 0
643 652 options[:image].text(i_left + i_width + 5,options[:top] + 1, "#{issue.status.name} #{issue.done_ratio}%")
644 653 else
645 654 options[:image].text(options[:subject_width] + 5,options[:top] + 1, "#{issue.status.name} #{issue.done_ratio}%")
646 655 end
647 656
648 657 when :pdf
649 658 options[:pdf].SetY(options[:top]+1.5)
650 659 # Handle nil start_dates, rare but can happen.
651 660 i_start_date = if issue.start_date && issue.start_date >= @date_from
652 661 issue.start_date
653 662 else
654 663 @date_from
655 664 end
656 665
657 666 i_end_date = (issue.due_before <= @date_to ? issue.due_before : @date_to )
658 667
659 668 i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor
660 669 i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date )
661 670 i_done_date = (i_done_date >= @date_to ? @date_to : i_done_date )
662 671
663 672 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
664 673
665 674 i_left = ((i_start_date - @date_from)*options[:zoom])
666 675 i_width = ((i_end_date - i_start_date + 1)*options[:zoom])
667 676 d_width = ((i_done_date - i_start_date)*options[:zoom])
668 677 l_width = ((i_late_date - i_start_date+1)*options[:zoom]) if i_late_date
669 678 l_width ||= 0
670 679
671 680 # Make sure that negative i_left and i_width don't
672 681 # overflow the subject
673 682 if i_width > 0
674 683 options[:pdf].SetX(options[:subject_width] + i_left)
675 684 options[:pdf].SetFillColor(200,200,200)
676 685 options[:pdf].Cell(i_width, 2, "", 0, 0, "", 1)
677 686 end
678 687
679 688 if l_width > 0
680 689 options[:pdf].SetY(options[:top]+1.5)
681 690 options[:pdf].SetX(options[:subject_width] + i_left)
682 691 options[:pdf].SetFillColor(255,100,100)
683 692 options[:pdf].Cell(l_width, 2, "", 0, 0, "", 1)
684 693 end
685 694 if d_width > 0
686 695 options[:pdf].SetY(options[:top]+1.5)
687 696 options[:pdf].SetX(options[:subject_width] + i_left)
688 697 options[:pdf].SetFillColor(100,100,255)
689 698 options[:pdf].Cell(d_width, 2, "", 0, 0, "", 1)
690 699 end
691 700
692 701 options[:pdf].SetY(options[:top]+1.5)
693 702
694 703 # Make sure that negative i_left and i_width don't
695 704 # overflow the subject
696 705 if (i_left + i_width) >= 0
697 706 options[:pdf].SetX(options[:subject_width] + i_left + i_width)
698 707 else
699 708 options[:pdf].SetX(options[:subject_width])
700 709 end
701 710 options[:pdf].Cell(30, 2, "#{issue.status} #{issue.done_ratio}%")
702 711 end
703 712 else
704 713 ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before"
705 714 ''
706 715 end
707 716 end
708 717
709 718 # Generates a gantt image
710 719 # Only defined if RMagick is avalaible
711 720 def to_image(format='PNG')
712 721 date_to = (@date_from >> @months)-1
713 722 show_weeks = @zoom > 1
714 723 show_days = @zoom > 2
715 724
716 725 subject_width = 400
717 726 header_heigth = 18
718 727 # width of one day in pixels
719 728 zoom = @zoom*2
720 729 g_width = (@date_to - @date_from + 1)*zoom
721 730 g_height = 20 * number_of_rows + 30
722 731 headers_heigth = (show_weeks ? 2*header_heigth : header_heigth)
723 732 height = g_height + headers_heigth
724 733
725 734 imgl = Magick::ImageList.new
726 735 imgl.new_image(subject_width+g_width+1, height)
727 736 gc = Magick::Draw.new
728 737
729 738 # Subjects
730 739 subjects(:image => gc, :top => (headers_heigth + 20), :indent => 4, :format => :image)
731 740
732 741 # Months headers
733 742 month_f = @date_from
734 743 left = subject_width
735 744 @months.times do
736 745 width = ((month_f >> 1) - month_f) * zoom
737 746 gc.fill('white')
738 747 gc.stroke('grey')
739 748 gc.stroke_width(1)
740 749 gc.rectangle(left, 0, left + width, height)
741 750 gc.fill('black')
742 751 gc.stroke('transparent')
743 752 gc.stroke_width(1)
744 753 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
745 754 left = left + width
746 755 month_f = month_f >> 1
747 756 end
748 757
749 758 # Weeks headers
750 759 if show_weeks
751 760 left = subject_width
752 761 height = header_heigth
753 762 if @date_from.cwday == 1
754 763 # date_from is monday
755 764 week_f = date_from
756 765 else
757 766 # find next monday after date_from
758 767 week_f = @date_from + (7 - @date_from.cwday + 1)
759 768 width = (7 - @date_from.cwday + 1) * zoom
760 769 gc.fill('white')
761 770 gc.stroke('grey')
762 771 gc.stroke_width(1)
763 772 gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1)
764 773 left = left + width
765 774 end
766 775 while week_f <= date_to
767 776 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
768 777 gc.fill('white')
769 778 gc.stroke('grey')
770 779 gc.stroke_width(1)
771 780 gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1)
772 781 gc.fill('black')
773 782 gc.stroke('transparent')
774 783 gc.stroke_width(1)
775 784 gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s)
776 785 left = left + width
777 786 week_f = week_f+7
778 787 end
779 788 end
780 789
781 790 # Days details (week-end in grey)
782 791 if show_days
783 792 left = subject_width
784 793 height = g_height + header_heigth - 1
785 794 wday = @date_from.cwday
786 795 (date_to - @date_from + 1).to_i.times do
787 796 width = zoom
788 797 gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white')
789 798 gc.stroke('grey')
790 799 gc.stroke_width(1)
791 800 gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1)
792 801 left = left + width
793 802 wday = wday + 1
794 803 wday = 1 if wday > 7
795 804 end
796 805 end
797 806
798 807 # border
799 808 gc.fill('transparent')
800 809 gc.stroke('grey')
801 810 gc.stroke_width(1)
802 811 gc.rectangle(0, 0, subject_width+g_width, headers_heigth)
803 812 gc.stroke('black')
804 813 gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1)
805 814
806 815 # content
807 816 top = headers_heigth + 20
808 817
809 818 lines(:image => gc, :top => top, :zoom => zoom, :subject_width => subject_width, :format => :image)
810 819
811 820 # today red line
812 821 if Date.today >= @date_from and Date.today <= date_to
813 822 gc.stroke('red')
814 823 x = (Date.today-@date_from+1)*zoom + subject_width
815 824 gc.line(x, headers_heigth, x, headers_heigth + g_height-1)
816 825 end
817 826
818 827 gc.draw(imgl)
819 828 imgl.format = format
820 829 imgl.to_blob
821 830 end if Object.const_defined?(:Magick)
822 831
823 832 def to_pdf
824 833 pdf = ::Redmine::Export::PDF::IFPDF.new(current_language)
825 834 pdf.SetTitle("#{l(:label_gantt)} #{project}")
826 835 pdf.AliasNbPages
827 836 pdf.footer_date = format_date(Date.today)
828 837 pdf.AddPage("L")
829 838 pdf.SetFontStyle('B',12)
830 839 pdf.SetX(15)
831 840 pdf.Cell(PDF::LeftPaneWidth, 20, project.to_s)
832 841 pdf.Ln
833 842 pdf.SetFontStyle('B',9)
834 843
835 844 subject_width = PDF::LeftPaneWidth
836 845 header_heigth = 5
837 846
838 847 headers_heigth = header_heigth
839 848 show_weeks = false
840 849 show_days = false
841 850
842 851 if self.months < 7
843 852 show_weeks = true
844 853 headers_heigth = 2*header_heigth
845 854 if self.months < 3
846 855 show_days = true
847 856 headers_heigth = 3*header_heigth
848 857 end
849 858 end
850 859
851 860 g_width = PDF.right_pane_width
852 861 zoom = (g_width) / (self.date_to - self.date_from + 1)
853 862 g_height = 120
854 863 t_height = g_height + headers_heigth
855 864
856 865 y_start = pdf.GetY
857 866
858 867 # Months headers
859 868 month_f = self.date_from
860 869 left = subject_width
861 870 height = header_heigth
862 871 self.months.times do
863 872 width = ((month_f >> 1) - month_f) * zoom
864 873 pdf.SetY(y_start)
865 874 pdf.SetX(left)
866 875 pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
867 876 left = left + width
868 877 month_f = month_f >> 1
869 878 end
870 879
871 880 # Weeks headers
872 881 if show_weeks
873 882 left = subject_width
874 883 height = header_heigth
875 884 if self.date_from.cwday == 1
876 885 # self.date_from is monday
877 886 week_f = self.date_from
878 887 else
879 888 # find next monday after self.date_from
880 889 week_f = self.date_from + (7 - self.date_from.cwday + 1)
881 890 width = (7 - self.date_from.cwday + 1) * zoom-1
882 891 pdf.SetY(y_start + header_heigth)
883 892 pdf.SetX(left)
884 893 pdf.Cell(width + 1, height, "", "LTR")
885 894 left = left + width+1
886 895 end
887 896 while week_f <= self.date_to
888 897 width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom
889 898 pdf.SetY(y_start + header_heigth)
890 899 pdf.SetX(left)
891 900 pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
892 901 left = left + width
893 902 week_f = week_f+7
894 903 end
895 904 end
896 905
897 906 # Days headers
898 907 if show_days
899 908 left = subject_width
900 909 height = header_heigth
901 910 wday = self.date_from.cwday
902 911 pdf.SetFontStyle('B',7)
903 912 (self.date_to - self.date_from + 1).to_i.times do
904 913 width = zoom
905 914 pdf.SetY(y_start + 2 * header_heigth)
906 915 pdf.SetX(left)
907 916 pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C")
908 917 left = left + width
909 918 wday = wday + 1
910 919 wday = 1 if wday > 7
911 920 end
912 921 end
913 922
914 923 pdf.SetY(y_start)
915 924 pdf.SetX(15)
916 925 pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1)
917 926
918 927 # Tasks
919 928 top = headers_heigth + y_start
920 929 options = {
921 930 :top => top,
922 931 :zoom => zoom,
923 932 :subject_width => subject_width,
924 933 :g_width => g_width,
925 934 :indent => 0,
926 935 :indent_increment => 5,
927 936 :top_increment => 5,
928 937 :format => :pdf,
929 938 :pdf => pdf
930 939 }
931 940 render(options)
932
933 pdf.Line(15, top, subject_width+g_width, top)
934 941 pdf.Output
935 942 end
936 943
937 944 private
938 945
939 946 # Sorts a collection of issues by start_date, due_date, id for gantt rendering
940 947 def sort_issues!(issues)
941 948 issues.sort! do |a, b|
942 949 cmp = 0
943 950 cmp = (a.start_date <=> b.start_date) if a.start_date? && b.start_date?
944 951 cmp = (a.due_date <=> b.due_date) if cmp == 0 && a.due_date? && b.due_date?
945 952 cmp = (a.id <=> b.id) if cmp == 0
946 953 cmp
947 954 end
948 955 end
949 956
950 957 def pdf_new_page?(options)
951 958 if options[:top] > 180
952 959 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
953 960 options[:pdf].AddPage("L")
954 961 options[:top] = 15
955 962 options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1)
956 963 end
957 964 end
958 965 end
959 966 end
960 967 end
General Comments 0
You need to be logged in to leave comments. Login now