Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

implement zoom levels

as-op67435ca226ba
diff --git a/app/models/work_package/pdf_export/gantt.rb b/app/models/work_package/pdf_export/gantt.rb
index 9b42633d2bb..8d795b0f7b7 100644
--- a/app/models/work_package/pdf_export/gantt.rb
+++ b/app/models/work_package/pdf_export/gantt.rb
@@ -28,85 +28,62 @@
module WorkPackage::PDFExport::Gantt
def write_work_packages_gantt!(work_packages, _)
- @gantt_mode = :day
- @gantt_text_column_width = pdf.bounds.width / 4
- @gant_column_width = 24
+ zoom_levels = [
+ [:day, 48],
+ [:day, 24],
+ [:day, 18],
+ [:month, 128],
+ [:month, 64],
+ [:month, 32],
+ [:month, 24],
+ [:quarter, 64],
+ [:quarter, 32],
+ [:quarter, 24]
+ ]
+ zoom = options[:zoom] || 1
+ @gantt_mode, @gant_column_width = zoom_levels[zoom.to_i - 1].nil? ? zoom_levels[1] : zoom_levels[zoom.to_i - 1]
@gantt_header_row_height = 30
@gant_row_height = 20
- @gant_cell_padding = 1
- @gant_bar_cell_padding = 4
+ @gant_text_cell_padding = 2
+ @gant_bar_cell_padding = 5
@gantt_filter_empty = false
@gantt_grid_color = "9b9ea3"
+ @gantt_text_column_width = pdf.bounds.width / 4
+ @gantt_columns_per_next_page = (pdf.bounds.width / @gant_column_width).floor
+ gantt_columns_space_next_page = pdf.bounds.width - @gantt_columns_per_next_page * @gant_column_width
+ # distribute space to the default column widths
+ @gant_column_width += gantt_columns_space_next_page / @gantt_columns_per_next_page
gantt_columns_width_first_page = pdf.bounds.width - @gantt_text_column_width
@gantt_columns_per_first_page = (gantt_columns_width_first_page / @gant_column_width).floor
@gantt_columns_per_next_page = (pdf.bounds.width / @gant_column_width).floor
-
- # add space right to the first column
+ # distribute space to the first column
@gantt_text_column_width = pdf.bounds.width - (@gantt_columns_per_first_page * @gant_column_width)
gant_rows_height = pdf.bounds.height - @gantt_header_row_height
@gantt_work_packages_per_page = (gant_rows_height / @gant_row_height).floor
-
- # add space bottom to the first row
+ # distribute space bottom to the first row
@gantt_header_row_height = pdf.bounds.height - (@gantt_work_packages_per_page * @gant_row_height)
pages = build_gantt_pages(work_packages)
- pages = pages.filter { |page| page[:columns].map { |c| c[:work_packages] }.flatten.any? } if @gantt_filter_empty
-
- # paint pages
- pages.each_with_index do |page, page_index|
- # paint column lines
- paint_gantt_grid(page)
- # paint header row
- paint_gantt_header_row(page)
- # paint work packages
- page[:work_packages].each_with_index do |wp, index|
- paint_gantt_wp(page, wp, index)
- end
- @pdf.start_new_page if page_index != pages.size - 1
+ if pages.find { |page| !page[:text_column] }.nil?
+ # if there are not enough columns for even the first page of horizontal pages => distribute space to all columns
+ nr_of_columns = pages.first[:columns].length
+ @gantt_text_column_width = pdf.bounds.width / 4
+ @gant_column_width = (pdf.bounds.width - @gantt_text_column_width) / nr_of_columns
+ @gantt_columns_per_first_page = nr_of_columns
+ @gantt_columns_per_next_page = nr_of_columns
+ pages = build_gantt_pages(work_packages)
end
- end
- private
-
- def build_gantt_page(dates, index, work_packages)
- {
- index: index,
- columns: dates.each_with_index.map do |date, col_index|
- build_gantt_column(col_index, date, work_packages)
- end,
- work_packages: work_packages
- }
+ pages = pages.filter { |page| page[:columns].map { |c| c[:work_packages] }.flatten.any? } if @gantt_filter_empty
+ paint_pages(pages)
end
- def wp_on_month?(work_package, date)
- start_date = work_package.start_date
- end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
- Range.new(Date.new(start_date.year, start_date.month, 1), Date.new(end_date.year, end_date.month, -1))
- .include?(date)
- end
+ private
- def wp_on_day?(work_package, date)
- start_date = work_package.start_date
- end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
- Range.new(start_date, end_date).include?(date)
- end
+ def build_gantt_variables
- def build_gantt_column(index, date, work_packages)
- work_packages_on_date =
- case @gantt_mode
- when :day
- work_packages.select { |work_package| wp_on_day?(work_package, date) }
- else
- # when :month
- work_packages.select { |work_package| wp_on_month?(work_package, date) }
- end
- {
- index: index,
- date: date,
- work_packages: work_packages_on_date
- }
end
def build_gantt_pages(work_packages)
@@ -120,6 +97,8 @@ module WorkPackage::PDFExport::Gantt
dates = case @gantt_mode
when :day
(start_date..end_date).to_a
+ when :quarter
+ (start_date..end_date).map { |d| [d.year, quarter_of_date(d)] }.uniq.map { |year, quarter| Date.new(year, quarter * 3, -1) }
else
# when :month
(start_date..end_date).map { |d| Date.new(d.year, d.month, -1) }.uniq
@@ -130,6 +109,55 @@ module WorkPackage::PDFExport::Gantt
.flatten
end
+ def build_gantt_header_span_cell(text, columns)
+ { text: text, x: columns.first[:x], width: columns.last[:x] + columns.last[:width] - columns.first[:x] }
+ end
+
+ def build_gantt_header_row(columns)
+ result = []
+
+ years = columns.map { |column| column[:date].year }.uniq
+ result << years.map do |year|
+ year_columns = columns.select { |column| column[:date].year == year }
+ build_gantt_header_span_cell(year.to_s, year_columns)
+ end
+
+ if [:quarter, :month].include?(@gantt_mode)
+ quarters = columns.map { |column| [quarter_of_date(column[:date]), column[:date].year] }.uniq
+ result << quarters.map do |quarter_tuple|
+ quarter, year = quarter_tuple
+ quarter_columns = columns.select { |column| column[:date].year == year && quarter_of_date(column[:date]) == quarter }
+ build_gantt_header_span_cell("Q#{quarter}", quarter_columns)
+ end
+ end
+
+ if [:day, :month].include?(@gantt_mode)
+ months = columns.map { |column| [column[:date].month, column[:date].year] }.uniq
+ result << months.map do |month_tuple|
+ month, year = month_tuple
+ month_columns = columns.select { |column| column[:date].year == year && column[:date].month == month }
+ build_gantt_header_span_cell(month_columns.first[:date].strftime("%b"), month_columns)
+ end
+ end
+
+ if @gantt_mode == :day
+ result << columns.map do |column|
+ build_gantt_header_span_cell(column[:date].day.to_s, [column])
+ end
+ end
+
+ cell_height = @gantt_header_row_height / result.length
+ result.each_with_index do |cell_row, index|
+ y = index * cell_height
+ cell_row.each do |cell|
+ cell[:y] = y
+ cell[:height] = cell_height
+ end
+ end
+
+ result.flatten
+ end
+
def build_gantt_horizontal_pages(work_packages, dates)
horizontal_pages_needed = [((dates.size - @gantt_columns_per_first_page).to_f / @gantt_columns_per_next_page.to_f).ceil, 0].max + 1
result = []
@@ -137,66 +165,155 @@ module WorkPackage::PDFExport::Gantt
result << build_gantt_page(dates_on_page, 0, work_packages)
list = (0..horizontal_pages_needed - 2)
- list.map do |index|
+ list.each do |index|
dates_on_page = dates.slice(@gantt_columns_per_first_page + index * @gantt_columns_per_next_page, @gantt_columns_per_next_page)
result << build_gantt_page(dates_on_page, index + 1, work_packages)
end
result
end
- def paint_gantt_wp_milestone(y, page, wp, paint_columns)
+ def build_gantt_page(dates, index, work_packages)
+ x = index == 0 ? @gantt_text_column_width : 0
+ columns = dates.each_with_index.map { |date, col_index| build_gantt_column(x + (col_index * @gant_column_width), date, work_packages) }
+ y = @gantt_header_row_height
+ shapes = work_packages.each_with_index.map do |work_package, row_index|
+ paint_columns = columns.filter { |column| column[:work_packages].include?(work_package) }
+ build_gantt_shape(y + (row_index * @gant_row_height), paint_columns, work_package) unless paint_columns.empty?
+ end.compact
+ header = build_gantt_header_row(columns)
+ {
+ text_column: index == 0,
+ width: x + (dates.size * @gant_column_width),
+ height: @gantt_header_row_height + (@gantt_work_packages_per_page * @gant_row_height),
+ columns: columns,
+ header: header,
+ shapes: shapes,
+ work_packages: work_packages
+ }
+ end
+
+ def build_gantt_shape(y, paint_columns, work_package)
paint_column_first = paint_columns.first
- offset = page[:index] == 0 ? @gantt_text_column_width : 0
- x = offset + (paint_column_first[:index] * @gant_column_width)
- center_x = @pdf.bounds.left + x + @gant_column_width / 2
- center_y = @pdf.bounds.top - y - @gant_row_height / 2
- diamond_size = @gant_column_width / 3
- pdf.rotate(45, origin: [center_x, center_y]) do
- paint_gant_rect(center_x - diamond_size / 2, center_y + diamond_size / 2, diamond_size, diamond_size, gantt_wp_color(wp))
+ paint_column_last = paint_columns.last
+ x = paint_column_first[:x]
+ width = paint_column_last[:x] + paint_column_last[:width] - paint_column_first[:x]
+ {
+ x: x,
+ y: y,
+ width: width,
+ work_package: work_package,
+ column_first: paint_column_first,
+ column_last: paint_column_last,
+ x1: x + calc_start_offset(work_package, paint_column_first[:date]),
+ x2: x + width - calc_end_offset(work_package, paint_columns.last[:date]),
+ y1: y + @gant_bar_cell_padding,
+ y2: y + @gant_row_height - @gant_bar_cell_padding,
+ type: work_package.milestone? ? :milestone : :bar,
+ color: gantt_wp_color(work_package)
+ }
+ end
+
+ def build_gantt_column(x, date, work_packages)
+ work_packages_on_date =
+ case @gantt_mode
+ when :day
+ work_packages.select { |work_package| wp_on_day?(work_package, date) }
+ when :quarter
+ work_packages.select { |work_package| wp_on_quarter?(work_package, date) }
+ else
+ # when :month
+ work_packages.select { |work_package| wp_on_month?(work_package, date) }
+ end
+ {
+ date: date,
+ x: x,
+ width: @gant_column_width,
+ work_packages: work_packages_on_date
+ }
+ end
+
+
+ def paint_pages(pages)
+ pages.each_with_index do |page, page_index|
+ paint_page(page)
+ # start a new page if not last
+ @pdf.start_new_page if page_index != pages.size - 1
end
end
- def paint_gant_rect(x, y, width, height, color)
- current_color = @pdf.fill_color
- @pdf.fill_color color
- @pdf.fill_rectangle([x, y], width, height)
- @pdf.fill_color = current_color
+ def paint_page(page)
+ # paint grid lines
+ paint_gantt_grid(page)
+ # paint header row
+ paint_gantt_header_row(page)
+ # paint work packages titles if first of horizontal pages
+ page[:work_packages].each_with_index { |wp, index| paint_gantt_wp(wp, index) } if page[:text_column]
+ # paint work packages shapes
+ page[:shapes].each { |shape| paint_gantt_shape(shape) }
end
- def paint_gantt_wp_bar(y, page, wp, paint_columns)
- paint_column_first = paint_columns.first
- paint_column_last = paint_columns.last
- start_offset = calc_start_offset(wp, paint_column_first[:date])
- end_offset = calc_end_offset(wp, paint_column_last[:date])
- offset = page[:index] == 0 ? @gantt_text_column_width : 0
- x1 = offset + (paint_column_first[:index] * @gant_column_width) + start_offset
- x2 = offset + ((paint_column_last[:index] + 1) * @gant_column_width) - end_offset
- paint_gant_rect(@pdf.bounds.left + x1, @pdf.bounds.top - y - @gant_bar_cell_padding, x2 - x1, @gant_row_height - @gant_bar_cell_padding * 2, gantt_wp_color(wp))
+ def paint_gantt_grid(page)
+ paint_gantt_line_v(0, page[:height], 0)
+ paint_gantt_line_v(0, page[:height], page[:width])
+ paint_gantt_line_h(0, page[:width], page[:height])
+ paint_gantt_line_v(0, page[:height], @gantt_text_column_width) if page[:text_column]
+ page[:columns].each { |column| paint_gantt_line_v(@gantt_header_row_height, page[:height], column[:x] + column[:width]) }
+ (0..@gantt_work_packages_per_page).each do |index|
+ paint_gantt_line_h(0, page[:width], @gantt_header_row_height + index * @gant_row_height)
+ end
end
- def gantt_wp_color(wp)
- wp.type.color.hexcode.sub("#", "")
+ def paint_gantt_header_row(page)
+ if page[:text_column]
+ paint_gantt_text_box(heading, 0, 0, @gantt_text_column_width, @gantt_header_row_height,
+ @gant_text_cell_padding * 2, 0, { size: 10, style: :bold })
+ paint_gantt_line_h(0, @gantt_text_column_width, 0)
+ end
+ page[:header].each do |cell|
+ paint_gantt_text_box(cell[:text], cell[:x], cell[:y], cell[:width], cell[:height], 0, 0, { size: 10, style: :bold, align: :center })
+ paint_gantt_line_h(cell[:x], cell[:x] + cell[:width], cell[:y])
+ paint_gantt_line_v(cell[:y], cell[:y] + cell[:height], cell[:x])
+ end
+ end
+
+ def paint_gantt_header_cell(text, columns, y, height)
+ x1 = columns.first[:x]
+ x2 = columns.last[:x] + columns.last[:width]
+ paint_gantt_text_box(text, x1, y, x2 - x1, height, 0, 0, { size: 8, style: :bold, align: :center })
+ paint_gantt_line_h(x1, x2, y)
+ paint_gantt_line_v(y, y + height, x1)
+ end
+
+ def paint_gantt_shape_bar(shape)
+ paint_gant_rect(shape[:x1], shape[:y1], [shape[:x2] - shape[:x1], 0.1].max, shape[:y2] - shape[:y1], shape[:color])
end
- def paint_gantt_wp(page, wp, index)
- y = @gantt_header_row_height + (index * @gant_row_height)
- # paint row line
- paint_gantt_line_h(0, @pdf.bounds.width, y + @gant_row_height)
- # paint row title
- if page[:index] == 0
- paint_gantt_text_box("#{wp.type} ##{wp.id} - #{wp.subject}", 0, y, @gantt_text_column_width, @gant_row_height,
- @gant_bar_cell_padding * 2, @gant_bar_cell_padding)
+ def paint_gantt_shape_milestone(shape)
+ diamond_size = [@gant_column_width, @gant_row_height].min / 2
+ diamond_half_size = diamond_size / 2
+ center_x = @gantt_mode == :day ? shape[:x] + @gant_column_width / 2 : shape[:x1] + diamond_half_size
+ center_y = shape[:y] + @gant_row_height / 2
+ pdf.rotate(45, origin: [@pdf.bounds.left + center_x, @pdf.bounds.top - center_y]) do
+ paint_gant_rect(center_x - diamond_half_size, center_y - diamond_half_size, diamond_size, diamond_size, shape[:color])
end
- # paint work package in gantt
- paint_columns = page[:columns].select { |column| column[:work_packages].include?(wp) }
- return if paint_columns.empty?
- if wp.milestone?
- paint_gantt_wp_milestone(y, page, wp, paint_columns)
+ end
+
+ def paint_gantt_shape(shape)
+ if shape[:type] == :milestone
+ paint_gantt_shape_milestone(shape)
else
- paint_gantt_wp_bar(y, page, wp, paint_columns)
+ paint_gantt_shape_bar(shape)
end
end
+ def paint_gantt_wp(wp, index)
+ paint_gantt_text_box(
+ "#{wp.type} ##{wp.id} - #{wp.subject}",
+ 0, @gantt_header_row_height + (index * @gant_row_height),
+ @gantt_text_column_width, @gant_row_height,
+ @gant_text_cell_padding * 2, @gant_text_cell_padding)
+ end
+
def paint_gantt_line(x, y, x2, y2)
@pdf.stroke do
@pdf.line_width = 0.5
@@ -213,17 +330,11 @@ module WorkPackage::PDFExport::Gantt
paint_gantt_line(@pdf.bounds.left + x, @pdf.bounds.top - y1, @pdf.bounds.left + x, @pdf.bounds.top - y2)
end
- def paint_gantt_grid(page)
- paint_gantt_line_v(0, @pdf.bounds.height, 0)
- paint_gantt_line_v(0, @pdf.bounds.height, @gantt_text_column_width) if page[:index] == 0
- offset = page[:index] == 0 ? @gantt_text_column_width : 0
- page[:columns].each_with_index do |_, index|
- paint_gantt_line_v(@gantt_header_row_height, @pdf.bounds.height, offset + ((index + 1) * @gant_column_width))
- end
- paint_gantt_line_h(0, @pdf.bounds.width, 0)
- paint_gantt_line_h(0, @pdf.bounds.width, @gantt_header_row_height)
- paint_gantt_line_h(0, @pdf.bounds.width, @pdf.bounds.height)
- paint_gantt_line_v(0, @pdf.bounds.height, @pdf.bounds.width)
+ def paint_gant_rect(x, y, width, height, color)
+ current_color = @pdf.fill_color
+ @pdf.fill_color color
+ @pdf.fill_rectangle([@pdf.bounds.left + x, @pdf.bounds.top - y], width, height)
+ @pdf.fill_color = current_color
end
def paint_gantt_text_box(text, x_offset, y_offset, width, height, padding_h, padding_v, additional_options = {})
@@ -233,85 +344,95 @@ module WorkPackage::PDFExport::Gantt
overflow: :shrink_to_fit, min_font_size: 5, valign: :center, size: 8, leading: 0, **additional_options)
end
- def paint_gantt_header_cell(text, page, columns, y, height)
- offset = page[:index] == 0 ? @gantt_text_column_width : 0
- x = offset + (columns.first[:index] * @gant_column_width)
- x2 = offset + ((columns.last[:index] + 1) * @gant_column_width)
- paint_gantt_text_box(text, x, y, columns.size * @gant_column_width, height,
- 0, 0, { size: 8, style: :bold, align: :center })
- paint_gantt_line_h(x, x2, y + height)
- paint_gantt_line_v(y, y + height, x2)
- end
-
- def paint_gantt_header_row(page)
- if page[:index] == 0
- paint_gantt_text_box(heading, 0, 0, @gantt_text_column_width, @gantt_header_row_height,
- @gant_bar_cell_padding * 2, 0, { size: 10, style: :bold })
- end
- height = @gantt_header_row_height / 3
- y = 0
- years = page[:columns].map { |column| column[:date].year }.uniq
- years.each do |year|
- year_columns = page[:columns].select { |column| column[:date].year == year }
- paint_gantt_header_cell(year.to_s, page, year_columns, y, height)
- end
- if @gantt_mode == :month
- y += height
- quarters = page[:columns].map { |column| [quarter_of_date(column[:date]), column[:date].year] }.uniq
- quarters.each do |quarter_tuple|
- quarter, year = quarter_tuple
- quarter_columns = page[:columns].select { |column| column[:date].year == year && quarter_of_date(column[:date]) == quarter }
- paint_gantt_header_cell("Q#{quarter}", page, quarter_columns, y, height)
- end
- end
+ def gantt_wp_color(wp)
+ wp.type.color.hexcode.sub("#", "")
+ end
- y += height
- months = page[:columns].map { |column| [column[:date].month, column[:date].year] }.uniq
- months.each do |month_tuple|
- month, year = month_tuple
- month_columns = page[:columns].select { |column| column[:date].year == year && column[:date].month == month }
- paint_gantt_header_cell(month_columns.first[:date].strftime("%b"), page, month_columns, y, height)
+ def calc_end_offset(wp, date)
+ case @gantt_mode
+ when :quarter
+ wp_date = wp.due_date.nil? ? Date.today : wp.due_date
+ quarter = quarter_of_date(date)
+ test_date = Date.new(date.year, (quarter * 3), -1)
+ return 0 if wp_date >= test_date
+
+ width_per_day = @gant_column_width.to_f / days_of_quarter(date)
+ day_in_quarter = day_in_quarter(wp_date)
+ @gant_column_width - day_in_quarter * width_per_day
+ when :month
+ wp_date = wp.due_date.nil? ? Date.today : wp.due_date
+ test_date = Date.new(date.year, date.month, -1)
+ return 0 if wp_date >= test_date
+
+ width_per_day = @gant_column_width.to_f / days_of_month(test_date)
+ day_in_month = wp_date.day
+ @gant_column_width - day_in_month * width_per_day
+ else
+ 0
end
+ end
- return unless @gantt_mode == :day
-
- y += height
- page[:columns].each_with_index do |column|
- paint_gantt_header_cell(column[:date].day.to_s, page, [column], y, height)
+ def calc_start_offset(wp, date)
+ case @gantt_mode
+ when :quarter
+ quarter = quarter_of_date(date)
+ test_date = Date.new(date.year, (quarter * 3) - 2, 1)
+ return 0 if wp.start_date <= test_date
+
+ width_per_day = @gant_column_width.to_f / days_of_quarter(date)
+ day_in_quarter = day_in_quarter(wp.start_date) - 1
+ day_in_quarter * width_per_day
+ when :month
+ test_date = Date.new(date.year, date.month, 1)
+ return 0 if wp.start_date <= test_date
+
+ width_per_day = @gant_column_width.to_f / days_of_month(date)
+ day_in_month = wp.start_date.day - 1
+ day_in_month * width_per_day
+ else
+ return 0
end
end
- def calc_end_offset(wp, date)
- return 0 if @gantt_mode == :day
-
- wp_date = wp.due_date.nil? ? Date.today : wp.due_date
- test_date = Date.new(date.year, date.month, -1)
- return 0 if wp_date >= test_date
+ def quarter_of_date(date)
+ (date.month / 3.0).ceil
+ end
- days = days_of_month(test_date)
- width_per_day = @gant_column_width.to_f / days.to_f
- day_in_month = wp_date.day
- @gant_column_width - (day_in_month * width_per_day)
+ def days_of_month(date)
+ Date.new(date.year, date.month, -1).day
end
- def calc_start_offset(wp, date)
- return 0 if @gantt_mode == :day
+ def day_in_quarter(date)
+ date.yday - Date.new(date.year, (quarter_of_date(date) * 3) - 2, 1).yday + 1
+ end
- test_date = Date.new(date.year, date.month, 1)
- return 0 if wp.start_date <= test_date
+ def days_of_quarter(date)
+ quarter = quarter_of_date(date)
+ days = 0
+ (1..3).each do |q|
+ days += days_of_month(Date.new(date.year, (quarter * 3) - 2 + q, 1))
+ end
+ days
+ end
- days = days_of_month(date)
- width_per_day = @gant_column_width.to_f / days.to_f
- day_in_month = wp.start_date.day - 1
- day_in_month * width_per_day
+ def wp_on_month?(work_package, date)
+ start_date = work_package.start_date
+ end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
+ Range.new(Date.new(start_date.year, start_date.month, 1), Date.new(end_date.year, end_date.month, -1))
+ .include?(date)
end
- def quarter_of_date(date)
- (date.month / 3.0).ceil
+ def wp_on_day?(work_package, date)
+ start_date = work_package.start_date
+ end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
+ Range.new(start_date, end_date).include?(date)
end
- def days_of_month(date)
- Date.new(date.year, date.month, -1).day
+ def wp_on_quarter?(work_package, date)
+ start_date = work_package.start_date
+ end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
+ Range.new(Date.new(start_date.year, (quarter_of_date(start_date) * 3) - 2, 1), Date.new(end_date.year, (quarter_of_date(end_date) * 3), -1))
+ .include?(date)
end
end