senga 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Rakefile +81 -0
  2. data/doc/LICENSE +19 -0
  3. data/doc/README +2 -0
  4. data/doc/TODO +0 -0
  5. data/lib/senga.rb +87 -0
  6. metadata +68 -0
@@ -0,0 +1,81 @@
1
+ require 'rake/gempackagetask'
2
+ require 'rake/rdoctask'
3
+
4
+ $: << "#{File.dirname(__FILE__)}/lib"
5
+
6
+ spec = Gem::Specification.new { |s|
7
+ s.platform = Gem::Platform::RUBY
8
+
9
+ s.author = "Pete Elmore"
10
+ s.email = "1337p337@gmail.com"
11
+ s.files = Dir["{lib,doc,bin,ext}/**/*"].delete_if {|f|
12
+ /\/rdoc(\/|$)/i.match f
13
+ } + %w(Rakefile)
14
+ s.require_path = 'lib'
15
+ s.has_rdoc = true
16
+ s.extra_rdoc_files = Dir['doc/*'].select(&File.method(:file?))
17
+ s.extensions << 'ext/extconf.rb' if File.exist? 'ext/extconf.rb'
18
+ Dir['bin/*'].map(&File.method(:basename)).map(&s.executables.method(:<<))
19
+
20
+ s.name = 'senga'
21
+ s.summary = "Senga draws lines. Simplest graphing library."
22
+ s.homepage = "http://debu.gs/#{s.name}"
23
+ %w(rmagick).each &s.method(:add_dependency)
24
+ s.version = '0.1.0'
25
+ }
26
+
27
+ Rake::RDocTask.new(:doc) { |t|
28
+ t.main = 'doc/README'
29
+ t.rdoc_files.include 'lib/**/*.rb', 'doc/*', 'bin/*', 'ext/**/*.c',
30
+ 'ext/**/*.rb'
31
+ t.options << '-S' << '-N'
32
+ t.rdoc_dir = 'doc/rdoc'
33
+ }
34
+
35
+ Rake::GemPackageTask.new(spec) { |pkg|
36
+ pkg.need_tar_bz2 = true
37
+ }
38
+ desc "Cleans out the packaged files."
39
+ task(:clean) {
40
+ FileUtils.rm_rf 'pkg'
41
+ }
42
+
43
+ desc "Builds and installs the gem for #{spec.name}"
44
+ task(:install => :package) {
45
+ g = "pkg/#{spec.name}-#{spec.version}.gem"
46
+ system "sudo gem install -l #{g}"
47
+ }
48
+
49
+ desc "Runs IRB, automatically require()ing #{spec.name}."
50
+ task(:irb) {
51
+ exec "irb -Ilib -r#{spec.name}"
52
+ }
53
+
54
+ desc "Generates a static gemspec file; useful for github."
55
+ task(:static_gemspec) {
56
+ # This whole thing is hacky.
57
+ spec.validate
58
+ spec_attrs = %w(
59
+ platform author email files require_path has_rdoc extra_rdoc_files
60
+ extensions executables name summary homepage
61
+ ).map { |attr|
62
+ "\ts.#{attr} = #{spec.send(attr).inspect}\n"
63
+ }.join <<
64
+ "\ts.version = #{spec.version.to_s.inspect}\n" <<
65
+ spec.dependencies.map { |dep|
66
+ "\ts.add_dependency #{dep.inspect}\n"
67
+ }.join
68
+
69
+ File.open("#{spec.name}.gemspec", 'w') { |f|
70
+ f.print <<-EOGEMSPEC
71
+ # This is a static gempsec automatically generated by rake. It's better to
72
+ # edit the Rakefile than this file. It is kept in the repository for the
73
+ # benefit of github.
74
+
75
+ spec = Gem::Specification.new { |s|
76
+ #{spec_attrs}}
77
+
78
+ Gem::Builder.new(spec).build if __FILE__ == $0
79
+ EOGEMSPEC
80
+ }
81
+ }
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Peter Elmore (pete.elmore at gmail.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a
4
+ copy of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom the
8
+ Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+ Senga draws lines. That's it. If what you want is some lines drawn and to do
2
+ the rest yourself using RMagick, then this may be what you want.
File without changes
@@ -0,0 +1,87 @@
1
+ require 'RMagick'
2
+
3
+ # Make a new graph:
4
+ # graph = Senga.new
5
+ # Plot a couple of lines:
6
+ # graph.plot('blue', [1, 2, 3])
7
+ # graph.plot('#ff00ff', [3, 2, 1], 2)
8
+ # Render it:
9
+ # image = graph.render(:xscale => 3, :yscale => 20)
10
+ # There are probably other things you'll want to do with that, like this:
11
+ # File.open('some-graph.png', 'w') { |f| f.write image.to_blob }
12
+ class Senga
13
+ # Render has it some options.
14
+ DefaultRenderOpts = {
15
+ :xscale => nil,
16
+ :yscale => nil,
17
+ :stroke_width => 1,
18
+ :format => 'PNG',
19
+ }
20
+
21
+ def plots
22
+ @plots ||= []
23
+ end
24
+
25
+ def plot(*a)
26
+ plots << a
27
+ end
28
+
29
+ # Render has it some options. See Senga::DefaultRenderOpts.
30
+ def render opts = {}
31
+ opts = sanitize_opts opts
32
+ coords = data_coords opts[:xscale], opts[:yscale]
33
+ image = Magick::Image.new(opts[:width], opts[:height], 'transparent')
34
+ image.format = opts[:format]
35
+ draw = Magick::Draw.new
36
+ coords.each { |color,coords,width|
37
+ draw.stroke color
38
+ draw.stroke_width width
39
+ coords.inject { |a,b|
40
+ draw.line *[a,b].flatten
41
+ b
42
+ }
43
+ }
44
+ draw.draw image
45
+ image
46
+ end
47
+
48
+ private
49
+
50
+ def sanitize_opts o
51
+ xmax = plots.inject(0) { |max,p| [max, p[1].size].max }
52
+ raise ArgumentError, "Don't have anything to plot?" if xmax < 2
53
+ ymax = plots.inject(0) { |max,p| [max, p.max].max }
54
+
55
+ o = DefaultOpts.merge o
56
+
57
+ if o[:xscale].nil?
58
+ if o[:width].nil?
59
+ o[:width] = xmax
60
+ end
61
+ o[:xscale] = o[:width] / xmax
62
+ end
63
+ o[:width] ||= o[:xscale] * xmax
64
+
65
+ if o[:yscale].nil?
66
+ if o[:height].nil?
67
+ o[:height] = [ymax, 1].max
68
+ end
69
+ o[:yscale] = o[:height] / ymax
70
+ end
71
+ o[:height] ||= o[:yscale] * ymax
72
+
73
+ o
74
+ end
75
+
76
+ # Turns the current set of plots into arrays of coordinates.
77
+ def data_coords(xscale, yscale)
78
+ plots.map { |c,data,*rest|
79
+ n = 0
80
+ [c] << data.map { |point|
81
+ a = [n, point * yscale]
82
+ n += xscale
83
+ a
84
+ } + rest
85
+ }
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: senga
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pete Elmore
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rmagick
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: 1337p337@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - doc/LICENSE
33
+ - doc/README
34
+ - doc/TODO
35
+ files:
36
+ - lib/senga.rb
37
+ - doc/LICENSE
38
+ - doc/README
39
+ - doc/TODO
40
+ - Rakefile
41
+ has_rdoc: true
42
+ homepage: http://debu.gs/senga
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.1
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Senga draws lines. Simplest graphing library.
67
+ test_files: []
68
+