svg_charts 1.0 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54e2d61c5b862d2563636268a0b6f257a1eb4512
4
+ data.tar.gz: 3b8f4b7399292b8f92e75ec201c829f42b1db1fb
5
+ SHA512:
6
+ metadata.gz: f461cce55168620110f26c085dadeff3c75b977c86379be58e0b88abb5436720186bb204ebd22159eb4f974de6414d8bec1a8c2f765fb7b5f0164e5db3bea621
7
+ data.tar.gz: 941dd3606d129893f975b1728431a8b0b24826c9be194c9acbfdf0fd94a2074efc26181d900ab14ae524c5f5095d003be83a48828c8eba7ca9d9a86fe5d44619
data/.rspec CHANGED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ svg_charts (1.0.3)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.1)
10
+ rspec (2.13.0)
11
+ rspec-core (~> 2.13.0)
12
+ rspec-expectations (~> 2.13.0)
13
+ rspec-mocks (~> 2.13.0)
14
+ rspec-core (2.13.1)
15
+ rspec-expectations (2.13.0)
16
+ diff-lcs (>= 1.1.3, < 2.0)
17
+ rspec-mocks (2.13.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ rspec
24
+ svg_charts!
@@ -1,47 +1,56 @@
1
- = SVGCharts
1
+ # SVGCharts
2
2
 
3
3
  Draw charts using SVG.
4
4
 
5
- == Installation
5
+ ## Installation
6
6
 
7
7
  gem install svg_charts
8
8
 
9
- == Usage
9
+ ## Usage
10
10
 
11
- # file.rb
12
- require "rubygems"
13
- require "svg_charts"
11
+ ~~~.ruby
12
+ #=> file.rb
14
13
 
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
- })
14
+ require "rubygems"
15
+ require "svg_charts"
26
16
 
27
- @scale = line_chart.draw_scale
28
- @chart = line_chart.draw_chart
17
+ line = SVGCharts::Line.new({
18
+ :height => 300,
19
+ :width => 300,
20
+ :y_retreat => 20,
21
+ :x_retreat => 20
22
+ })
29
23
 
30
- # file.html
31
- <svg width="680" height="350">
32
- <%= @scale %>
33
- <%= @chart %>
34
- </svg>
24
+ @scale = line.scale({
25
+ :y_label => "Numbers",
26
+ :x_label => "Letters"
27
+ })
35
28
 
36
- == Maintainer
29
+ @chart = line.draw({
30
+ :scale => ["A", "B", "C"],
31
+ :data => [10, 20, 30],
32
+ :data_color => "#000000",
33
+ :show_scale => true,
34
+ :show_dashed => true
35
+ })
36
+ ~~~
37
37
 
38
- * Rogério Zambon (http://rogeriozambon.com)
38
+ # file.erb
39
39
 
40
- == Collaborators
40
+ <svg width="680" height="350">
41
+ <%= @scale %>
42
+ <%= @chart %>
43
+ </svg>
41
44
 
42
- * André Ronix (http://www.linkedin.com/pub/andr%C3%A9-s-ronix/27/212/3b2)
45
+ ## Maintainer
43
46
 
44
- == License
47
+ * Rogério Zambon (http://rogerio.me)
48
+
49
+ ## Collaborators
50
+
51
+ * André Ronix (http://www.linkedin.com/pub/andr%C3%A9-s-ronix/27/212/3b2): Generated the coordinates calc.
52
+
53
+ ## License
45
54
 
46
55
  (The MIT License)
47
56
 
File without changes
data/lib/charts/line.rb CHANGED
@@ -2,31 +2,20 @@ module SVGCharts
2
2
  class Line
3
3
  include SVGElements
4
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
5
+ def initialize(options)
6
+ validation options, [:width, :height, :y_retreat, :x_retreat]
11
7
 
12
8
  @width = options[:width]
13
9
  @height = options[:height]
14
10
 
15
11
  @y_retreat = options[:y_retreat]
16
- @y_label = options[:y_label]
17
-
18
12
  @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
13
  end
25
14
 
26
- def draw_scale
27
- scales = ""
15
+ def scale(options)
16
+ validation options, [:y_label, :x_label]
28
17
 
29
- scales << line({
18
+ scale = line({
30
19
  :x1 => @x_retreat - 8,
31
20
  :x2 => @x_retreat - 8,
32
21
  :y1 => @y_retreat - 2,
@@ -35,7 +24,7 @@ module SVGCharts
35
24
  :color => "#aaa"
36
25
  })
37
26
 
38
- scales << line({
27
+ scale << line({
39
28
  :x1 => 0,
40
29
  :x2 => @width,
41
30
  :y1 => @height,
@@ -44,30 +33,32 @@ module SVGCharts
44
33
  :color => "#aaa"
45
34
  })
46
35
 
47
- scales << text({
48
- :x => @width/2 - (@y_label.length * 2),
36
+ scale << text({
37
+ :x => @width/2 - (options[:y_label].length * 2),
49
38
  :y => @height + 30,
50
39
  :color => "#bbb",
51
40
  :font_size => 11,
52
41
  :font_weight => "bold",
53
- :value => @x_label
42
+ :value => options[:x_label]
54
43
  })
55
44
 
56
- scales << text_with_rotation({
57
- :x => @height/2 - (@y_label.length * 2),
45
+ scale << text_with_rotation({
46
+ :x => @height/2 - (options[:y_label].length * 2),
58
47
  :y => 8,
59
48
  :color => "#bbb",
60
49
  :font_size => 11,
61
50
  :font_weight => "bold",
62
51
  :rotate => "270 #{@height/2} #{@height/2}",
63
- :value => @y_label
52
+ :value => options[:y_label]
64
53
  })
65
54
 
66
- scales
55
+ scale
67
56
  end
68
57
 
69
- def draw_chart
70
- calculate_coordinates
58
+ def draw(options)
59
+ validation options, [:scale, :data, :data_color, :show_scale, :show_dashed]
60
+
61
+ calculate_coordinates options[:data]
71
62
 
72
63
  elements = ""
73
64
 
@@ -78,17 +69,19 @@ module SVGCharts
78
69
  :y1 => coordinate[2],
79
70
  :y2 => coordinate[3],
80
71
  :line_width => 1,
81
- :color => "#999"
72
+ :color => options[:data_color]
82
73
  })
83
74
 
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
- })
75
+ if options[:show_dashed]
76
+ elements << line_dashed({
77
+ :x1 => coordinate[0],
78
+ :x2 => coordinate[0],
79
+ :y1 => @height,
80
+ :y2 => coordinate[2],
81
+ :line_width => 1,
82
+ :color => "#b9b9b9"
83
+ })
84
+ end
92
85
 
93
86
  elements << circle({
94
87
  :x => coordinate[0],
@@ -102,39 +95,41 @@ module SVGCharts
102
95
  elements << text({
103
96
  :x => coordinate[0] - 10,
104
97
  :y => coordinate[2] - 10,
105
- :color => @data_color,
98
+ :color => options[:data_color],
106
99
  :font_size => 11,
107
100
  :font_weight => "bold",
108
- :value => @data[i]
101
+ :value => options[:data][i]
109
102
  })
110
103
 
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
- })
104
+ if options[:show_scale]
105
+ elements << text({
106
+ :x => coordinate[0] - 5,
107
+ :y => @height + 12,
108
+ :color => "#999",
109
+ :font_size => 10,
110
+ :font_weight => "normal",
111
+ :value => options[:scale][i]
112
+ })
113
+ end
119
114
  end
120
115
 
121
116
  elements
122
117
  end
123
118
 
124
119
  private
125
- def calculate_coordinates
120
+ def calculate_coordinates(data)
126
121
  @coordinates = []
127
122
 
128
- y_positions = (@height - @y_retreat)/@data.max
129
- x_positions = @width/@data.size
123
+ y_positions = (@height - @y_retreat)/data.max
124
+ x_positions = @width/data.size
130
125
 
131
- @data.each_with_index do |value, i|
126
+ data.each_with_index do |value, i|
132
127
  x1 = @x_retreat + (x_positions * i)
133
128
  y1 = @height - (y_positions * value)
134
129
 
135
- unless @data[i + 1].nil?
130
+ unless data[i + 1].nil?
136
131
  x2 = @x_retreat + (x_positions * (i + 1))
137
- y2 = @height - (y_positions * @data[i + 1])
132
+ y2 = @height - (y_positions * data[i + 1])
138
133
  else
139
134
  x2 = x1
140
135
  y2 = y1
@@ -143,5 +138,13 @@ module SVGCharts
143
138
  @coordinates << [x1.abs, x2.abs, y1.abs, y2.abs]
144
139
  end
145
140
  end
141
+
142
+ def validation(options, params)
143
+ raise ArgumentError if options.empty?
144
+
145
+ params.each do |key|
146
+ raise ArgumentError unless options.has_key?(key)
147
+ end
148
+ end
146
149
  end
147
150
  end
@@ -0,0 +1,3 @@
1
+ module SVGCharts
2
+ VERSION = "1.0.3"
3
+ end
data/lib/svg_charts.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "charts/elements"
2
2
  require "charts/line"
3
+ require "charts/version"
@@ -1,79 +1,135 @@
1
1
  require "spec_helper"
2
2
 
3
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)
4
+ context "initialize object" do
5
+ context "missing parameters" do
6
+ it "when hash empty" do
7
+ expect { chart = SVGCharts::Line.new }.to raise_error(ArgumentError)
8
+ end
9
+
10
+ it "when the hash keys is incomplete" do
11
+ options = {
12
+ :width => 300,
13
+ :height => 300
14
+ }
15
+
16
+ expect { chart = SVGCharts::Line.new options }.to raise_error(ArgumentError)
17
+ end
7
18
  end
19
+ end
8
20
 
9
- it "when the hash keys is incomplete" do
10
- options = {
11
- :width => 300,
12
- :height => 300
13
- }
21
+ def count_words(expression, target)
22
+ counter = 0
14
23
 
15
- expect { chart = SVGCharts::Line.new options }.to raise_error(ArgumentError)
24
+ expression.split.each do |word|
25
+ counter += 1 if word.include? target
16
26
  end
27
+
28
+ counter
17
29
  end
18
30
 
19
- context "when generated" do
31
+ context "set scale" do
20
32
  before do
21
- @options = {
22
- :height => 320,
23
- :width => 680,
33
+ @svg_chart = SVGCharts::Line.new({
34
+ :height => 300,
35
+ :width => 300,
24
36
  :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
- }
37
+ :x_retreat => 20
38
+ })
32
39
  end
33
40
 
34
- it "check chart values" do
35
- line_chart = SVGCharts::Line.new(@options).draw_chart
41
+ context "missing parameters" do
42
+ it "when hash empty" do
43
+ expect { chart = @svg_chart.scale }.to raise_error(ArgumentError)
44
+ end
45
+
46
+ it "when the hash keys is incomplete" do
47
+ options = {
48
+ :y_label => "First label"
49
+ }
36
50
 
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")
51
+ expect { chart = @svg_chart.scale options }.to raise_error(ArgumentError)
52
+ end
44
53
  end
45
54
 
46
- it "check scale values" do
47
- line_chart = SVGCharts::Line.new(@options).draw_scale
55
+ context "when generated" do
56
+ it "check values" do
57
+ scale = @svg_chart.scale({
58
+ :y_label => "Label1",
59
+ :x_label => "Label2"
60
+ })
48
61
 
49
- values = line_chart.gsub( /<[^>]*>/, " ").split
50
- values.should include("Letters")
51
- values.should include("Numbers")
52
- end
62
+ scale = scale.gsub( /<[^>]*>/, " ").split
63
+ scale.should include("Label1")
64
+ scale.should include("Label2")
65
+ end
53
66
 
54
- def count_words(expression, target)
55
- counter = 0
67
+ it "check elements" do
68
+ scale = @svg_chart.scale({
69
+ :y_label => "Label1",
70
+ :x_label => "Label2"
71
+ })
56
72
 
57
- expression.split.each do |word|
58
- counter += 1 if word.include? target
73
+ count_words(scale, "<line").should == 2
74
+ count_words(scale, "<text").should == 2
59
75
  end
76
+ end
77
+ end
60
78
 
61
- counter
79
+ context "set chart" do
80
+ before do
81
+ @svg_chart = SVGCharts::Line.new({
82
+ :height => 300,
83
+ :width => 300,
84
+ :y_retreat => 20,
85
+ :x_retreat => 20
86
+ })
62
87
  end
63
88
 
64
- it "check chart elements" do
65
- line_chart = SVGCharts::Line.new(@options).draw_chart
89
+ context "missing parameters" do
90
+ it "when hash empty" do
91
+ expect { chart = @svg_chart.draw }.to raise_error(ArgumentError)
92
+ end
93
+
94
+ it "when the hash keys is incomplete" do
95
+ options = {
96
+ :data_color => "#000000",
97
+ :show_scale => false,
98
+ :show_dashed => false
99
+ }
66
100
 
67
- count_words(line_chart, "<line").should == 6
68
- count_words(line_chart, "<text").should == 6
69
- count_words(line_chart, "<circle").should == 3
101
+ expect { chart = @svg_chart.draw options }.to raise_error(ArgumentError)
102
+ end
70
103
  end
71
104
 
72
- it "check scale elements" do
73
- line_chart = SVGCharts::Line.new(@options).draw_scale
105
+ context "when generated" do
106
+ it "check elements with no scale and dashed" do
107
+ chart = @svg_chart.draw({
108
+ :scale => ["A", "B", "C"],
109
+ :data => [10, 20, 30],
110
+ :data_color => "#000000",
111
+ :show_scale => false,
112
+ :show_dashed => false
113
+ })
114
+
115
+ count_words(chart, "<line").should == 3
116
+ count_words(chart, "<text").should == 3
117
+ count_words(chart, "<circle").should == 3
118
+ end
119
+
120
+ it "check elements with scale and dashed" do
121
+ chart = @svg_chart.draw({
122
+ :scale => ["A", "B", "C"],
123
+ :data => [10, 20, 30],
124
+ :data_color => "#000000",
125
+ :show_scale => true,
126
+ :show_dashed => true
127
+ })
74
128
 
75
- count_words(line_chart, "<line").should == 2
76
- count_words(line_chart, "<text").should == 2
129
+ count_words(chart, "<line").should == 6
130
+ count_words(chart, "<text").should == 6
131
+ count_words(chart, "<circle").should == 3
132
+ end
77
133
  end
78
134
  end
79
135
  end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,6 @@
1
- Dir[File.expand_path("../../lib/charts", __FILE__) + "/*.rb"].each { |file| require file }
1
+ require "bundler/setup"
2
+ Bundler.require :default, :development
3
+
4
+ require "svg_charts"
5
+ require "charts/elements"
6
+ require "charts/line"
data/svg_charts.gemspec CHANGED
@@ -1,24 +1,19 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "charts/version"
4
+
2
5
  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"
6
+ s.name = "svg_charts"
7
+ s.version = SVGCharts::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Rogério Zambon"]
10
+ s.email = ["rogeriozambon@gmail.com"]
11
+ s.homepage = "http://github.com/rogeriozambon/svg_charts"
12
+ s.summary = "Draw charts using SVG."
13
+ s.description = s.summary
14
+
15
+ s.files = `git ls-files`.split("\n")
20
16
  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"
17
+
18
+ s.add_development_dependency "rspec"
24
19
  end
metadata CHANGED
@@ -1,53 +1,68 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg_charts
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
5
- prerelease:
4
+ version: 1.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rogério Zambon
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-25 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2013-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
14
27
  description: Draw charts using SVG.
15
- email: rogeriozambon@gmail.com
28
+ email:
29
+ - rogeriozambon@gmail.com
16
30
  executables: []
17
31
  extensions: []
18
32
  extra_rdoc_files: []
19
33
  files:
20
34
  - .rspec
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - README.md
21
38
  - lib/charts/elements.rb
22
39
  - lib/charts/line.rb
40
+ - lib/charts/version.rb
23
41
  - lib/svg_charts.rb
24
- - spec/spec_helper.rb
25
42
  - spec/charts/line_spec.rb
26
- - VERSION
27
- - README.rdoc
43
+ - spec/spec_helper.rb
28
44
  - svg_charts.gemspec
29
45
  homepage: http://github.com/rogeriozambon/svg_charts
30
46
  licenses: []
47
+ metadata: {}
31
48
  post_install_message:
32
49
  rdoc_options: []
33
50
  require_paths:
34
51
  - lib
35
52
  required_ruby_version: !ruby/object:Gem::Requirement
36
- none: false
37
53
  requirements:
38
- - - ! '>='
54
+ - - '>='
39
55
  - !ruby/object:Gem::Version
40
56
  version: '0'
41
57
  required_rubygems_version: !ruby/object:Gem::Requirement
42
- none: false
43
58
  requirements:
44
- - - ! '>='
59
+ - - '>='
45
60
  - !ruby/object:Gem::Version
46
- version: 1.8.23
61
+ version: '0'
47
62
  requirements: []
48
63
  rubyforge_project:
49
- rubygems_version: 1.8.23
64
+ rubygems_version: 2.0.0
50
65
  signing_key:
51
- specification_version: 3
66
+ specification_version: 4
52
67
  summary: Draw charts using SVG.
53
68
  test_files: []
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.0