Class: SVGCharts::Line

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

Instance Method Summary collapse

Methods included from SVGElements

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

Constructor Details

#initialize(options) ⇒ Line

Returns a new instance of Line.



5
6
7
8
9
10
11
12
13
# File 'lib/charts/line.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/charts/line.rb', line 58

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

  calculate_coordinates options[:data]

  elements = ""

  @coordinates.each_with_index do |coordinate, i|
    elements << line({
      :x1 => coordinate[0],
      :x2 => coordinate[1],
      :y1 => coordinate[2],
      :y2 => coordinate[3],
      :line_width => 1,
      :color => options[:data_color]
    })

    if options[:show_dashed]
      elements << line_dashed({
        :x1 => coordinate[0],
        :x2 => coordinate[0],
        :y1 => @height,
        :y2 => coordinate[2],
        :line_width => 1,
        :color => "#b9b9b9"
      })
    end

    elements << circle({
      :x => coordinate[0],
      :y => coordinate[2],
      :radius => 2,
      :background => "#fff",
      :line_width => 2,
      :line_color => "#444"
    })

    elements << text({
      :x => coordinate[0] - 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[0] - 5,
        :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/line.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