Class: Tuttify::BarGraph

Inherits:
Graph
  • Object
show all
Defined in:
lib/tuftify/bar_graph.rb

Instance Method Summary collapse

Constructor Details

#initializeBarGraph

Returns a new instance of BarGraph.



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

def initialize
  
end

Instance Method Details

#drawObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tuftify/bar_graph.rb', line 10

def draw
  max_value = @values.collect {|v| v[1]}.max
  barwidth = @width / @values.length
  x = 0
  lh = label_height
  draw_height = @height - lh
  @values.each do |key, value|
    h = draw_height / max_value * value
    @drawing.fill('black')
    @drawing.stroke('white')
    @drawing.rectangle(x, draw_height, x + barwidth, draw_height - h)
    @drawing.annotate(@canvas, barwidth, lh, x, draw_height, key.to_s)
    x += barwidth
  end
  @drawing.draw(@canvas)
  return @canvas
end