Class: Senga

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

Overview

Make a new graph:

graph = Senga.new

Plot a couple of lines:

graph.plot('blue', [1, 2, 3])
graph.plot('#ff00ff', [3, 2, 1], 2)

Render it:

image = graph.render(:xscale => 3, :yscale => 20)

There are probably other things you’ll want to do with that, like this:

File.open('some-graph.png', 'w') { |f| f.write image.to_blob }

Constant Summary collapse

DefaultRenderOpts =

Render has it some options.

{
	:xscale => nil,
	:yscale => nil,
	:stroke_width => 1,
	:format => 'PNG',
}

Instance Method Summary collapse

Instance Method Details

#plot(*a) ⇒ Object



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

def plot(*a)
	plots << a
end

#plotsObject



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

def plots
	@plots ||= []
end

#render(opts = {}) ⇒ Object

Render has it some options. See Senga::DefaultRenderOpts.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/senga.rb', line 30

def render opts = {}
	opts = sanitize_opts opts
	coords = data_coords opts[:xscale], opts[:yscale]
	image = Magick::Image.new(opts[:width], opts[:height], 'transparent')
	image.format = opts[:format]
	draw = Magick::Draw.new
	coords.each { |color,coords,width|
		draw.stroke color
		draw.stroke_width width
		coords.inject { |a,b|
			draw.line *[a,b].flatten
			b
		}
	}
	draw.draw image
	image
end