Skip to content

Commit

Permalink
Merge pull request #26 from raphaelcm/image_charts
Browse files Browse the repository at this point in the history
Add image charts to image charts.
  • Loading branch information
winston committed Mar 1, 2012
2 parents 1e7a10f + cf5b637 commit 0b31855
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/google_visualr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

# Image Charts
require "#{lib_path}/google_visualr/image/spark_line"
require "#{lib_path}/google_visualr/image/bar_chart"
require "#{lib_path}/google_visualr/image/line_chart"
require "#{lib_path}/google_visualr/image/pie_chart"


# Rails Helper
Expand Down
47 changes: 47 additions & 0 deletions lib/google_visualr/image/bar_chart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module GoogleVisualr
module Image

# http://code.google.com/apis/chart/interactive/docs/gallery/imagebarchart.html
class BarChart < BaseChart
include GoogleVisualr::Packages::ImageChart

# For Configuration Options, please refer to:
# http://code.google.com/apis/chart/interactive/docs/gallery/imagebarchart.html

# Create URI for image bar chart. Override parameters by passing in a hash.
# (see http://code.google.com/apis/chart/image/docs/chart_params.html)
#
# Parameters:
# *params [Optional] Hash of url query parameters
def uri(params = {})
query_params = {}

# isStacked/isVertical, Chart Type
chart_type = "b"
chart_type += @options["isVertical"] ? "v" : "h"
chart_type += @options["isStacked"] == false ? "g" : "s"
query_params[:cht] = chart_type

# showCategoryLabels (works as long as :chxt => "x,y")
labels = ""
val_column = @options["isVertical"] ? 1 : 0
cat_column = @options["isVertical"] ? 0 : 1
if @options["showCategoryLabels"] == false
labels = "#{cat_column}:||"
else
labels = "#{cat_column}:|" + data_table.get_column(0).join('|') + "|"
end

# showValueLabels (works as long as :chxt => "x,y")
if @options["showValueLabels"] == false
labels += "#{val_column}:||"
end

query_params[:chxl] = labels unless labels.blank?

chart_image_url(query_params.merge(params))
end
end

end
end
47 changes: 47 additions & 0 deletions lib/google_visualr/image/line_chart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module GoogleVisualr
module Image

# http://code.google.com/apis/chart/interactive/docs/gallery/imagelinechart.html
class LineChart < BaseChart
include GoogleVisualr::Packages::ImageChart

# For Configuration Options, please refer to:
# http://code.google.com/apis/chart/interactive/docs/gallery/imagelinechart.html

# Create URI for image line chart. Override parameters by passing in a hash.
# (see http://code.google.com/apis/chart/image/docs/chart_params.html)
#
# Parameters:
# *params [Optional] Hash of url query parameters
def uri(params = {})
query_params = {}

# Chart type: line
query_params[:cht] = "lc"

# showAxisLines
if @options["showAxisLines"] == false
query_params[:cht] = "lc:nda"
end

# showCategoryLabels (works as long as :chxt => "x,y")
labels = ""
if @options["showCategoryLabels"] == false
labels = "0:||"
else
labels = "0:|" + data_table.get_column(0).join('|') + "|"
end

# showValueLabels (works as long as :chxt => "x,y")
if @options["showValueLabels"] == false
labels += "1:||"
end

query_params[:chxl] = labels unless labels.blank?

chart_image_url(query_params.merge(params))
end
end

end
end
47 changes: 47 additions & 0 deletions lib/google_visualr/image/pie_chart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module GoogleVisualr
module Image

# http://code.google.com/apis/chart/interactive/docs/gallery/imagepiechart.html
class PieChart < BaseChart
include GoogleVisualr::Packages::ImageChart

# For Configuration Options, please refer to:
# http://code.google.com/apis/chart/interactive/docs/gallery/imagepiechart.html

# Create URI for image pie chart. Override parameters by passing in a hash.
# (see http://code.google.com/apis/chart/image/docs/chart_params.html)
#
# Parameters:
# *params [Optional] Hash of url query parameters
def uri(params = {})
query_params = {}

# Chart Type: normal or 3D
query_params[:cht] = @options["is3D"] ? "p3" : "p"

# Legend (override generic image chart behavior)
query_params[:chdl] = @data_table.get_column(0).join('|')

# Labels
case options["labels"]
when "name"
query_params[:chl] = @data_table.get_column(0).join('|')
when "value"
query_params[:chl] = @data_table.get_column(1).join('|')
else
query_params[:chl] = ""
end

# data (override generic chart behavior)
query_params[:chd] = "t:" + @data_table.get_column(1).join(',')

# Chart Colors (override generic chart default)
query_params[:chco] = @options["colors"].join('|').gsub(/#/, '') if @options["colors"]

chart_image_url(query_params.merge(params))
end

end

end
end
25 changes: 23 additions & 2 deletions lib/google_visualr/image/spark_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,29 @@ class SparkLine < BaseChart
include GoogleVisualr::Packages::ImageChart

# For Configuration Options, please refer to:
# http://code.google.com/apis/chart/interactive/docs/gallery/imagesparkline.html#Configuration_Options
end
# http://code.google.com/apis/chart/interactive/docs/gallery/imagesparkline.html

# Create URI for sparkline. Override parameters by passing in a hash.
# (see http://code.google.com/apis/chart/image/docs/chart_params.html)
#
# Parameters:
# *params [Optional] Hash of url query parameters
def uri(params = {})
query_params = {}

# Chart type: line
query_params[:cht] = "ls"

# showValueLabels (works as long as :chxt => "x,y")
labels = "0:||"
if @options["showValueLabels"] == false || !@options["showAxisLines"]
labels += "1:||"
end

query_params[:chxl] = labels

chart_image_url(query_params.merge(params))
end
end
end
end
82 changes: 82 additions & 0 deletions lib/google_visualr/packages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,94 @@ def package_name
end

module ImageChart
include GoogleVisualr::ParamHelpers

def package_name
"image#{self.class.to_s.split("::").last.downcase}"
end
def class_name
"Image#{self.class.to_s.split('::').last}"
end

# Set defaults according to http://code.google.com/apis/chart/interactive/docs/gallery/genericimagechart.html#Configuration_Options
IMAGE_DEFAULTS = {
# Automatic Scaling
:chds => "a",
# Size
:chs => "400x200",
# Axes
:chxt => "x,y"
}

# Generates HTTP GET URL for the chart image
#
# Parameters:
# *opts [Optional] Hash of standard chart options (see http://code.google.com/apis/chart/image/docs/chart_params.html)
def chart_image_url(superseding_params = {})

#####
# Generic image chart defaults
query_params = IMAGE_DEFAULTS.clone

# backgroundColor
query_params[:chf] = "bg,s," + options["backgroundColor"].gsub(/#/, '') if options["backgroundColor"]

# color, colors ('color' param is ignored if 'colors' is present)
if options["colors"]
query_params[:chco] = options["colors"].join(',').gsub(/#/, '')
elsif options["color"]
query_params[:chco] = options["color"].gsub(/#/, '')
end

# fill (this will often not look good - better for user to override this parameter)
query_params[:chm] = "B,#{query_params[:chco].split(',').first},0,0,0" if options["fill"] && query_params[:chco]

# firstHiddenColumn, singleColumnDisplay, data
firstHiddenColumn = options["firstHiddenColumn"] ? options["firstHiddenColumn"] : data_table.cols.size - 1
query_params[:chd] = "t:"
unless options["singleColumnDisplay"]
for i in 1..firstHiddenColumn do
query_params[:chd] += "|" if i > 1
query_params[:chd] += data_table.get_column(i).join(',')
end
else
query_params[:chd] += data_dable.get_column(options["singleColumnDisplay"])
end

# height, width
if options["height"] && options["width"]
query_params[:chs] = "#{options["width"]}x#{options["height"]}"
end

# title
query_params[:chtt] = options["title"] if options["title"]

# legend
unless options["legend"] == 'none'
query_params[:chdlp] = options["legend"].first unless options["legend"].blank?
query_params[:chdl] = data_table.cols[1..-1].map{|col| col[:label] }.join('|')
else
query_params.delete(:chdlp)
query_params.delete(:chdl)
end

# min, max, valueLabelsInterval (works as long as :chxt => "x,y" and both 'min' and 'max' are set)
if options["min"] && options["max"]
query_params[:chxr] = "1,#{options['min']},#{options['max']}"
query_params[:chxr] += ",#{options['valueLabelsInterval']}" if options['valueLabelsInterval']
query_params[:chds] = "#{options['min']},#{options['max']}"
end
#####

query_params = stringify_keys!(query_params.merge(superseding_params))
base_url = "https://chart.googleapis.com/chart"
query = ""
query_params.each_with_index do |(k,v),i|
query += (i == 0) ? "?" : "&"
query += "#{k}=#{CGI.escape(v)}"
end
URI.parse(base_url + query)
end
end

end
Expand Down
65 changes: 65 additions & 0 deletions spec/google_visualr/image_charts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'spec_helper'

describe GoogleVisualr::Image::PieChart do
describe "#uri" do
let(:chart){ GoogleVisualr::Image::PieChart.new( data_table, {} )}
image_pie_options.each_with_index do |opts, i|
context "#{opts.inspect}" do
it "generates correct URI" do
chart.options = opts
uri = chart.uri
#puts uri.to_s
CGI.parse(uri.query).should == CGI.parse(image_pie_chart_uris[i].query)
end
end
end
end
end

describe GoogleVisualr::Image::LineChart do
describe "#uri" do
let(:chart){ GoogleVisualr::Image::LineChart.new( data_table, {} )}
image_line_options.each_with_index do |opts, i|
context "#{opts.inspect}" do
it "generates correct URI" do
chart.options = opts
uri = chart.uri
#puts uri.to_s
CGI.parse(uri.query).should == CGI.parse(image_line_chart_uris[i].query)
end
end
end
end
end

describe GoogleVisualr::Image::BarChart do
describe "#uri" do
let(:chart){ GoogleVisualr::Image::BarChart.new( data_table, {} )}
image_bar_options.each_with_index do |opts, i|
context "#{opts.inspect}" do
it "generates correct URI" do
chart.options = opts
uri = chart.uri
#puts uri.to_s
CGI.parse(uri.query).should == CGI.parse(image_bar_chart_uris[i].query)
end
end
end
end
end

describe GoogleVisualr::Image::SparkLine do
describe "#uri" do
let(:chart){ GoogleVisualr::Image::SparkLine.new( data_table, {} )}
sparkline_options.each_with_index do |opts, i|
context "#{opts.inspect}" do
it "generates correct URI" do
chart.options = opts
uri = chart.uri
#puts uri.to_s
CGI.parse(uri.query).should == CGI.parse(sparkline_uris[i].query)
end
end
end
end
end
Loading

0 comments on commit 0b31855

Please sign in to comment.