Class: Scruffy::Components::ValueMarkers

Inherits:
Base
  • Object
show all
Defined in:
lib/scruffy/components/value_markers.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#id, #options, #position, #size, #visible

Instance Method Summary collapse

Methods inherited from Base

#initialize, #process, #render

Constructor Details

This class inherits a constructor from Scruffy::Components::Base

Instance Attribute Details

#markersObject

Returns the value of attribute markers.



4
5
6
# File 'lib/scruffy/components/value_markers.rb', line 4

def markers
  @markers
end

Instance Method Details

#draw(svg, bounds, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/scruffy/components/value_markers.rb', line 6

def draw(svg, bounds, options={})
  markers = (options[:markers] || self.markers) || 5
  all_values = []

  (0...markers).each do |idx|
    marker = ((1 / (markers - 1).to_f) * idx) * bounds[:height]
    all_values << (options[:max_value] - options[:min_value]) * ((1 / (markers - 1).to_f) * idx) + options[:min_value]
  end
  
  (0...markers).each do |idx|
    marker = ((1 / (markers - 1).to_f) * idx) * bounds[:height]
    marker_value = (options[:max_value] - options[:min_value]) * ((1 / (markers - 1).to_f) * idx) + options[:min_value]
    marker_value = options[:value_formatter].route_format(marker_value, idx, options.merge({:all_values => all_values})) if options[:value_formatter]

    svg.text( marker_value.to_s, 
      :x => bounds[:width], 
      :y => (bounds[:height] - marker), 
      'font-size' => relative(8),
      'font-family' => options[:theme].font_family,
      :fill => ((options.delete(:marker_color_override) || options[:theme].marker) || 'white').to_s,
      'text-anchor' => 'end')
  end
  
end