class TaskJuggler::GanttChart

This class represents an abstract (output format independent) Gantt chart. It provides generator functions that can transform the abstract form into formats such as HTML or SVG. The appearance of the chart depend on 3 variable: the report period, the geometrical width and the scale. The report period is always provided by the user. In addition the width or the scale can be provided. The non-provided value will then be calculated. So after the object has been created, the user must call generateByWidth or generateByResolution.



The height in pixels of a horizontal scrollbar on an HTML page. This value should be large enough to work for all browsers.



Public Class Methods

new(now, weekStartsMonday, columnDef, table = nil, markdate = nil) click to toggle source

Create the GanttChart object, but don’t do much right now. We still need more information about the chart before we can actually generate it. now is the date that should be used as current date. weekStartsMonday is true if the weeks should start on Mondays instead of Sundays. table is a reference to the TableReport that the chart is part of.

# File lib/taskjuggler/reports/GanttChart.rb, line 46
def initialize(now, weekStartsMonday, columnDef, table = nil, markdate = nil)
  # The start and end dates of the reported interval.
  @start = nil
  @end = nil
  @now = now
  @columnDef = columnDef
  @table = table
  @markdate = markdate

  # This defines the possible horizontal scales that the Gantt chart can
  # have. The scales differ in their resolution and the amount of detail
  # that is displayed. A scale is defined by its name. The _name_ must be
  # unique and can be used to select the scale. The _stepSize_ defines the
  # width of a scale step in pixels. The _stepsToFunc_ is a TjTime method
  # that determines the number of steps between 2 dates. _minTimeOff_
  # defines the minimum required length of an time-off interval that is
  # displayed in this scale.
  @@scales = [
    { 'name' => 'hour', 'stepSize' => 20, 'stepsToFunc' => :hoursTo,
      'minTimeOff' => 5 * 60 },
    { 'name' => 'day', 'stepSize' => 20, 'stepsToFunc' => :daysTo,
      'minTimeOff' => 6 * 60 * 60 },
    { 'name' => 'week', 'stepSize' => 20, 'stepsToFunc' => :weeksTo,
      'minTimeOff' => 24 * 60 * 60 },
    { 'name' => 'month', 'stepSize' => 35, 'stepsToFunc' => :monthsTo,
      'minTimeOff' => 5 * 24 * 60 * 60 },
    { 'name' => 'quarter', 'stepSize' => 28, 'stepsToFunc' => :quartersTo,
      'minTimeOff' => -1 },
    { 'name' => 'year', 'stepSize' => 20, 'stepsToFunc' => :yearsTo,
      'minTimeOff' => -1 }
  # This points to one of the scales above and marks the current scale.
  @scale = nil
  # The height of the chart (without the header)
  @height = 0
  # The width of the chart in pixels.
  @width = 0
  # The width of the view that the chart is presented in. If it's nil, the
  # view will be adapted to the width of the chart.
  @viewWidth = nil
  # True of the week starts on a Monday.
  @weekStartsMonday = weekStartsMonday

  # Reference to the GanttHeader object that models the chart header.
  @header = nil
  # The GanttLine objects that model the lines of the chart.
  @lines = []
  # The router for dependency lines.
  @router = nil
  # This dictionary stores primary task lines indexed by their task. To
  # handle multiple scenarios, the dictionary stored the lines in an Array.
  # This is used to generate dependency arrows.
  @tasks = {}
  # This is a list of the dependency lines. Each entry is an Array of [x, y]
  # coordinate pairs.
  @depArrows = []
  # This is the list of arrow heads used for the dependency arrows. It
  # contains an Array of [ x, y ] coordinates that mark the tip of the
  # arrow.
  @arrowHeads = []

Public Instance Methods

addTask(task, line) click to toggle source

Add a primary tasks line to the dictonary. task is a reference to the Task object and line is the corresponding primary ReportTableLine.

# File lib/taskjuggler/reports/GanttChart.rb, line 110
def addTask(task, line)
  if @tasks.include?(task)
    # Append the line to the existing lines.
    @tasks[task] << line
    # Add a new Array for this tasks and store the first line.
    @tasks[task] = [ line ]
dateToX(date) click to toggle source

Utility function that convers a date to the corresponding X-position in the Gantt chart.

# File lib/taskjuggler/reports/GanttChart.rb, line 211
def dateToX(date)
  ((@width / (@end - @start)) * (date - @start)).to_i
generateByScale(periodStart, periodEnd, scaleName) click to toggle source

Generate the actual chart data based on the report interval specified by periodStart and periodEnd as well as the name of the requested scale to be used. This function (or generateByWidth) must be called before any GanttLine objects are created for this chart.

# File lib/taskjuggler/reports/GanttChart.rb, line 132
def generateByScale(periodStart, periodEnd, scaleName)
  @start = periodStart
  @end = periodEnd
  @scale = scaleByName(scaleName)
  @stepSize = @scale['stepSize']
  steps = @start.send(@scale['stepsToFunc'], @end)
  @width = @stepSize * steps

  @header =, self)
generateByWidth(periodStart, periodEnd, width) click to toggle source
# File lib/taskjuggler/reports/GanttChart.rb, line 121
def generateByWidth(periodStart, periodEnd, width)
  @start = periodStart
  @end = periodEnd
  @width = width
  # TODO
hasScrollbar?() click to toggle source

Returns true if the chart includes a scrollbar.

# File lib/taskjuggler/reports/GanttChart.rb, line 225
def hasScrollbar?
  @viewWidth && (@viewWidth < @width)
to_csv(csv, startColumn) click to toggle source

This is a noop function.

# File lib/taskjuggler/reports/GanttChart.rb, line 204
def to_csv(csv, startColumn)
  # Can't put a Gantt chart into a CSV file.
to_html() click to toggle source

Convert the chart into an HTML representation.

# File lib/taskjuggler/reports/GanttChart.rb, line 144
def to_html

  # The chart is rendered into a cell that extends over the full height of
  # the table. No other cells for this column will be generated. In case
  # there is a scrollbar, the table will have an extra line to hold the
  # scrollbar.
  td ='td',
    'rowspan' => "#{2 + @lines.length + (hasScrollbar? ? 1 : 0)}",
    'style' => 'padding:0px; vertical-align:top;')
  # Now we generate two 'div's nested into each other. The first div is the
  # view. It may contain a scrollbar if the second div is wider than the
  # first one. In case we need a scrollbar The outer div is
  # SCROLLBARHEIGHT pixels heigher to hold the scrollbar. Unfortunately
  # this must be a hardcoded value even though the height of the scrollbar
  # varies from system to system. This value should be good enough for
  # most systems.
  td << (scrollDiv ='div', 'class' => 'tabback',
    'style' => 'position:relative; ' +
               "overflow:auto; " +
               "width:#{hasScrollbar? ? @viewWidth : @width}px; " +
               "height:#{@height +
                         (hasScrollbar? ? SCROLLBARHEIGHT : 0)}px;"))
  scrollDiv << (div ='div',
    'style' => "margin:0px; padding:0px; " +
               "position:absolute; overflow:hidden; " +
               "top:0px; left:0px; " +
               "width:#{@width}px; " +
               "height:#{@height}px; " +
  # Add the header.
  div << @header.to_html
  # These are the lines of the chart.
  @lines.each do |line|
    div << line.to_html

  # This is used for debugging and testing only.
  #div << @router.to_html

  # Render the dependency lines.
  @depArrows.each do |arrow|
    xx = yy = nil
    arrow.each do |x, y|
      if xx
        div << lineToHTML(xx, yy, x, y, 'depline')
      xx = x
      yy = y
  # And the corresponsing arrow heads.
  @arrowHeads.each do |x, y|
    div << arrowHeadToHTML(x, y)