Class: Redmine::Helpers::Gantt
- Inherits:
-
Object
- Object
- Redmine::Helpers::Gantt
- Defined in:
- lib/redmine/helpers/gantt.rb
Overview
Simple class to handle gantt chart data
Instance Attribute Summary
-
- (Object) date_from
readonly
Returns the value of attribute date_from.
-
- (Object) date_to
readonly
Returns the value of attribute date_to.
-
- (Object) events
Returns the value of attribute events.
-
- (Object) month_from
readonly
Returns the value of attribute month_from.
-
- (Object) months
readonly
Returns the value of attribute months.
-
- (Object) year_from
readonly
Returns the value of attribute year_from.
-
- (Object) zoom
readonly
Returns the value of attribute zoom.
Instance Method Summary
-
- (Gantt) initialize(options = {})
constructor
A new instance of Gantt.
- - (Object) params
- - (Object) params_next
- - (Object) params_previous
-
- (Object) to_image(format = 'PNG')
Generates a gantt image Only defined if RMagick is avalaible.
Constructor Details
- (Gantt) initialize(options = {})
A new instance of Gantt
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/redmine/helpers/gantt.rb', line 24 def initialize(={}) = .dup @events = [] if [:year] && [:year].to_i >0 @year_from = [:year].to_i if [:month] && [:month].to_i >=1 && [:month].to_i <= 12 @month_from = [:month].to_i else @month_from = 1 end else @month_from ||= Date.today.month @year_from ||= Date.today.year end zoom = ([:zoom] || User.current.pref[:gantt_zoom]).to_i @zoom = (zoom > 0 && zoom < 5) ? zoom : 2 months = ([:months] || User.current.pref[:gantt_months]).to_i @months = (months > 0 && months < 25) ? months : 6 # Save gantt parameters as user preference (zoom and months count) if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months])) User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months User.current.preference.save end @date_from = Date.civil(@year_from, @month_from, 1) @date_to = (@date_from >> @months) - 1 end |
Instance Attribute Details
- (Object) date_from (readonly)
Returns the value of attribute date_from
22 23 24 |
# File 'lib/redmine/helpers/gantt.rb', line 22 def date_from @date_from end |
- (Object) date_to (readonly)
Returns the value of attribute date_to
22 23 24 |
# File 'lib/redmine/helpers/gantt.rb', line 22 def date_to @date_to end |
- (Object) events
Returns the value of attribute events
22 23 24 |
# File 'lib/redmine/helpers/gantt.rb', line 22 def events @events end |
- (Object) month_from (readonly)
Returns the value of attribute month_from
22 23 24 |
# File 'lib/redmine/helpers/gantt.rb', line 22 def month_from @month_from end |
- (Object) months (readonly)
Returns the value of attribute months
22 23 24 |
# File 'lib/redmine/helpers/gantt.rb', line 22 def months @months end |
- (Object) year_from (readonly)
Returns the value of attribute year_from
22 23 24 |
# File 'lib/redmine/helpers/gantt.rb', line 22 def year_from @year_from end |
- (Object) zoom (readonly)
Returns the value of attribute zoom
22 23 24 |
# File 'lib/redmine/helpers/gantt.rb', line 22 def zoom @zoom end |
Instance Method Details
- (Object) params
80 81 82 |
# File 'lib/redmine/helpers/gantt.rb', line 80 def params { :zoom => zoom, :year => year_from, :month => month_from, :months => months } end |
- (Object) params_next
88 89 90 |
# File 'lib/redmine/helpers/gantt.rb', line 88 def params_next { :year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months } end |
- (Object) params_previous
84 85 86 |
# File 'lib/redmine/helpers/gantt.rb', line 84 def params_previous { :year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months } end |
- (Object) to_image(format = 'PNG')
Generates a gantt image Only defined if RMagick is avalaible
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/redmine/helpers/gantt.rb', line 94 def to_image(format='PNG') date_to = (@date_from >> @months)-1 show_weeks = @zoom > 1 show_days = @zoom > 2 subject_width = 320 header_heigth = 18 # width of one day in pixels zoom = @zoom*2 g_width = (@date_to - @date_from + 1)*zoom g_height = 20 * events.length + 20 headers_heigth = (show_weeks ? 2*header_heigth : header_heigth) height = g_height + headers_heigth imgl = Magick::ImageList.new imgl.new_image(subject_width+g_width+1, height) gc = Magick::Draw.new # Subjects top = headers_heigth + 20 gc.fill('black') gc.stroke('transparent') gc.stroke_width(1) events.each do |i| gc.text(4, top + 2, (i.is_a?(Issue) ? i.subject : i.name)) top = top + 20 end # Months headers month_f = @date_from left = subject_width @months.times do width = ((month_f >> 1) - month_f) * zoom gc.fill('white') gc.stroke('grey') gc.stroke_width(1) gc.rectangle(left, 0, left + width, height) gc.fill('black') gc.stroke('transparent') gc.stroke_width(1) gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}") left = left + width month_f = month_f >> 1 end # Weeks headers if show_weeks left = subject_width height = header_heigth if @date_from.cwday == 1 # date_from is monday week_f = date_from else # find next monday after date_from week_f = @date_from + (7 - @date_from.cwday + 1) width = (7 - @date_from.cwday + 1) * zoom gc.fill('white') gc.stroke('grey') gc.stroke_width(1) gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1) left = left + width end while week_f <= date_to width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom gc.fill('white') gc.stroke('grey') gc.stroke_width(1) gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1) gc.fill('black') gc.stroke('transparent') gc.stroke_width(1) gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s) left = left + width week_f = week_f+7 end end # Days details (week-end in grey) if show_days left = subject_width height = g_height + header_heigth - 1 wday = @date_from.cwday (date_to - @date_from + 1).to_i.times do width = zoom gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white') gc.stroke('grey') gc.stroke_width(1) gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1) left = left + width wday = wday + 1 wday = 1 if wday > 7 end end # border gc.fill('transparent') gc.stroke('grey') gc.stroke_width(1) gc.rectangle(0, 0, subject_width+g_width, headers_heigth) gc.stroke('black') gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1) # content top = headers_heigth + 20 gc.stroke('transparent') events.each do |i| if i.is_a?(Issue) i_start_date = (i.start_date >= @date_from ? i.start_date : @date_from ) i_end_date = (i.due_before <= date_to ? i.due_before : date_to ) i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date ) i_done_date = (i_done_date >= date_to ? date_to : i_done_date ) i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today i_left = subject_width + ((i_start_date - @date_from)*zoom).floor i_width = ((i_end_date - i_start_date + 1)*zoom).floor # total width of the issue d_width = ((i_done_date - i_start_date)*zoom).floor # done width l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor : 0 # delay width gc.fill('grey') gc.rectangle(i_left, top, i_left + i_width, top - 6) gc.fill('red') gc.rectangle(i_left, top, i_left + l_width, top - 6) if l_width > 0 gc.fill('blue') gc.rectangle(i_left, top, i_left + d_width, top - 6) if d_width > 0 gc.fill('black') gc.text(i_left + i_width + 5,top + 1, "#{i.status.name} #{i.done_ratio}%") else i_left = subject_width + ((i.start_date - @date_from)*zoom).floor gc.fill('green') gc.rectangle(i_left, top, i_left + 6, top - 6) gc.fill('black') gc.text(i_left + 11, top + 1, i.name) end top = top + 20 end # today red line if Date.today >= @date_from and Date.today <= date_to gc.stroke('red') x = (Date.today-@date_from+1)*zoom + subject_width gc.line(x, headers_heigth, x, headers_heigth + g_height-1) end gc.draw(imgl) imgl.format = format imgl.to_blob end if Object.const_defined?(:Magick) |