Class: ChartHelpers::GanttChart

Inherits:
Object
  • Object
show all
Defined in:
lib/chart_helpers/gantt_chart.rb

Constant Summary collapse

DEFAULT =
{
  font_size:         12,
  font_family:       'arial',
  font_weight:       'regular',
  title_font_weight: 'bold',
  font_color:        '#000',
  row_color:         '#9370DB',
  row_height:        30
}.freeze
TITLE_TOP_PADDING =
20
GRID_TOP_PADDING =
TITLE_TOP_PADDING + 80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, data:, scale:, width: 2000, height: nil, date_format: nil, number_format: nil, options: {}) ⇒ GanttChart

Returns a new instance of GanttChart.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chart_helpers/gantt_chart.rb', line 20

def initialize(title:, data:, scale:, width: 2000, height: nil, date_format: nil, number_format: nil, options: {})
  @title = title
  @data = data
  @scale = scale
  @date_format = date_format
  @number_format = number_format
  @options = options

  @max_value = @data.collect { |v| v[:end] }.max
  @max_value = @max_value.to_time if @max_value.is_a?(DateTime)
  @max_value = @max_value.to_f

  @min_value = @data.collect { |v| v[:start] }.min
  @min_value = @min_value.to_time if @min_value.is_a?(DateTime)
  @min_value = @min_value.to_f

  # We aren't guaranteed for our min_value to be 0, so base everything off of the difference
  @diff_value = @max_value - @min_value
  @row_height = setting(:row_height)
  @width = width
  # Height should be one "row_height"" for each row + one for the axis + title
  @height = height || @row_height * (data.size + 1) + 100

  # Cache for text sizes to avoid duplicating work
  @text_sizes = {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



18
19
20
# File 'lib/chart_helpers/gantt_chart.rb', line 18

def data
  @data
end

#scaleObject (readonly)

Returns the value of attribute scale.



18
19
20
# File 'lib/chart_helpers/gantt_chart.rb', line 18

def scale
  @scale
end

#titleObject (readonly)

Returns the value of attribute title.



18
19
20
# File 'lib/chart_helpers/gantt_chart.rb', line 18

def title
  @title
end

Instance Method Details

#build(output) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/chart_helpers/gantt_chart.rb', line 47

def build(output)
  svg = Victor::SVG.new(width: @width, height: @height)
  render_title(svg)
  render_grid(svg)
  svg.g(transform: "translate(0, #{TITLE_TOP_PADDING})") do
    @data.each_with_index { |row, i| render_row(svg, row, i + 1) }
  end
  svg.save(output)
end