Class: SvgDataPlotterGem::Gauge

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Gauge

Returns a new instance of Gauge.



5
6
7
8
9
10
11
# File 'lib/svg_data_plotter/gauge.rb', line 5

def initialize(options)
  validation options, [:score, :total, :size]

  @score = options[:score]
  @total = options[:total]
  @size  = options[:size]
end

Instance Attribute Details

#scoreObject (readonly)

Returns the value of attribute score.



3
4
5
# File 'lib/svg_data_plotter/gauge.rb', line 3

def score
  @score
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/svg_data_plotter/gauge.rb', line 3

def size
  @size
end

#totalObject (readonly)

Returns the value of attribute total.



3
4
5
# File 'lib/svg_data_plotter/gauge.rb', line 3

def total
  @total
end

Instance Method Details

#arc_endObject



61
62
63
# File 'lib/svg_data_plotter/gauge.rb', line 61

def arc_end
  "#{x_co_ord(180)} #{y_co_ord(180)}"
end

#arc_startObject



57
58
59
# File 'lib/svg_data_plotter/gauge.rb', line 57

def arc_start
  "#{x_co_ord(0)} #{y_co_ord(0)}"
end

#centerObject



21
22
23
# File 'lib/svg_data_plotter/gauge.rb', line 21

def center
  size / 2.0
end

#heightObject



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

def height
  (size / 2.0) + 15.0
end

#offsetObject



37
38
39
# File 'lib/svg_data_plotter/gauge.rb', line 37

def offset
  percentage_adj - semi_circle
end

#percentageObject



17
18
19
# File 'lib/svg_data_plotter/gauge.rb', line 17

def percentage
  (score / total.to_f) * 100
end

#percentage_adjObject



33
34
35
# File 'lib/svg_data_plotter/gauge.rb', line 33

def percentage_adj
  (score / total.to_f) * semi_circle
end

#radians(degrees) ⇒ Object



41
42
43
# File 'lib/svg_data_plotter/gauge.rb', line 41

def radians(degrees)
  degrees * Math::PI / 180
end

#radiusObject



25
26
27
# File 'lib/svg_data_plotter/gauge.rb', line 25

def radius
  center - 10.0
end

#semi_circleObject



29
30
31
# File 'lib/svg_data_plotter/gauge.rb', line 29

def semi_circle
  radius * Math::PI
end

#validation(options, params) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
# File 'lib/svg_data_plotter/gauge.rb', line 65

def validation(options, params)
  raise ArgumentError if options.empty?

  params.each do |key|
    raise ArgumentError unless options.has_key?(key)
  end
end

#x_co_ord(angle) ⇒ Object



45
46
47
48
49
# File 'lib/svg_data_plotter/gauge.rb', line 45

def x_co_ord(angle)
  rad = radians(angle)
  x   = radius * Math.cos(rad)
  center + x
end

#y_co_ord(angle) ⇒ Object



51
52
53
54
55
# File 'lib/svg_data_plotter/gauge.rb', line 51

def y_co_ord(angle)
  rad = radians(angle)
  y   = radius * Math.sin(rad)
  center - y
end