svg_charts 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = SVGCharts
2
+
3
+ Draw charts using SVG.
4
+
5
+ == Installation
6
+
7
+ gem install svg_charts
8
+
9
+ == Usage
10
+
11
+ # file.rb
12
+ require "rubygems"
13
+ require "svg_charts"
14
+
15
+ line_chart = SVGCharts::Line.new({
16
+ :height => 320,
17
+ :width => 680,
18
+ :y_retreat => 20,
19
+ :y_label => "Numbers",
20
+ :x_retreat => 20,
21
+ :x_label => "Letters",
22
+ :x_scale => ["A", "B", "C"],
23
+ :data => [10, 20, 30],
24
+ :data_color => "#000000"
25
+ })
26
+
27
+ @scale = line_chart.draw_scale
28
+ @chart = line_chart.draw_chart
29
+
30
+ # file.html
31
+ <svg width="680" height="350">
32
+ <%= @scale %>
33
+ <%= @chart %>
34
+ </svg>
35
+
36
+ == Maintainer
37
+
38
+ * Rogério Zambon (http://rogeriozambon.com)
39
+
40
+ == Collaborators
41
+
42
+ * André Ronix (http://www.linkedin.com/pub/andr%C3%A9-s-ronix/27/212/3b2)
43
+
44
+ == License
45
+
46
+ (The MIT License)
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0
@@ -0,0 +1,25 @@
1
+ module SVGElements
2
+ def line(params = {})
3
+ "<line x1=\"#{params[:x1]}\" y1=\"#{params[:y1]}\" x2=\"#{params[:x2]}\" y2=\"#{params[:y2]}\" stroke-width=\"#{params[:line_width]}\" stroke=\"#{params[:color]}\" />"
4
+ end
5
+
6
+ def line_dashed(params = {})
7
+ "<line x1=\"#{params[:x1]}\" y1=\"#{params[:y1]}\" x2=\"#{params[:x2]}\" y2=\"#{params[:y2]}\" stroke-width=\"#{params[:line_width]}\" stroke=\"#{params[:color]}\" stroke-dasharray=\"5,5\" />"
8
+ end
9
+
10
+ def circle(params = {})
11
+ "<circle cx=\"#{params[:x]}\" cy=\"#{params[:y]}\" r=\"#{params[:radius]}\" stroke=\"#{params[:line_color]}\" stroke-width=\"#{params[:line_width]}\" fill=\"#{params[:background]}\" />"
12
+ end
13
+
14
+ def rect(params = {})
15
+ "<rect x=\"#{params[:x]}\" y=\"#{params[:y]}\" width=\"#{params[:width]}\" height=\"#{params[:height]}\" fill=\"#{params[:color]}\" />"
16
+ end
17
+
18
+ def text(params = {})
19
+ "<text x=\"#{params[:x]}\" y=\"#{params[:y]}\" fill=\"#{params[:color]}\" font-size=\"#{params[:font_size]}\" font-weight=\"#{params[:font_weight]}\">#{params[:value]}</text>"
20
+ end
21
+
22
+ def text_with_rotation(params = {})
23
+ "<text x=\"#{params[:x]}\" y=\"#{params[:y]}\" fill=\"#{params[:color]}\" font-size=\"#{params[:font_size]}\" font-weight=\"#{params[:font_weight]}\" transform=\"rotate(#{params[:rotate]})\">#{params[:value]}</text>"
24
+ end
25
+ end
@@ -0,0 +1,147 @@
1
+ module SVGCharts
2
+ class Line
3
+ include SVGElements
4
+
5
+ def initialize(options = {})
6
+ raise ArgumentError if options.empty?
7
+
8
+ [:width, :height, :y_retreat, :y_label, :x_retreat, :x_label, :x_scale, :data, :data_color].each do |key|
9
+ raise ArgumentError unless options.has_key?(key)
10
+ end
11
+
12
+ @width = options[:width]
13
+ @height = options[:height]
14
+
15
+ @y_retreat = options[:y_retreat]
16
+ @y_label = options[:y_label]
17
+
18
+ @x_retreat = options[:x_retreat]
19
+ @x_label = options[:x_label]
20
+ @x_scale = options[:x_scale]
21
+
22
+ @data = options[:data]
23
+ @data_color = options[:data_color]
24
+ end
25
+
26
+ def draw_scale
27
+ scales = ""
28
+
29
+ scales << line({
30
+ :x1 => @x_retreat - 8,
31
+ :x2 => @x_retreat - 8,
32
+ :y1 => @y_retreat - 2,
33
+ :y2 => @height + 13,
34
+ :line_width => 2,
35
+ :color => "#aaa"
36
+ })
37
+
38
+ scales << line({
39
+ :x1 => 0,
40
+ :x2 => @width,
41
+ :y1 => @height,
42
+ :y2 => @height,
43
+ :line_width => 2,
44
+ :color => "#aaa"
45
+ })
46
+
47
+ scales << text({
48
+ :x => @width/2 - (@y_label.length * 2),
49
+ :y => @height + 30,
50
+ :color => "#bbb",
51
+ :font_size => 11,
52
+ :font_weight => "bold",
53
+ :value => @x_label
54
+ })
55
+
56
+ scales << text_with_rotation({
57
+ :x => @height/2 - (@y_label.length * 2),
58
+ :y => 8,
59
+ :color => "#bbb",
60
+ :font_size => 11,
61
+ :font_weight => "bold",
62
+ :rotate => "270 #{@height/2} #{@height/2}",
63
+ :value => @y_label
64
+ })
65
+
66
+ scales
67
+ end
68
+
69
+ def draw_chart
70
+ calculate_coordinates
71
+
72
+ elements = ""
73
+
74
+ @coordinates.each_with_index do |coordinate, i|
75
+ elements << line({
76
+ :x1 => coordinate[0],
77
+ :x2 => coordinate[1],
78
+ :y1 => coordinate[2],
79
+ :y2 => coordinate[3],
80
+ :line_width => 1,
81
+ :color => "#999"
82
+ })
83
+
84
+ elements << line_dashed({
85
+ :x1 => coordinate[0],
86
+ :x2 => coordinate[0],
87
+ :y1 => @height,
88
+ :y2 => coordinate[2],
89
+ :line_width => 1,
90
+ :color => "#b9b9b9"
91
+ })
92
+
93
+ elements << circle({
94
+ :x => coordinate[0],
95
+ :y => coordinate[2],
96
+ :radius => 2,
97
+ :background => "#fff",
98
+ :line_width => 2,
99
+ :line_color => "#444"
100
+ })
101
+
102
+ elements << text({
103
+ :x => coordinate[0] - 10,
104
+ :y => coordinate[2] - 10,
105
+ :color => @data_color,
106
+ :font_size => 11,
107
+ :font_weight => "bold",
108
+ :value => @data[i]
109
+ })
110
+
111
+ elements << text({
112
+ :x => coordinate[0] - 5,
113
+ :y => @height + 12,
114
+ :color => "#999",
115
+ :font_size => 10,
116
+ :font_weight => "normal",
117
+ :value => @x_scale[i]
118
+ })
119
+ end
120
+
121
+ elements
122
+ end
123
+
124
+ private
125
+ def calculate_coordinates
126
+ @coordinates = []
127
+
128
+ y_positions = (@height - @y_retreat)/@data.max
129
+ x_positions = @width/@data.size
130
+
131
+ @data.each_with_index do |value, i|
132
+ x1 = @x_retreat + (x_positions * i)
133
+ y1 = @height - (y_positions * value)
134
+
135
+ unless @data[i + 1].nil?
136
+ x2 = @x_retreat + (x_positions * (i + 1))
137
+ y2 = @height - (y_positions * @data[i + 1])
138
+ else
139
+ x2 = x1
140
+ y2 = y1
141
+ end
142
+
143
+ @coordinates << [x1.abs, x2.abs, y1.abs, y2.abs]
144
+ end
145
+ end
146
+ end
147
+ end
data/lib/svg_charts.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "charts/elements"
2
+ require "charts/line"
@@ -0,0 +1,79 @@
1
+ require "spec_helper"
2
+
3
+ describe SVGCharts::Line do
4
+ context "missing parameters" do
5
+ it "when hash empty" do
6
+ expect { chart = SVGCharts::Line.new }.to raise_error(ArgumentError)
7
+ end
8
+
9
+ it "when the hash keys is incomplete" do
10
+ options = {
11
+ :width => 300,
12
+ :height => 300
13
+ }
14
+
15
+ expect { chart = SVGCharts::Line.new options }.to raise_error(ArgumentError)
16
+ end
17
+ end
18
+
19
+ context "when generated" do
20
+ before do
21
+ @options = {
22
+ :height => 320,
23
+ :width => 680,
24
+ :y_retreat => 20,
25
+ :y_label => "Numbers",
26
+ :x_retreat => 20,
27
+ :x_label => "Letters",
28
+ :x_scale => ["A", "B", "C"],
29
+ :data => [10, 20, 30],
30
+ :data_color => "#000000"
31
+ }
32
+ end
33
+
34
+ it "check chart values" do
35
+ line_chart = SVGCharts::Line.new(@options).draw_chart
36
+
37
+ values = line_chart.gsub( /<[^>]*>/, " ").split
38
+ values.should include("10")
39
+ values.should include("20")
40
+ values.should include("30")
41
+ values.should include("A")
42
+ values.should include("B")
43
+ values.should include("C")
44
+ end
45
+
46
+ it "check scale values" do
47
+ line_chart = SVGCharts::Line.new(@options).draw_scale
48
+
49
+ values = line_chart.gsub( /<[^>]*>/, " ").split
50
+ values.should include("Letters")
51
+ values.should include("Numbers")
52
+ end
53
+
54
+ def count_words(expression, target)
55
+ counter = 0
56
+
57
+ expression.split.each do |word|
58
+ counter += 1 if word.include? target
59
+ end
60
+
61
+ counter
62
+ end
63
+
64
+ it "check chart elements" do
65
+ line_chart = SVGCharts::Line.new(@options).draw_chart
66
+
67
+ count_words(line_chart, "<line").should == 6
68
+ count_words(line_chart, "<text").should == 6
69
+ count_words(line_chart, "<circle").should == 3
70
+ end
71
+
72
+ it "check scale elements" do
73
+ line_chart = SVGCharts::Line.new(@options).draw_scale
74
+
75
+ count_words(line_chart, "<line").should == 2
76
+ count_words(line_chart, "<text").should == 2
77
+ end
78
+ end
79
+ end
@@ -0,0 +1 @@
1
+ Dir[File.expand_path("../../lib/charts", __FILE__) + "/*.rb"].each { |file| require file }
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.authors = ["Rogério Zambon"]
4
+ s.date = "2013-01-25"
5
+ s.description = "Draw charts using SVG."
6
+ s.email = "rogeriozambon@gmail.com"
7
+ s.files = [
8
+ ".rspec",
9
+ "lib/charts/elements.rb",
10
+ "lib/charts/line.rb",
11
+ "lib/svg_charts.rb",
12
+ "spec/spec_helper.rb",
13
+ "spec/charts/line_spec.rb",
14
+ "VERSION",
15
+ "README.rdoc",
16
+ "svg_charts.gemspec"
17
+ ]
18
+ s.homepage = "http://github.com/rogeriozambon/svg_charts"
19
+ s.name = "svg_charts"
20
+ s.require_paths = ["lib"]
21
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.8.23")
22
+ s.summary = s.description
23
+ s.version = "1.0"
24
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svg_charts
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rogério Zambon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Draw charts using SVG.
15
+ email: rogeriozambon@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .rspec
21
+ - lib/charts/elements.rb
22
+ - lib/charts/line.rb
23
+ - lib/svg_charts.rb
24
+ - spec/spec_helper.rb
25
+ - spec/charts/line_spec.rb
26
+ - VERSION
27
+ - README.rdoc
28
+ - svg_charts.gemspec
29
+ homepage: http://github.com/rogeriozambon/svg_charts
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.23
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.23
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Draw charts using SVG.
53
+ test_files: []