gchart 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,12 @@
1
+ == 0.4.0 (2007-12-28)
2
+
3
+ * New committer, Abhay Kumar
4
+ * Implement all three encodings. Fix extended encoding calculation.
5
+ * Support bar thickness/spacing for bar charts
6
+ * (bug #16565 fixed) GChart now tells you when your chart is too big.
7
+ * Pushed some bar chart params logic down where it belongs
8
+ * Enforce pie chart spec: pie charts only take one set of data
9
+
1
10
  == 0.3.0 (2007-12-24)
2
11
 
3
12
  * Breaking change: labels --> legend
data/Rakefile CHANGED
@@ -20,5 +20,12 @@ Spec::Rake::SpecTask.new do |t|
20
20
  t.spec_opts = ["--options", "spec/spec.opts"]
21
21
  end
22
22
 
23
+ desc "Run all specs and get coverage statistics"
24
+ Spec::Rake::SpecTask.new('spec:rcov') do |t|
25
+ t.spec_files = FileList["spec/**/*_spec.rb"]
26
+ t.rcov = true
27
+ t.spec_opts = ["--options", "spec/spec.opts"]
28
+ end
29
+
23
30
  Rake::Task[:default].prerequisites.clear
24
31
  task :default => :spec
@@ -6,8 +6,9 @@ end
6
6
 
7
7
  module GChart
8
8
  URL = "http://chart.apis.google.com/chart"
9
- CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.".split("")
10
- PAIRS = CHARS.collect { |first| CHARS.collect { |second| first + second } }.flatten
9
+ SIMPLE_CHARS = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
10
+ EXTENDED_CHARS = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + %w[- .]
11
+ EXTENDED_PAIRS = EXTENDED_CHARS.collect { |first| EXTENDED_CHARS.collect { |second| first + second } }.flatten
11
12
 
12
13
  class << self
13
14
  # Convenience constructor for GChart::Line.
@@ -34,12 +35,19 @@ module GChart
34
35
  # Encode +n+ as a string. +n+ is normalized based on +max+.
35
36
  # +encoding+ can currently only be :extended.
36
37
  def encode(encoding, n, max)
37
- unless encoding == :extended
38
+ case encoding
39
+ when :simple
40
+ return "_" if n.nil?
41
+ SIMPLE_CHARS[((n/max.to_f) * (SIMPLE_CHARS.size - 1)).round]
42
+ when :text
43
+ return "-1" if n.nil?
44
+ ((((n/max.to_f) * 1000.0).round)/10.0).to_s
45
+ when :extended
46
+ return "__" if n.nil?
47
+ EXTENDED_PAIRS[((n/max.to_f) * (EXTENDED_PAIRS.size - 1)).round]
48
+ else
38
49
  raise ArgumentError, "unsupported encoding: #{encoding.inspect}"
39
50
  end
40
-
41
- return "__" if n.nil?
42
- PAIRS[(n * (PAIRS.size - 1) / max).to_i]
43
51
  end
44
52
  end
45
53
  end
@@ -7,6 +7,9 @@ module GChart
7
7
 
8
8
  attr_reader :orientation
9
9
 
10
+ attr_reader :thickness
11
+ attr_reader :spacing
12
+
10
13
  def initialize(*args, &block)
11
14
  @grouped = false
12
15
  @orientation = :horizontal
@@ -30,6 +33,26 @@ module GChart
30
33
  @orientation == :vertical
31
34
  end
32
35
 
36
+ def thickness=(thickness)
37
+ raise ArgumentError, "Invalid thickness: #{thickness.inspect}" if thickness.nil? || thickness < 1
38
+ @thickness = thickness
39
+ end
40
+
41
+ def spacing=(spacing)
42
+ raise ArgumentError, "Invalid spacing: #{spacing.inspect}" if spacing.nil? || spacing < 1
43
+ @spacing = spacing
44
+ end
45
+
46
+ # overrides GChart::Base#query_params
47
+ def query_params(params={}) #:nodoc:
48
+ values = []
49
+ values << thickness if thickness
50
+ values << spacing if thickness && spacing
51
+
52
+ params["chbh"] = values.join(",") unless values.empty?
53
+ super(params)
54
+ end
55
+
33
56
  def render_chart_type #:nodoc:
34
57
  "b#{@orientation.to_s[0..0]}#{grouped? ? "g" : "s"}"
35
58
  end
@@ -37,15 +37,23 @@ module GChart
37
37
  yield(self) if block_given?
38
38
  end
39
39
 
40
- # Sets the chart's width, in pixels. Raises +ArgumentError+ if +width+ is less than 1.
40
+ # Sets the chart's width, in pixels. Raises +ArgumentError+
41
+ # if +width+ is less than 1 or greater than 1,000.
41
42
  def width=(width)
42
- raise ArgumentError, "Invalid width: #{width.inspect}" if width.nil? || width < 1
43
+ if width.nil? || width < 1 || width > 1_000
44
+ raise ArgumentError, "Invalid width: #{width.inspect}"
45
+ end
46
+
43
47
  @width = width
44
48
  end
45
49
 
46
- # Sets the chart's height, in pixels. Raises +ArgumentError+ if +height+ is less than 1.
50
+ # Sets the chart's height, in pixels. Raises +ArgumentError+
51
+ # if +height+ is less than 1 or greater than 1,000.
47
52
  def height=(height)
48
- raise ArgumentError, "Invalid height: #{height.inspect}" if height.nil? || height < 1
53
+ if height.nil? || height < 1 || height > 1_000
54
+ raise ArgumentError, "Invalid height: #{height.inspect}"
55
+ end
56
+
49
57
  @height = height
50
58
  end
51
59
 
@@ -54,9 +62,14 @@ module GChart
54
62
  "#{width}x#{height}"
55
63
  end
56
64
 
57
- # Sets the chart's size as "WIDTHxHEIGHT".
65
+ # Sets the chart's size as "WIDTHxHEIGHT". Raises +ArgumentError+
66
+ # if +width+ * +height+ is greater than 300,000 pixels.
58
67
  def size=(size)
59
68
  self.width, self.height = size.split("x").collect { |n| Integer(n) }
69
+
70
+ if (width * height) > 300_000
71
+ raise ArgumentError, "Invalid size: #{size.inspect} yields a graph with more than 300,000 pixels"
72
+ end
60
73
  end
61
74
 
62
75
  # Returns the chart's URL.
@@ -79,9 +92,9 @@ module GChart
79
92
 
80
93
  protected
81
94
 
82
- def query_params #:nodoc:
83
- params = { "cht" => render_chart_type, "chs" => size }
84
-
95
+ def query_params(raw_params={}) #:nodoc:
96
+ params = raw_params.merge("cht" => render_chart_type, "chs" => size)
97
+
85
98
  render_data(params)
86
99
  render_title(params)
87
100
  render_colors(params)
@@ -106,7 +119,7 @@ module GChart
106
119
  end
107
120
 
108
121
  def render_title(params) #:nodoc:
109
- params["chtt"] = title.tr("\n", "|").gsub(/\s+/, "+") if title
122
+ params["chtt"] = title.tr("\n ", "|+") if title
110
123
  end
111
124
 
112
125
  def render_colors(params) #:nodoc:
@@ -1,10 +1,20 @@
1
1
  module GChart
2
2
  class Pie < GChart::Base
3
+ # A single array of chart data. Raises +ArgumentError+
4
+ # if more than one data set is provided.
5
+ def data=(data)
6
+ if data.is_a?(Array) and data.first.is_a?(Array) and data.size > 1
7
+ raise ArgumentError, "Pie charts only have one data set"
8
+ end
9
+
10
+ super(data)
11
+ end
12
+
3
13
  def render_chart_type #:nodoc:
4
14
  "p"
5
15
  end
6
16
 
7
- def render_legend(params)
17
+ def render_legend(params) #:nodoc:
8
18
  params["chl"] = legend.join("|") if legend
9
19
  end
10
20
  end
@@ -1,3 +1,3 @@
1
1
  module GChart
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -22,3 +22,59 @@ describe GChart::Bar do
22
22
  GChart::Bar.new(:orientation => :vertical).render_chart_type.should == "bvs"
23
23
  end
24
24
  end
25
+
26
+ describe GChart::Bar, "#orientation" do
27
+ before(:each) { @chart = GChart::Bar.new }
28
+
29
+ it "complains if orientation is invalid" do
30
+ lambda { @chart.orientation = :monkey }.should raise_error(ArgumentError)
31
+ end
32
+ end
33
+
34
+ describe GChart::Bar, "#thickness" do
35
+ before(:each) { @chart = GChart::Bar.new }
36
+
37
+ it "can be specified" do
38
+ @chart.thickness = 10
39
+ @chart.thickness.should == 10
40
+ end
41
+
42
+ it "complains about negative numbers" do
43
+ lambda { @chart.thickness = -1 }.should raise_error(ArgumentError)
44
+ end
45
+ end
46
+
47
+ describe GChart::Bar, "#spacing" do
48
+ before(:each) { @chart = GChart::Bar.new }
49
+
50
+ it "can be specified" do
51
+ @chart.spacing = 2
52
+ @chart.spacing.should == 2
53
+ end
54
+
55
+ it "complains about negative numbers" do
56
+ lambda { @chart.spacing = -1 }.should raise_error(ArgumentError)
57
+ end
58
+ end
59
+
60
+ describe GChart::Bar, "#query_params" do
61
+ before(:each) { @chart = GChart::Bar.new }
62
+
63
+ it "contains the chart's type" do
64
+ @chart.query_params["cht"].should =~ /b(h|v)(g|s)/
65
+ end
66
+
67
+ it "contains the chart's bar height" do
68
+ @chart.thickness = 10
69
+ @chart.query_params.keys.include?("chbh").should be_true
70
+ @chart.query_params["chbh"].should == "10"
71
+ @chart.spacing = 2
72
+ @chart.query_params.keys.include?("chbh").should be_true
73
+ @chart.query_params["chbh"].should == "10,2"
74
+ end
75
+
76
+ it "it does not contain a bar height without a thickness" do
77
+ @chart.spacing = 2
78
+ @chart.query_params.keys.include?("chbh").should be_false
79
+ end
80
+ end
@@ -52,6 +52,12 @@ describe GChart::Base, "#size" do
52
52
  lambda { @chart.width = -1 }.should raise_error(ArgumentError)
53
53
  lambda { @chart.height= -1 }.should raise_error(ArgumentError)
54
54
  end
55
+
56
+ it "complains about sizes that are out of bounds (300,000 pixel graph limit, 1000 pixel side limit)" do
57
+ lambda { @chart.size = "491x611" }.should raise_error(ArgumentError)
58
+ lambda { @chart.size = "1001x300" }.should raise_error(ArgumentError)
59
+ lambda { @chart.size = "300x1001" }.should raise_error(ArgumentError)
60
+ end
55
61
  end
56
62
 
57
63
  describe GChart::Base, "#render_chart_type" do
@@ -5,3 +5,25 @@ describe GChart::Pie do
5
5
  GChart::Pie.new.render_chart_type.should == "p"
6
6
  end
7
7
  end
8
+
9
+ describe GChart::Pie, "#query_params" do
10
+ before(:each) { @chart = GChart::Pie.new }
11
+
12
+ it "contains the chart's type" do
13
+ @chart.query_params["cht"].should == "p"
14
+ end
15
+
16
+ it "contains the chart's legend" do
17
+ @chart.legend = ["foo"]
18
+ @chart.query_params.keys.include?("chl").should be_true
19
+ @chart.query_params["chl"].should == "foo"
20
+ end
21
+ end
22
+
23
+ describe GChart::Pie, "#data" do
24
+ it "complains if you provide more than one set of data" do
25
+ lambda { GChart::Pie.new(:data => [1, 2, 3]) }.should_not raise_error(ArgumentError)
26
+ lambda { GChart::Pie.new(:data => [[1, 2, 3]]) }.should_not raise_error(ArgumentError)
27
+ lambda { GChart::Pie.new(:data => [[1, 2, 3], [3, 2, 1]]) }.should raise_error(ArgumentError)
28
+ end
29
+ end
@@ -7,8 +7,32 @@ describe GChart do
7
7
  end
8
8
 
9
9
  describe GChart, ".encode" do
10
- it "only supports the extended encoding" do
11
- lambda { GChart.encode(:simple, 4, 10) }.should raise_error(ArgumentError)
10
+ it "supports the simple, text, and extended encoding" do
11
+ lambda { GChart.encode(:simple, 4, 10) }.should_not raise_error(ArgumentError)
12
+ lambda { GChart.encode(:text, 4, 10) }.should_not raise_error(ArgumentError)
13
+ lambda { GChart.encode(:extended, 4, 10) }.should_not raise_error(ArgumentError)
14
+ lambda { GChart.encode(:monkey, 4, 10) }.should raise_error(ArgumentError)
15
+ end
16
+
17
+ it "implements the simple encoding" do
18
+ expected = {
19
+ 0 => "A", 19 => "T", 27 => "b", 53 => "1", 61 => "9",
20
+ 12 => "M", 39 => "n", 57 => "5", 45 => "t", 51 => "z"
21
+ }
22
+
23
+ expected.each do |original, encoded|
24
+ GChart.encode(:simple, original, 61).should == encoded
25
+ end
26
+ end
27
+
28
+ it "implements the text encoding" do
29
+ expected = {
30
+ 0 => "0.0", 10 => "10.0", 58 => "58.0", 95 => "95.0", 30 => "30.0", 8 => "8.0", 63 => "63.0", 100 => "100.0"
31
+ }
32
+
33
+ expected.each do |original, encoded|
34
+ GChart.encode(:text, original, 100).should == encoded
35
+ end
12
36
  end
13
37
 
14
38
  it "implements the extended encoding" do
@@ -24,16 +48,18 @@ describe GChart, ".encode" do
24
48
  end
25
49
 
26
50
  it "encodes nil correctly" do
51
+ GChart.encode(:simple, nil, 1).should == "_"
52
+ GChart.encode(:text, nil, 1).should == "-1"
27
53
  GChart.encode(:extended, nil, 1).should == "__"
28
54
  end
29
55
  end
30
56
 
31
57
  describe GChart, ".line" do
32
58
  it "can render a basic line URL" do
33
- expected = { "cht" => "lc", "chs" => "300x200", "chd" => "e:AAAo..", "chtt" => "test" }
59
+ expected = { "cht" => "lc", "chs" => "300x200", "chd" => "e:AAAp..", "chtt" => "test" }
34
60
  chart = GChart.line(:title => "test", :data => [1, 100, 10000])
35
61
 
36
62
  chart.query_params.should == expected
37
- chart.to_url.should == "http://chart.apis.google.com/chart?chs=300x200&cht=lc&chtt=test&chd=e:AAAo.."
63
+ chart.to_url.should == "http://chart.apis.google.com/chart?chs=300x200&cht=lc&chtt=test&chd=e:AAAp.."
38
64
  end
39
65
  end
@@ -0,0 +1,7 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --backtrace
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gchart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Barnette
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2007-12-24 00:00:00 -08:00
12
+ date: 2007-12-28 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.3.0
22
+ version: 1.4.0
23
23
  version:
24
24
  description: "== PROBLEMS/TODO * Add support fills (area or background), grid lines, shape markers, range markers * Support shorthand colors and color names * Make venn data specification friendlier There are lots of missing features. Until they're implemented, you can directly specify query parameters using the :extras key, e.g., # provides a legend for each data set g = GChart.line(:data => [[1, 2], [3, 4]], :extras => { \"chdl\" => [\"First\", \"Second\"] })"
25
25
  email: jbarnette@rubyforge.org