Class: Drawr::Base

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

Direct Known Subclasses

Bar, Line, Pie

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_width = 800) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/drawr.rb', line 17

def initialize(target_width = 800)
  @data_points = {}
  @theme = {
    :background_color => '#dbdbdb',
    :color_scheme     => 'red',
    :padding          => 'left: 30, right: 0, top: 10, bottom: 30',
  }
  @extra_options = {}
  if target_width.is_a? String
    @width, @height = target_width.split(/x/, 2)
  else
    @width, @height = target_width, (target_width * (3.0/4.0))
  end
  @div = 'plotr'
end

Instance Attribute Details

#divObject

Returns the value of attribute div.



6
7
8
# File 'lib/drawr.rb', line 6

def div
  @div
end

#hide_dotsObject

Returns the value of attribute hide_dots.



5
6
7
# File 'lib/drawr.rb', line 5

def hide_dots
  @hide_dots
end

#labelsObject

A hash of names for the individual columns, where the key is the array index for the column this label represents.



11
12
13
# File 'lib/drawr.rb', line 11

def labels
  @labels
end

#themeObject

Returns the value of attribute theme.



7
8
9
# File 'lib/drawr.rb', line 7

def theme
  @theme
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/drawr.rb', line 4

def title
  @title
end

Instance Method Details

#data(name, data_points = [], color = nil) ⇒ Object



13
14
15
# File 'lib/drawr.rb', line 13

def data(name, data_points = [], color = nil)
  @data_points[name] = data_points
end

#to_sObject



40
41
42
43
44
45
# File 'lib/drawr.rb', line 40

def to_s
  <<END
  <div><canvas id="#{div}" height="#{@height.to_i}" width="#{@width.to_i}"></canvas></div>
  <script>#{dataset}\n#{options}\n#{commands}</script>
END
end

#write(file) ⇒ Object



38
# File 'lib/drawr.rb', line 38

def write(file); File.open(file, 'wb') { |f| f.write to_s }; end

#write_to_div(div) ⇒ Object



33
34
35
36
# File 'lib/drawr.rb', line 33

def write_to_div(div)
  @div = div
  to_s
end