Class: SVGCharts::Bar

Inherits:
Object
  • Object
show all
Includes:
SVGElements
Defined in:
lib/charts/bar.rb

Instance Method Summary collapse

Methods included from SVGElements

#circle, #line, #line_dashed, #rect, #text, #text_with_rotation

Constructor Details

#initialize(options) ⇒ Bar

Returns a new instance of Bar.



5
6
7
8
9
10
11
12
13
# File 'lib/charts/bar.rb', line 5

def initialize(options)
  validation options, [:width, :height, :y_retreat, :x_retreat]

  @width = options[:width]
  @height = options[:height]

  @y_retreat = options[:y_retreat]
  @x_retreat = options[:x_retreat]
end

Instance Method Details

#draw(options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/charts/bar.rb', line 58

def draw(options)
  validation options, [:scale, :data, :data_color, :show_scale]

  calculate_coordinates options[:data]

  elements = ""

  @coordinates.each_with_index do |coordinate, i|
    elements << rect({
      :x => coordinate[0] * 1.025,
      :y => coordinate[2],
      :width => coordinate[1] - coordinate[0],
      :height => @height - coordinate[2],
      :color => options[:data_color]
    })

    elements << text({
      :x => (((coordinate[1] - coordinate[0]) - options[:scale][i].length)/2) + (coordinate[0] * 1.025) - 10,
      :y => coordinate[2] - 10,
      :color => options[:data_color],
      :font_size => 11,
      :font_weight => "bold",
      :value => options[:data][i]
    })

    if options[:show_scale]
      elements << text({
        :x => (((coordinate[1] - coordinate[0]) - options[:scale][i].length)/2) + (coordinate[0] * 1.025) - 10,
        :y => @height + 12,
        :color => "#999",
        :font_size => 10,
        :font_weight => "normal",
        :value => options[:scale][i]
      })
    end
  end

  elements
end

#scale(options) ⇒ Object



15
16
17
18
19
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
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/charts/bar.rb', line 15

def scale(options)
  validation options, [:y_label, :x_label]

  scale = line({
    :x1 => @x_retreat - 8,
    :x2 => @x_retreat - 8,
    :y1 => @y_retreat - 2,
    :y2 => @height + 13,
    :line_width => 2,
    :color => "#aaa"
  })

  scale << line({
    :x1 => 0,
    :x2 => @width,
    :y1 => @height,
    :y2 => @height,
    :line_width => 2,
    :color => "#aaa"
  })

  scale << text({
    :x => @width/2 - (options[:y_label].length * 2),
    :y => @height + 30,
    :color => "#bbb",
    :font_size => 11,
    :font_weight => "bold",
    :value => options[:x_label]
  })

  scale << text_with_rotation({
    :x => @height/2 - (options[:y_label].length * 2),
    :y => 8,
    :color => "#bbb",
    :font_size => 11,
    :font_weight => "bold",
    :rotate => "270 #{@height/2} #{@height/2}",
    :value => options[:y_label]
  })

  scale
end