google-image-charts 0.4.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.
@@ -0,0 +1,90 @@
1
+ # Google Image Charts
2
+
3
+ Ruby library for interfacing with Google's Image Chart API. (Part of the Google Image Visualization API.)
4
+
5
+ Google Image Charts allows a user to enter series data into a query string and retrieve a PNG or SVG image graph. See [http://code.google.com/apis/chart/image/](http://code.google.com/apis/chart/image/) for more information.
6
+
7
+ Right now only Line Graphs and Pie Charts are supported. Let me know if you would like to contribute!
8
+
9
+ ## Installation
10
+
11
+ gem install google-image-charts
12
+
13
+ ## Line Graph Example
14
+ ### Code Sample
15
+ # Load the library
16
+ require 'google-image-charts.rb'
17
+
18
+ # Populate the chart details
19
+ chartDetails = {
20
+ :title => "Test chart",
21
+ :height => 250,
22
+ :width => 400,
23
+ :data => [[4,5,10,6,2,9], [2,2,12,9,11,1]], # Array of data arrays (one for each line)
24
+ :labels => ["Series 1", "Series 2"]
25
+ }
26
+
27
+ # Create the chart
28
+ lineGraph = GoogleImageCharts::LineGraph.new(chartDetails)
29
+
30
+ # Use it!
31
+ chartUrl = lineGraph.chart_url
32
+ # => "http://chart.apis.google.com/chart?cht=lc&chs=400x250&chd=t:4,5,10,6,2,9%7C2,2,12,9,11,1&chdl=Series%201%7CSeries%202&chdlp=b&chtt=Test%20chart&chco=a&chds=a&chxt=x,y"
33
+
34
+ imageTag = lineGraph.html_img_tag
35
+ # => "<img src='http://chart.apis.google.com/chart?cht=lc&chs=400x250&chd=t:4,5,10,6,2,9%7C2,2,12,9,11,1&chdl=Series%201%7CSeries%202&chdlp=b&chtt=Test%20chart&chco=a&chds=a&chxt=x,ycht=lc&chs=400x250&chd=t:4,5,10,6,2,9%7C2,2,12,9,11,1&chdl=Series%201%7CSeries%202&chdlp=b&chtt=Test%20chart&chco=a&chds=a&chxt=x,y' alt='Test chart' height='250' width='400' />"
36
+
37
+ # Make it prettier
38
+ lineGraph.chartColors = ["5CB8E6","E68A00"]
39
+
40
+ ### Result
41
+ <div align="center"><img src='http://chart.apis.google.com/chart?cht=lc&chs=400x250&chd=t:4,5,10,6,2,9%7C2,2,12,9,11,1&chdl=Series%201%7CSeries%202&chdlp=b&chtt=Test%20chart&chco=a&chds=a&chxt=x,ycht=lc&chs=400x250&chd=t:4,5,10,6,2,9%7C2,2,12,9,11,1&chdl=Series%201%7CSeries%202&chdlp=b&chtt=Test%20chart&chco=5CB8E6,E68A00&chds=a&chxt=x,y' alt='Test chart' height='250' width='400' /></div>
42
+
43
+ ## Pie Chart Example
44
+ ### Code Sample
45
+ # Load the library
46
+ require 'google-image-charts.rb'
47
+
48
+ # Populate the chart details
49
+ chartDetails = {
50
+ :title => "Pie Chart Test",
51
+ :height => 200,
52
+ :width => 200,
53
+ :data => [15, 85],
54
+ :labels => ["This Stinks!", "This is awesome!"],
55
+ :colors => ["5CB8E6","E68A00"]
56
+ }
57
+
58
+ # Create the chart
59
+ pieChart = GoogleImageCharts::PieChart.new(chartDetails)
60
+
61
+ # Use it!
62
+ chartUrl = pieChart.chart_url
63
+ # => "http://chart.apis.google.com/chart?cht=p&chs=200x200&chd=t:15,85&chdl=This%20Stinks!%7CThis%20is%20awesome!&chdlp=b&chtt=Pie%20Chart%20Test&chco=5CB8E6,E68A00&chds=a"
64
+
65
+ imageTag = pieChart.html_img_tag
66
+ # => "<img src='http://chart.apis.google.com/chart?cht=p&chs=200x200&chd=t:15,85&chdl=This%20Stinks!%7CThis%20is%20awesome!&chdlp=b&chtt=Pie%20Chart%20Test&chco=5CB8E6,E68A00&chds=acht=p&chs=200x200&chd=t:15,85&chdl=This%20Stinks!%7CThis%20is%20awesome!&chdlp=b&chtt=Pie%20Chart%20Test&chco=5CB8E6,E68A00&chds=a' alt='Pie Chart Test' height='200' width='200' />"
67
+
68
+ ### Result
69
+ <div align="center"><img src='http://chart.apis.google.com/chart?cht=p&chs=200x200&chd=t:15,85&chdl=This%20Stinks!%7CThis%20is%20awesome!&chdlp=b&chtt=Pie%20Chart%20Test&chco=5CB8E6,E68A00&chds=acht=p&chs=200x200&chd=t:15,85&chdl=This%20Stinks!%7CThis%20is%20awesome!&chdlp=b&chtt=Pie%20Chart%20Test&chco=5CB8E6,E68A00&chds=a' alt='Pie Chart Test' height='200' width='200' /></div>
70
+
71
+ ## Additional Options
72
+
73
+ ### Larger Charts
74
+ As of 0.4 there is support for Google's POST method allowing up to 16K of data to be graphed. Simply set `:usePost = true` in your chartOptions hash and call `get_chart`. Be sure to set your content return type to PNG for this to work in a browser.
75
+
76
+ ### Additional Options
77
+ There are plenty of features that this library does not yet make available. If you'd like to use the simplicity of the library, but pass in one of the additional features documented on [https://developers.google.com/chart/image/docs/chart_params](Google's Chart Feature List) you can now pass those as a string into your :additionalOptions element in the chartOptions hash.
78
+
79
+ Each additional line element should be separated by an '&'
80
+
81
+ chartDetails = {
82
+ :title => "Pie Chart Test",
83
+ :height => 200,
84
+ :width => 200,
85
+ :data => [15, 85],
86
+ :labels => ["This Stinks!", "This is awesome!"],
87
+ :colors => ["5CB8E6","E68A00"],
88
+ :additionalOptions => "&chls=5" #Makes the line bolder
89
+ }
90
+
@@ -0,0 +1,21 @@
1
+
2
+ module GoogleImageCharts
3
+ class LineGraph < ChartBase
4
+
5
+ def initialize(chartOptionsHash)
6
+ super(chartOptionsHash)
7
+
8
+ @chartType = "lc" #Line chart
9
+
10
+ @chartSpecificOptions += "&chds=a" # Chart Scale (automatic scaling by Google)
11
+ @chartSpecificOptions += "&chxt=x,y" # Scale labels for the X and Y axis'
12
+
13
+ end
14
+
15
+ def chartDataFlattened
16
+ # Line graphs are series arrays seperated by pipes
17
+ @chartData.join("|")
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # Ref: http://code.google.com/apis/chart/image/docs/gallery/pie_charts.html
2
+
3
+ module GoogleImageCharts
4
+ class PieChart < ChartBase
5
+
6
+ def initialize(chartOptionsHash)
7
+ super(chartOptionsHash)
8
+
9
+ @chartType = "p" # Pie chart
10
+
11
+ @chartSpecificOptions << "&chds=a" # Chart Scale (automatic scaling by Google)
12
+
13
+ end
14
+
15
+ def chartDataFlattened
16
+ # Pie Charts are simple data numbers seperated by commas
17
+ @chartData.join(",")
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,132 @@
1
+ require 'uri'
2
+
3
+ module GoogleImageCharts
4
+
5
+ class ChartBase
6
+ attr_accessor :chartTitle, :chartWidth, :chartHeight, :chartLabels, :chartColors, :additionalChartOptions, :usePost
7
+
8
+ CHART_URI_BASE = "http://chart.apis.google.com/chart?"
9
+
10
+ def initialize(chartOptionsHash)
11
+ @chartSpecificOptions = "" # Should be overridden by child-classes
12
+
13
+ @chartWidth = chartOptionsHash[:width]
14
+ @chartHeight = chartOptionsHash[:height]
15
+ @chartTitle = chartOptionsHash[:title]
16
+ self.chartData=(chartOptionsHash[:data]) # Should be an array of data arrays
17
+
18
+ @chartLabels = chartOptionsHash[:labels] # Should be an array of labels
19
+ @chartLabelPosition = "b" if chartOptionsHash[:labels].nil? == false
20
+
21
+ @chartColors = chartOptionsHash[:colors]
22
+
23
+ # These might be set by someone who knows what they are doing, perhaps after reading:
24
+ # https://developers.google.com/chart/image/docs/chart_params
25
+ @additionalChartOptions = chartOptionsHash[:additionalOptions]
26
+
27
+ # Support the Google Image Charts POST method which allows up to 16K of chart data
28
+ # https://developers.google.com/chart/image/docs/post_requests
29
+ @usePost = chartOptionsHash[:usePost] #((chartOptionsHash[:usePost].nil? == true) || (@usePost != true)) ? false : true
30
+
31
+ end
32
+
33
+ def chartDataFlattened
34
+ # Override in subclasses
35
+ @chartData
36
+ end
37
+
38
+ def chartData=(data)
39
+ # Take array of arrays and flatten to array of strings
40
+
41
+ @chartData = Array.new
42
+ data.each_index do |index|
43
+ # Join the data only if it contains an array
44
+ if data[index].class == Array
45
+ @chartData.push data[index].join(",").to_s
46
+ else
47
+ @chartData.push data[index]
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ def set_chart_label_position(position)
54
+ @chartLabelPosition = case
55
+ when position == :top then "t"
56
+ when position == :left then "l"
57
+ when position == :right then "r"
58
+ when position == :bottom then "b"
59
+ else "b"
60
+ end
61
+
62
+ end
63
+
64
+ def get_chart
65
+ # returns a URL if usePost = false,
66
+ # returns a PNG if usePost = true. Pass your encoding to the browser as appropriate.
67
+ return @usePost == false ? chart_url : post_chart_data
68
+ end
69
+
70
+ def chart_url
71
+ # Standard GET request
72
+ chartURL = CHART_URI_BASE
73
+ chartURL = chartURL + "cht=" + @chartType
74
+ chartURL = chartURL + "&chs=" + @chartWidth.to_s + "x" + @chartHeight.to_s
75
+ chartURL = chartURL + "&chd=t:" + chartDataFlattened if @chartData.nil? == false # Simple text for now
76
+ chartURL = chartURL + "&chdl=" + @chartLabels.join("|") if @chartLabels.nil? == false
77
+ chartURL = chartURL + "&chdlp=" + @chartLabelPosition if @chartLabels.nil? == false
78
+ chartURL = chartURL + "&chtt=" + @chartTitle if @chartTitle.nil? == false
79
+ chartURL = chartURL + "&chco=" + @chartColors.join(",") if @chartColors.nil? == false
80
+
81
+ chartURL = chartURL + @chartSpecificOptions + @additionalChartOptions
82
+
83
+ return URI.escape(chartURL)
84
+ end
85
+
86
+ def post_chart_data
87
+ # Used when you need to send more than 2K (up to 16K) of data to Google.
88
+ require 'net/https'
89
+
90
+ uri = URI.parse("https://chart.googleapis.com/chart")
91
+
92
+ dataHash = {
93
+ "cht" => @chartType,
94
+ "chs" => @chartWidth.to_s + "x" + @chartHeight.to_s
95
+ }
96
+
97
+ dataHash.merge!( (@chartData.nil? == false) ? {"chd" => "t:#{chartDataFlattened.to_s}" } : {} )
98
+ dataHash.merge!( (@chartLabels.nil? == false) ? {"chdl" => @chartLabels.join("|").to_s } : {} )
99
+ dataHash.merge!( (@chartLabels.nil? == false) ? {"chdlp" => @chartLabelPosition } : {} )
100
+ dataHash.merge!( (@chartTitle.nil? == false) ? {"chtt" => @chartTitle } : {} )
101
+ dataHash.merge!( (@chartColors.nil? == false) ? {"chco" => @chartColors.join(",").to_s } : {} )
102
+
103
+ #For the remainder of these, we need to split on '&', then k,v on '='
104
+ (@chartSpecificOptions.to_s + @additionalChartOptions.to_s).split('&').each do |option|
105
+ pair = option.split('=')
106
+ newElement = { pair[0].to_s => pair[1].to_s }
107
+ dataHash.merge!( newElement )
108
+ end
109
+
110
+ http = Net::HTTP.new(uri.host, uri.port)
111
+ http.use_ssl = true
112
+
113
+ request = Net::HTTP::Post.new(uri.request_uri)
114
+ request.set_form_data(dataHash)
115
+
116
+ response = http.request(request)
117
+ return response.body
118
+
119
+ end
120
+
121
+ def html_img_tag
122
+ return "<img src='#{chart_url}' alt='#{@chartTitle}' height='#{@chartHeight}' width='#{@chartWidth}' />"
123
+ end
124
+
125
+ end
126
+
127
+ # Load all the chart types
128
+ require 'GoogleImageCharts/line-graph.rb'
129
+ require 'GoogleImageCharts/pie-chart.rb'
130
+
131
+ end
132
+
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-image-charts
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 3
10
+ version: 0.4.3
11
+ platform: ruby
12
+ authors:
13
+ - Christopher Brito
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-24 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Simple Ruby library for interfacing with Google's Visualization API, specifically their Image Chart capability.
22
+ email: cbrito@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - README.markdown
31
+ - lib/google-image-charts.rb
32
+ - lib/GoogleImageCharts/line-graph.rb
33
+ - lib/GoogleImageCharts/pie-chart.rb
34
+ homepage: http://github.com/cbrito/google-image-charts
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.15
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Ruby Library for interfacing with Google's Visualization API
67
+ test_files: []
68
+