svg_charts 1.04 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9920997d912efe6b187a38cdd73c986820f5e03
4
- data.tar.gz: 79c7fa131050c51b12a6be59270ffb4721cbc2cf
3
+ metadata.gz: 2ef2bf97f73e4421d0c5f21d46c15edc2a17e143
4
+ data.tar.gz: 53d3e6e0150077460727318a64b8c7b0f2c3dfd5
5
5
  SHA512:
6
- metadata.gz: cf9df50976608b2a741c768dc0fd3358fb7c205c5454912595d4f4d46946132c54eda5b1e50dd20f759e56570021f409ccf33c9c3949f85cc471c560f62a4694
7
- data.tar.gz: 20aa4daebd225210fc5fffa7f4f4883d9919edcfa7f75c376b50706aa28e4f075891736c0873ac8748817e1b7bbd9004968046ae16670cb04193d30e6037a519
6
+ metadata.gz: e479de15f2056ef6447f8ef1248bebb8eb6a6c914643018c9daefe7c6037e02cde5d48a561696ec62dcc4f66ebeb31094fd70e19ae3ea02736c0fff0512ef69d
7
+ data.tar.gz: bddead4d0d98523a0367c104353f965c8d53336198949c135b67539adb4e2214bfc277703eaf3e24fedba21e83cfd24aa87374e26b6418dc2756620c563ebb5d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- svg_charts (1.03)
4
+ svg_charts (1.04)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/lib/charts/bar.rb ADDED
@@ -0,0 +1,127 @@
1
+ module SVGCharts
2
+ class Bar
3
+ include SVGElements
4
+
5
+ def initialize(options)
6
+ validation options, [:width, :height, :y_retreat, :x_retreat]
7
+
8
+ @width = options[:width]
9
+ @height = options[:height]
10
+
11
+ @y_retreat = options[:y_retreat]
12
+ @x_retreat = options[:x_retreat]
13
+ end
14
+
15
+ def scale(options)
16
+ validation options, [:y_label, :x_label]
17
+
18
+ scale = line({
19
+ :x1 => @x_retreat - 8,
20
+ :x2 => @x_retreat - 8,
21
+ :y1 => @y_retreat - 2,
22
+ :y2 => @height + 13,
23
+ :line_width => 2,
24
+ :color => "#aaa"
25
+ })
26
+
27
+ scale << line({
28
+ :x1 => 0,
29
+ :x2 => @width,
30
+ :y1 => @height,
31
+ :y2 => @height,
32
+ :line_width => 2,
33
+ :color => "#aaa"
34
+ })
35
+
36
+ scale << text({
37
+ :x => @width/2 - (options[:y_label].length * 2),
38
+ :y => @height + 30,
39
+ :color => "#bbb",
40
+ :font_size => 11,
41
+ :font_weight => "bold",
42
+ :value => options[:x_label]
43
+ })
44
+
45
+ scale << text_with_rotation({
46
+ :x => @height/2 - (options[:y_label].length * 2),
47
+ :y => 8,
48
+ :color => "#bbb",
49
+ :font_size => 11,
50
+ :font_weight => "bold",
51
+ :rotate => "270 #{@height/2} #{@height/2}",
52
+ :value => options[:y_label]
53
+ })
54
+
55
+ scale
56
+ end
57
+
58
+ def draw(options)
59
+ validation options, [:scale, :data, :data_color, :show_scale]
60
+
61
+ calculate_coordinates options[:data]
62
+
63
+ elements = ""
64
+
65
+ @coordinates.each_with_index do |coordinate, i|
66
+ elements << rect({
67
+ :x => coordinate[0] * 1.025,
68
+ :y => coordinate[2],
69
+ :width => coordinate[1] - coordinate[0],
70
+ :height => @height - coordinate[2],
71
+ :color => options[:data_color]
72
+ })
73
+
74
+ elements << text({
75
+ :x => (((coordinate[1] - coordinate[0]) - options[:scale][i].length)/2) + (coordinate[0] * 1.025) - 10,
76
+ :y => coordinate[2] - 10,
77
+ :color => options[:data_color],
78
+ :font_size => 11,
79
+ :font_weight => "bold",
80
+ :value => options[:data][i]
81
+ })
82
+
83
+ if options[:show_scale]
84
+ elements << text({
85
+ :x => (((coordinate[1] - coordinate[0]) - options[:scale][i].length)/2) + (coordinate[0] * 1.025) - 10,
86
+ :y => @height + 12,
87
+ :color => "#999",
88
+ :font_size => 10,
89
+ :font_weight => "normal",
90
+ :value => options[:scale][i]
91
+ })
92
+ end
93
+ end
94
+
95
+ elements
96
+ end
97
+
98
+ private
99
+ def calculate_coordinates(data)
100
+ @coordinates = []
101
+
102
+ y_positions = (@height - @y_retreat)/data.max
103
+ x_positions = (@width - (@x_retreat * 2))/data.size
104
+
105
+ data.each_with_index do |value, i|
106
+ x1 = @x_retreat + (x_positions * i)
107
+ y1 = @height - (y_positions * value)
108
+
109
+ unless data[i + 1].nil?
110
+ x2 = @x_retreat + (x_positions * (i + 1))
111
+ else
112
+ x2 = @width - (@x_retreat + 12)
113
+ end
114
+
115
+ @coordinates << [x1.abs, x2.abs, y1.abs]
116
+ end
117
+ end
118
+
119
+ def validation(options, params)
120
+ raise ArgumentError if options.empty?
121
+
122
+ params.each do |key|
123
+ raise ArgumentError unless options.has_key?(key)
124
+ end
125
+ end
126
+ end
127
+ end
@@ -1,3 +1,3 @@
1
1
  module SVGCharts
2
- VERSION = "1.04"
2
+ VERSION = "2.0"
3
3
  end
data/lib/svg_charts.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require "charts/elements"
2
+ require "charts/bar"
2
3
  require "charts/line"
3
4
  require "charts/version"
@@ -0,0 +1,130 @@
1
+ require "spec_helper"
2
+
3
+ describe SVGCharts::Bar do
4
+ context "initialize object" do
5
+ context "missing parameters" do
6
+ it "when hash empty" do
7
+ expect { chart = SVGCharts::Bar.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::Bar.new options }.to raise_error(ArgumentError)
17
+ end
18
+ end
19
+ end
20
+
21
+ def count_words(expression, target)
22
+ counter = 0
23
+
24
+ expression.split.each do |word|
25
+ counter += 1 if word.include? target
26
+ end
27
+
28
+ counter
29
+ end
30
+
31
+ context "set scale" do
32
+ before do
33
+ @svg_chart = SVGCharts::Bar.new({
34
+ :height => 300,
35
+ :width => 300,
36
+ :y_retreat => 20,
37
+ :x_retreat => 20
38
+ })
39
+ end
40
+
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
+ }
50
+
51
+ expect { chart = @svg_chart.scale options }.to raise_error(ArgumentError)
52
+ end
53
+ end
54
+
55
+ context "when generated" do
56
+ it "check values" do
57
+ scale = @svg_chart.scale({
58
+ :y_label => "Label1",
59
+ :x_label => "Label2"
60
+ })
61
+
62
+ scale = scale.gsub( /<[^>]*>/, " ").split
63
+ scale.should include("Label1")
64
+ scale.should include("Label2")
65
+ end
66
+
67
+ it "check elements" do
68
+ scale = @svg_chart.scale({
69
+ :y_label => "Label1",
70
+ :x_label => "Label2"
71
+ })
72
+
73
+ count_words(scale, "<line").should == 2
74
+ count_words(scale, "<text").should == 2
75
+ end
76
+ end
77
+ end
78
+
79
+ context "set chart" do
80
+ before do
81
+ @svg_chart = SVGCharts::Bar.new({
82
+ :height => 300,
83
+ :width => 300,
84
+ :y_retreat => 20,
85
+ :x_retreat => 20
86
+ })
87
+ end
88
+
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
+ }
99
+
100
+ expect { chart = @svg_chart.draw options }.to raise_error(ArgumentError)
101
+ end
102
+ end
103
+
104
+ context "when generated" do
105
+ it "check elements with no scale and dashed" do
106
+ chart = @svg_chart.draw({
107
+ :scale => ["A", "B", "C"],
108
+ :data => [10, 20, 30],
109
+ :data_color => "#000000",
110
+ :show_scale => false
111
+ })
112
+
113
+ count_words(chart, "<rect").should == 3
114
+ count_words(chart, "<text").should == 3
115
+ end
116
+
117
+ it "check elements with scale and dashed" do
118
+ chart = @svg_chart.draw({
119
+ :scale => ["A", "B", "C"],
120
+ :data => [10, 20, 30],
121
+ :data_color => "#000000",
122
+ :show_scale => true
123
+ })
124
+
125
+ count_words(chart, "<rect").should == 3
126
+ count_words(chart, "<text").should == 6
127
+ end
128
+ end
129
+ end
130
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg_charts
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.04'
4
+ version: '2.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rogério Zambon
@@ -35,10 +35,12 @@ files:
35
35
  - Gemfile
36
36
  - Gemfile.lock
37
37
  - README.md
38
+ - lib/charts/bar.rb
38
39
  - lib/charts/elements.rb
39
40
  - lib/charts/line.rb
40
41
  - lib/charts/version.rb
41
42
  - lib/svg_charts.rb
43
+ - spec/charts/bar_spec.rb
42
44
  - spec/charts/line_spec.rb
43
45
  - spec/spec_helper.rb
44
46
  - svg_charts.gemspec