Class: LittleGraphs

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = 100, height = 35) ⇒ LittleGraphs

Returns a new instance of LittleGraphs.



6
7
8
9
# File 'lib/little_graphs.rb', line 6

def initialize(width = 100, height = 35)
  @width = width
  @height = height
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



4
5
6
# File 'lib/little_graphs.rb', line 4

def height
  @height
end

#widthObject

Returns the value of attribute width.



4
5
6
# File 'lib/little_graphs.rb', line 4

def width
  @width
end

Instance Method Details

#define_coordinates(datapoints) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/little_graphs.rb', line 24

def define_coordinates(datapoints)
  margin = 5
  point_space = @width/datapoints.length
  translated_datapoints = translate_datapoints(datapoints)
  coordinates = []

  translated_datapoints.each_with_index do |datapoint, i|
    coordinates.push [i*point_space+margin, datapoint]
  end
  coordinates.flatten
end

#draw(datapoints, filename = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/little_graphs.rb', line 11

def draw(datapoints, filename = nil)
  coordinates = define_coordinates(datapoints)

  f = QuickMagick::Image::solid(@width, @height, :white)
  f.draw_polyline(coordinates, :fill => :white, :stroke => :blue)

  if filename.nil?
    f.to_blob
  else
    f.save(filename)
  end
end

#translate_datapoints(datapoints) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/little_graphs.rb', line 40

def translate_datapoints(datapoints)
  height = @height # add a margin
  point_space = height/(datapoints.max - datapoints.min + 1)

  datapoints.map do |datapoint|
    puts @height
    @height - (datapoint - datapoints.min) * point_space 
  end
end

#translate_domain(datapoints) ⇒ Object



36
37
38
# File 'lib/little_graphs.rb', line 36

def translate_domain(datapoints)
  point_space = @width/datapoints.length
end