Kelsin-Kelsin-lilygraph 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg/*
@@ -0,0 +1,47 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{Kelsin-lilygraph}
8
+ s.version = "0.2.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Christopher Giroir"]
12
+ s.date = %q{2009-08-19}
13
+ s.description = %q{Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder.}
14
+ s.email = %q{kelsin@valefor.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "Kelsin-lilygraph.gemspec",
22
+ "LICENSE",
23
+ "README",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/lilygraph.rb",
27
+ "lilygraph.gemspec"
28
+ ]
29
+ s.homepage = %q{http://github.com/Kelsin/lilygraph}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.4}
33
+ s.summary = %q{Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder.}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ s.add_runtime_dependency(%q<color-tools>, [">= 1.3.0"])
41
+ else
42
+ s.add_dependency(%q<color-tools>, [">= 1.3.0"])
43
+ end
44
+ else
45
+ s.add_dependency(%q<color-tools>, [">= 1.3.0"])
46
+ end
47
+ end
data/LICENSE ADDED
File without changes
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |s|
4
+ s.name = "Kelsin-lilygraph"
5
+ s.summary = "Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder."
6
+ s.description = "Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder."
7
+ s.email = "kelsin@valefor.com"
8
+ s.homepage = "http://github.com/Kelsin/lilygraph"
9
+ s.authors = ["Christopher Giroir"]
10
+
11
+ s.add_dependency("color-tools", ">= 1.3.0")
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.3
data/lib/lilygraph.rb ADDED
@@ -0,0 +1,191 @@
1
+ # This is the main graphing class used for generating svg graphs.
2
+ #
3
+ # :author: Christopher Giroir <kelsin@valefor.com>
4
+
5
+ require 'color'
6
+
7
+ class Lilygraph
8
+ # Default options for the initializer
9
+ DEFAULT_OPTIONS = {
10
+ :height => '100%',
11
+ :width => '100%',
12
+ :indent => 2,
13
+ :padding => 14,
14
+ :bar_text => true,
15
+ :viewbox => {
16
+ :width => 800,
17
+ :height => 600
18
+ },
19
+ :margin => { :top => 50, :left => 50, :right => 50, :bottom => 100 }
20
+ }
21
+
22
+ # An array of labels to use on the y axis. Make sure you have the right number
23
+ # of labels. The size of this array should = the size of the data array.
24
+ attr_accessor :labels
25
+
26
+ # This is the data for the graph. It should be an array where every item is
27
+ # either a number or an array of numbers.
28
+ #
29
+ # Examples:
30
+ # For a simple bar graph: data=[1,2,3]
31
+ # For a grouped bar graph: data=[[1,10],[2,20],[3,30]]
32
+ attr_accessor :data
33
+
34
+ # Returns a new graph creator with some default options and other options that you pass.
35
+ #
36
+ # The options has includes:
37
+ # :height - String to use as height parameter on the svg tag. Default is '100%'.
38
+ # :width - String to use as width parameter on the svg tag. Default is '100%'.
39
+ # :indent - Indent option to the XmlMarkup object. Defaults to 2.
40
+ # :padding - Number of svg units in between two bars.
41
+ # :viewbox - Hash of :height and :width keys to use for the viewbox parameter of the svg tag. Defaults to { :height => 600, :width => 800 }.
42
+ # :margin - Hash of margins to use for graph (in svg units). Defaults to { :top => 50, :left => 50, :right => 50, :bottom => 100 }.
43
+ def initialize(options = {})
44
+ @options = DEFAULT_OPTIONS.merge(options)
45
+ @data = []
46
+ @labels = []
47
+ end
48
+
49
+ # Updates the graph options with items from the passed in hash
50
+ def update_options(options = {})
51
+ @options = @options.merge(options)
52
+ end
53
+
54
+ # This returns a string of the graph as an svg. You can pass in a block in
55
+ # order to add your own items to the graph. Your block is passed the XmlMarkup
56
+ # object to use as well as the options hash in case you need to use some of
57
+ # that data.
58
+ def render
59
+ output = ""
60
+ xml = Builder::XmlMarkup.new(:target => output, :indent => @options[:indent])
61
+
62
+ # Output headers unless we specified otherwise
63
+ xml.instruct!
64
+ xml.declare! :DOCTYPE, :svg, :PUBLIC, "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"
65
+
66
+ xml.svg(:viewBox => "0 0 #{@options[:viewbox][:width]} #{@options[:viewbox][:height]}",
67
+ :width => @options[:width], :height => @options[:height],
68
+ :xmlns => 'http://www.w3.org/2000/svg', :version => '1.1') do |xml|
69
+
70
+ xml.g(:fill => 'black', :stroke => 'black', 'stroke-width' => '2',
71
+ 'font-family' => 'Helvetica, Arial, sans-serif', 'font-size' => '10px', 'font-weight' => 'medium') do |xml|
72
+
73
+ # Outline
74
+ xml.rect(:x => @options[:margin][:left], :y => @options[:margin][:top],
75
+ :width => graph_width,
76
+ :height => graph_height,
77
+ :fill => 'lightgray')
78
+
79
+ xml.g 'stroke-width' => '1' do |xml|
80
+
81
+ # Title
82
+ xml.text(@options[:title], 'font-size' => '24px', :x => (@options[:viewbox][:width] / 2.0).round, :y => (@options[:subtitle] ? 24 : 32), 'text-anchor' => 'middle') if @options[:title]
83
+ xml.text(@options[:subtitle], 'font-size' => '18px', :x => (@options[:viewbox][:width] / 2.0).round, :y => 34, 'text-anchor' => 'middle') if @options[:subtitle]
84
+
85
+ # Lines
86
+ xml.g 'font-size' => '10px' do |xml|
87
+ line_x1 = @options[:margin][:left] + 1
88
+ line_x2 = @options[:viewbox][:width] - @options[:margin][:right] - 1
89
+
90
+ text_x = @options[:margin][:left] - 25
91
+
92
+ xml.text 0, :x => text_x, :y => (@options[:viewbox][:height] - @options[:margin][:bottom] + 4), 'stroke-width' => 0.5
93
+
94
+ 1.upto((max / 10) - 1) do |line_number|
95
+ y = (@options[:margin][:top] + (line_number * dy)).round
96
+ xml.line :x1 => line_x1, :y1 => y, :x2 => line_x2, :y2 => y, :stroke => '#666666'
97
+ xml.text max - line_number * 10, :x => text_x, :y => y + 4, 'stroke-width' => 0.5
98
+
99
+ # Smaller Line
100
+ xml.line(:x1 => line_x1, :y1 => y + (0.5 * dy), :x2 => line_x2, :y2 => y + (0.5 * dy), :stroke => '#999999') if max < 55
101
+ end
102
+
103
+ xml.text max, :x => text_x, :y => @options[:margin][:top] + 4, 'stroke-width' => 0.5
104
+ # Smaller Line
105
+ xml.line(:x1 => line_x1, :y1 => @options[:margin][:top] + (0.5 * dy), :x2 => line_x2, :y2 => @options[:margin][:top] + (0.5 * dy), :stroke => '#999999') if max < 55
106
+ end
107
+
108
+ # Labels
109
+ xml.g 'text-anchor' => 'end', 'font-size' => '12px', 'stroke-width' => 0.3 do |xml|
110
+ @labels.each_with_index do |label, index|
111
+ x = (@options[:margin][:left] + (dx * index) + (dx / 2.0)).round
112
+ y = @options[:viewbox][:height] - @options[:margin][:bottom] + 15
113
+ xml.text label, :x => x, :y => y, :transform => "rotate(-45 #{x} #{y})"
114
+ end
115
+ end
116
+
117
+ # Bars
118
+ xml.g 'font-size' => '8px', 'stroke-width' => 0.3 do |xml|
119
+ @data.each_with_index do |data, data_index|
120
+ data = Array(data)
121
+ width = dx - @options[:padding]
122
+ bar_width = (width / Float(data.size)).round
123
+
124
+ x = (@options[:margin][:left] + (dx * data_index)).round
125
+
126
+ # Rectangles
127
+ data.each_with_index do |number, number_index|
128
+ color = Color::HSL.from_fraction(data_index * (1.0 / @data.size),1.0, 0.4 + (number_index * 0.2)).to_rgb
129
+ height = ((dy / 10.0) * number).round
130
+
131
+ bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
132
+ bar_y = @options[:viewbox][:height] - @options[:margin][:bottom] - height
133
+
134
+ xml.rect :fill => color.html, :stroke => color.html, 'stroke-width' => 0, :x => bar_x, :width => bar_width, :y => bar_y, :height => height - 1
135
+ end
136
+
137
+ # Text
138
+ if @options[:bar_text]
139
+ data.each_with_index do |number, number_index|
140
+ height = ((dy / 10.0) * number).round
141
+
142
+ bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
143
+ text_x = (bar_x + (bar_width / 2.0)).round
144
+
145
+ bar_y = @options[:viewbox][:height] - @options[:margin][:bottom] - height
146
+ text_y = bar_y - 3
147
+
148
+ xml.text number, :x => text_x, :y => text_y, 'text-anchor' => 'middle'
149
+ end
150
+ end
151
+ end
152
+ end
153
+
154
+ # Yield in case they want to do some custom drawing and have a block ready
155
+ yield xml, @options if block_given?
156
+
157
+ end
158
+ end
159
+ end
160
+
161
+ output
162
+ end
163
+
164
+ private
165
+
166
+ def max
167
+ ((((@data.map do |num|
168
+ num.respond_to?(:max) ? num.max : num
169
+ end.max || 0) + 5) / 10.0).ceil * 10).round
170
+ end
171
+
172
+ def graph_height
173
+ @options[:viewbox][:height] - (@options[:margin][:top] + @options[:margin][:bottom])
174
+ end
175
+
176
+ def graph_width
177
+ @options[:viewbox][:width] - (@options[:margin][:left] + @options[:margin][:right])
178
+ end
179
+
180
+ def number_of_slots
181
+ @data.size
182
+ end
183
+
184
+ def dx
185
+ graph_height / Float(number_of_slots)
186
+ end
187
+
188
+ def dy
189
+ (graph_width * 10.0) / Float(max)
190
+ end
191
+ end
data/lilygraph.gemspec ADDED
@@ -0,0 +1,46 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{lilygraph}
8
+ s.version = "0.2.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Christopher Giroir"]
12
+ s.date = %q{2009-08-19}
13
+ s.description = %q{Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder.}
14
+ s.email = %q{kelsin@valefor.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/lilygraph.rb",
26
+ "lilygraph.gemspec"
27
+ ]
28
+ s.homepage = %q{http://github.com/Kelsin/lilygraph}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.4}
32
+ s.summary = %q{Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder.}
33
+
34
+ if s.respond_to? :specification_version then
35
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
39
+ s.add_runtime_dependency(%q<color-tools>, [">= 1.3.0"])
40
+ else
41
+ s.add_dependency(%q<color-tools>, [">= 1.3.0"])
42
+ end
43
+ else
44
+ s.add_dependency(%q<color-tools>, [">= 1.3.0"])
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Kelsin-Kelsin-lilygraph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Giroir
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-19 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: color-tools
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.3.0
24
+ version:
25
+ description: Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder.
26
+ email: kelsin@valefor.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README
34
+ files:
35
+ - .gitignore
36
+ - Kelsin-lilygraph.gemspec
37
+ - LICENSE
38
+ - README
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/lilygraph.rb
42
+ - lilygraph.gemspec
43
+ has_rdoc: false
44
+ homepage: http://github.com/Kelsin/lilygraph
45
+ licenses:
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder.
70
+ test_files: []
71
+