Class: Prawn::Graph::ChartComponents::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/graph/chart_components/canvas.rb

Overview

A Prawn::Graph::Canvas represents the area on which a graph will be drawn. Think of it as the container in which your chart / graph will be sized to fit within.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(series, prawn, options = {}) {|_self| ... } ⇒ Prawn::Graph::Canvas

Returns a canvas ready to be drawn on the provided prawn document.

Parameters:

Yields:

  • (_self)

Yield Parameters:


15
16
17
18
19
20
21
22
23
24
# File 'lib/prawn/graph/chart_components/canvas.rb', line 15

def initialize(series, prawn, options = {}, &block)
  @series   = series
  verify_series_are_ok!
  @options  =  {xaxis_labels: []}.merge(options.merge({ series_count: series.size }))
  @prawn    = prawn
  @theme    = options[:theme].nil? ? Prawn::Graph::Theme.default : options[:theme]
  @layout   = Prawn::Graph::Calculations::LayoutCalculator.new([prawn.bounds.width, prawn.bounds.height], @options, @theme).calculate

  yield self if block_given?
end

Instance Attribute Details

#layoutObject (readonly)

Returns the value of attribute layout.


8
9
10
# File 'lib/prawn/graph/chart_components/canvas.rb', line 8

def layout
  @layout
end

#optionsObject (readonly)

Returns the value of attribute options.


8
9
10
# File 'lib/prawn/graph/chart_components/canvas.rb', line 8

def options
  @options
end

#prawnObject (readonly)

Returns the value of attribute prawn.


8
9
10
# File 'lib/prawn/graph/chart_components/canvas.rb', line 8

def prawn
  @prawn
end

#seriesObject (readonly)

Returns the value of attribute series.


8
9
10
# File 'lib/prawn/graph/chart_components/canvas.rb', line 8

def series
  @series
end

#themeObject (readonly)

Returns the value of attribute theme.


8
9
10
# File 'lib/prawn/graph/chart_components/canvas.rb', line 8

def theme
  @theme
end

Instance Method Details

#drawnil

Fires off the actual drawing of the chart on the provided canvas.

Returns:

  • (nil)

29
30
31
32
33
34
35
36
37
38
# File 'lib/prawn/graph/chart_components/canvas.rb', line 29

def draw
  prawn.bounding_box(position, :width => layout.canvas_width, :height => layout.canvas_height, padding: 0) do
    prawn.save_graphics_state do         
      apply_theme! 
      render_title_area!
      render_series_keys!
      render_graph_area!
    end
  end
end

#positionArray

The coordinates which the canvas will be drawn at

Returns:

  • (Array)
    X-Coord, Y-Coord

43
44
45
# File 'lib/prawn/graph/chart_components/canvas.rb', line 43

def position
  @options[:at] || [0,0]
end