Class: Curvature::Chart
- Inherits:
-
Object
- Object
- Curvature::Chart
- Defined in:
- lib/curvature/chart.rb
Instance Attribute Summary collapse
-
#curves ⇒ Object
Returns the value of attribute curves.
-
#height ⇒ Object
Returns the value of attribute height.
-
#name ⇒ Object
Returns the value of attribute name.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x_range ⇒ Object
Returns the value of attribute x_range.
-
#y_range ⇒ Object
Returns the value of attribute y_range.
Instance Method Summary collapse
- #<<(curve) ⇒ Object
-
#initialize(params = {}) ⇒ Chart
constructor
A new instance of Chart.
- #scaled_point(x, y) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Chart
Returns a new instance of Chart.
12 13 14 15 16 17 18 |
# File 'lib/curvature/chart.rb', line 12 def initialize(params={}) @curves = [] { width: 500, height: 500, }.merge(params).each { |k, v| send("#{k}=", v) } end |
Instance Attribute Details
#curves ⇒ Object
Returns the value of attribute curves.
10 11 12 |
# File 'lib/curvature/chart.rb', line 10 def curves @curves end |
#height ⇒ Object
Returns the value of attribute height.
7 8 9 |
# File 'lib/curvature/chart.rb', line 7 def height @height end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/curvature/chart.rb', line 5 def name @name end |
#width ⇒ Object
Returns the value of attribute width.
6 7 8 |
# File 'lib/curvature/chart.rb', line 6 def width @width end |
#x_range ⇒ Object
Returns the value of attribute x_range.
8 9 10 |
# File 'lib/curvature/chart.rb', line 8 def x_range @x_range end |
#y_range ⇒ Object
Returns the value of attribute y_range.
9 10 11 |
# File 'lib/curvature/chart.rb', line 9 def y_range @y_range end |
Instance Method Details
#<<(curve) ⇒ Object
20 21 22 23 |
# File 'lib/curvature/chart.rb', line 20 def <<(curve) curve.chart = self @curves << curve end |
#scaled_point(x, y) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/curvature/chart.rb', line 25 def scaled_point(x, y) [ @width * ((x - @x_range.min) / (@x_range.max - @x_range.min)), @height * ((y - @y_range.min) / (@y_range.max - @y_range.min)), ] end |
#to_html ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/curvature/chart.rb', line 32 def to_html xml = Builder::XmlMarkup.new(indent: 2) xml.div(class: 'chart') do xml.h2(@name) if @name xml.svg(xmlns: 'http://www.w3.org/2000/svg', version: '1.1', width: @width, height: @height) do xml.g(width: @width, height: @height, transform: "translate(0,#{@height}) scale(1,-1)") do # draw border xml.rect(x: 0, y: 0, width: @width, height: @height, fill: 'none', :'stroke-width' => 1, stroke: 'blue') # draw linear line xml.line(x1: 0, y1: 0, x2: @width, y2: @height, :'stroke-width' => 0.5, stroke: 'blue') # draw curves @curves.each do |curve| xml << curve.to_html end end end end xml.target! end |