little_graphs 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
3
+ gem 'quick_magick'
4
+ gem 'rspec'
5
+
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ little_graphs (0.0.0)
5
+ quick_magick
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.1.3)
11
+ quick_magick (0.8.0)
12
+ rspec (2.11.0)
13
+ rspec-core (~> 2.11.0)
14
+ rspec-expectations (~> 2.11.0)
15
+ rspec-mocks (~> 2.11.0)
16
+ rspec-core (2.11.1)
17
+ rspec-expectations (2.11.3)
18
+ diff-lcs (~> 1.1.3)
19
+ rspec-mocks (2.11.3)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ little_graphs!
26
+ quick_magick
27
+ rspec
data/README.mkd ADDED
@@ -0,0 +1,10 @@
1
+ #Little Graphs
2
+
3
+ ## To Use:
4
+
5
+ ```ruby
6
+ require 'little_graphs'
7
+ graph = LittleGraphs.new()
8
+ graph.draw([12, 3, 0, 9, 10, 8, 2, 4, 1], "example.jpeg")
9
+ ```
10
+ ![example graph](https://raw.github.com/absperf/little_graphs/master/example.jpeg)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ #require "bundler/gem_tasks"
data/example.jpeg ADDED
Binary file
@@ -0,0 +1,44 @@
1
+ require 'quick_magick'
2
+
3
+ class LittleGraphs
4
+ attr_accessor :width, :height
5
+
6
+ def initialize(width = 100, height = 35)
7
+ @width = width
8
+ @height = height
9
+ end
10
+
11
+ def draw(datapoints, filename = nil)
12
+ if filename.nil? then filename = "new.jpeg" end
13
+ coordinates = define_coordinates(datapoints)
14
+
15
+ f = QuickMagick::Image::solid(@width, @height, :white)
16
+ f.draw_polyline(coordinates, :fill => :white, :stroke => :blue)
17
+
18
+ f.save(filename)
19
+ end
20
+
21
+ def define_coordinates(datapoints)
22
+ margin = 5
23
+ point_space = @width/datapoints.length
24
+ translated_datapoints = translate_datapoints(datapoints)
25
+ coordinates = []
26
+
27
+ translated_datapoints.each_with_index do |datapoint, i|
28
+ coordinates.push [i*point_space+margin, datapoint]
29
+ end
30
+ coordinates.flatten
31
+ end
32
+
33
+ def translate_domain(datapoints)
34
+ point_space = @width/datapoints.length
35
+ end
36
+
37
+ def translate_datapoints(datapoints)
38
+ height = @height - 5 # add a margin
39
+ point_space = height/(datapoints.max - datapoints.min)
40
+
41
+ datapoints.map { |datapoint| @height - datapoint * point_space }
42
+ end
43
+
44
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'little_graphs'
3
+ s.version = '1.0.0'
4
+ s.date = '2012-10-15'
5
+ s.summary = 'Sparklines'
6
+ s.authors = ['J. Cheng']
7
+ s.email = 'jcheng@absolute-performance.com'
8
+ s.files = [`git ls-files`.split($\)]
9
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
10
+
11
+ s.add_dependency "quick_magick"
12
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe LittleGraphs do
4
+ describe '#initialize' do
5
+ context 'with width and height' do
6
+ it 'sets width and height' do
7
+ lg = LittleGraphs.new(100, 200)
8
+ lg.width.should == 100
9
+ lg.height.should == 200
10
+ end
11
+ end
12
+
13
+ context 'without width and height' do
14
+ it 'sets width and height to default' do
15
+ lg = LittleGraphs.new
16
+ lg.width.should == 100
17
+ lg.height.should == 35
18
+ end
19
+ end
20
+ end
21
+
22
+ describe '#draw' do
23
+ context 'with datapoints' do
24
+ it 'draws a graph' do
25
+ lg = LittleGraphs.new
26
+ lg.draw([12, 3, 0, 9, 10, 8, 2, 4, 1], "example.jpeg")
27
+ end
28
+ end
29
+ end
30
+
31
+ describe '#define_coordinates' do
32
+ context 'datapoints' do
33
+ it 'finds x,y coordinates for 1D array' do
34
+ lg = LittleGraphs.new
35
+ coords = lg.define_coordinates([1, 2, 3, 4, 5])
36
+ #coords.should == [1,0, 10, 2, 20, 3, 30, 4, 40, 5]
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '#translate_datapoints' do
42
+ context 'datapoints' do
43
+ lg = LittleGraphs.new
44
+ datapoints = [1, 2, 3, 4, 5]
45
+ translated_datapoints = lg.translate_datapoints(datapoints)
46
+ translated_datapoints.should == [28, 21, 14, 7, 0]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'bundler/setup'
3
+ require 'little_graphs'
4
+
5
+ RSpec.configure do |config|
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: little_graphs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - J. Cheng
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: quick_magick
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description:
31
+ email: jcheng@absolute-performance.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - README.mkd
40
+ - Rakefile
41
+ - example.jpeg
42
+ - lib/little_graphs.rb
43
+ - little_graphs.gemspec
44
+ - spec/litte_graphs_spec.rb
45
+ - spec/spec_helper.rb
46
+ homepage:
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.23
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Sparklines
70
+ test_files:
71
+ - spec/litte_graphs_spec.rb
72
+ - spec/spec_helper.rb