Permalink
Browse files

Adding support for Google's map chart type.

  • Loading branch information...
gregoryfoster
gregoryfoster committed Jun 23, 2008
1 parent 26ba8ea commit 367694f4c4c0df6b7fbd42347e27f0241c4684a4
View
@@ -1,17 +1,18 @@
Gem::Specification.new do |s|
+ require 'rake'
s.name = "gchartrb"
- s.version = "0.9"
+ s.version = "0.9.1"
s.authors = ["Deepak Jois"]
s.email = "deepak.jois@gmail.com"
- s.date = "2008-05-25"
+ s.date = "2008-06-16"
s.homepage = "http://code.google.com/p/gchartrb"
s.summary = "Ruby Wrapper for the Google Chart API"
s.description = "gchartrb is a Ruby wrapper around the Google Chart API, located at http://code.google.com/apis/chart/. Visit http://code.google.com/p/gchartrb to track development regarding gchartrb."
s.has_rdoc = true
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
s.rdoc_options = ["--main", "README.txt"]
- s.files = ["CREDITS", "History.txt", "Manifest.txt", "README.txt", "Rakefile", "TODO", "gchartrb.gemspec", "lib/core_ext.rb", "lib/example.rb", "lib/gchartrb.rb", "lib/google_chart.rb", "lib/google_chart/bar_chart.rb", "lib/google_chart/base.rb", "lib/google_chart/line_chart.rb", "lib/google_chart/linexy_chart.rb", "lib/google_chart/modules/axis.rb", "lib/google_chart/modules/color.rb", "lib/google_chart/modules/data_array.rb", "lib/google_chart/modules/fills.rb", "lib/google_chart/modules/grid.rb", "lib/google_chart/modules/label.rb", "lib/google_chart/modules/legend.rb", "lib/google_chart/modules/markers.rb", "lib/google_chart/pie_chart.rb", "lib/google_chart/radar_chart.rb", "lib/google_chart/scatter_plot.rb", "lib/google_chart/sparkline_chart.rb", "lib/google_chart/venn_diagram.rb", "lib/test.rb", "spec/gchartrb/axis_spec.rb", "spec/gchartrb/bar_chart_spec.rb", "spec/gchartrb/fills_spec.rb", "spec/gchartrb/grid_spec.rb", "spec/gchartrb/line_chart_spec.rb", "spec/gchartrb/linexy_chart_spec.rb", "spec/gchartrb/markers_spec.rb", "spec/gchartrb/pie_chart_spec.rb", "spec/gchartrb/radar_chart_spec.rb", "spec/gchartrb/scatter_plot_spec.rb", "spec/gchartrb/sparkline_chart_spec.rb", "spec/gchartrb/venn_diagram_spec.rb", "spec/gchartrb_spec.rb", "spec/helper.rb", "spec/spec.opts"]
+ s.files = ["CREDITS", "History.txt", "Manifest.txt", "README.txt", "Rakefile", "TODO", "gchartrb.gemspec" ] + FileList["{lib,spec}/**/*"].to_a
s.require_paths = ["lib"]
s.rubyforge_project = "gchartrb"
s.rubygems_version = "1.1.1"
View
@@ -3,7 +3,7 @@
require File.dirname(__FILE__) + "/google_chart/modules/#{mod}"
end
-%w(base line_chart linexy_chart sparkline_chart scatter_plot bar_chart radar_chart venn_diagram pie_chart).each do |type|
+%w(base line_chart linexy_chart sparkline_chart scatter_plot bar_chart radar_chart venn_diagram pie_chart map_chart).each do |type|
require File.dirname(__FILE__) + "/google_chart/#{type}"
end
View
@@ -51,19 +51,21 @@ def title=(title) #:nodoc:
end
# Sets the chart's width, in pixels. Raises +ArgumentError+
- # if +width+ is less than 1 or greater than 1,000.
+ # if +width+ is less than 1 or greater than 1,000 (440 for maps).
def width=(width)
- if width.nil? || width < 1 || width > 1_000
+ width_max = @chart_type == 't' ? 440 : 1_000
+ if width.nil? || width < 1 || width > width_max
raise ArgumentError, "Invalid width: #{width.inspect}"
end
@width = width
end
# Sets the chart's height, in pixels. Raises +ArgumentError+
- # if +height+ is less than 1 or greater than 1,000.
+ # if +height+ is less than 1 or greater than 1,000 (220 for maps).
def height=(height)
- if height.nil? || height < 1 || height > 1_000
+ height_max = @chart_type == 't' ? 220 : 1_000
+ if height.nil? || height < 1 || height > height_max
raise ArgumentError, "Invalid height: #{height.inspect}"
end
@@ -105,6 +107,7 @@ def query_params
add_grid if @grid
add_markers if @markers
add_bar_width_and_spacing if respond_to?(:add_bar_width_and_spacing)
+ add_map_parameters if respond_to?(:add_map_parameters)
return @params
end
@@ -0,0 +1,74 @@
+module GoogleChart
+ class MapChart < Base
+ include Color
+ include DataArray
+ include Fills
+
+ GEOGRAPHICAL_AREAS = [ :africa, :asia, :europe, :middle_east, :south_america, :usa, :world ]
+ ISO_3166_1_ALPHA_2 = [ :AF, :AX, :AL, :DZ, :AS, :AD, :AO, :AI, :AQ, :AG, :AR, :AM, :AW, :AU, :AT, :AZ, :BS, :BH, :BD, :BB, :BY, :BE, :BZ, :BJ, :BM, :BT, :BO, :BA, :BW, :BV, :BR, :IO, :BN, :BG, :BF, :BI, :KH, :CM, :CA, :CV, :KY, :CF, :TD, :CL, :CN, :CX, :CC, :CO, :KM, :CG, :CD, :CK, :CR, :CI, :HR, :CU, :CY, :CZ, :DK, :DJ, :DM, :DO, :EC, :EG, :SV, :GQ, :ER, :EE, :ET, :FK, :FO, :FJ, :FI, :FR, :GF, :PF, :TF, :GA, :GM, :GE, :DE, :GH, :GI, :GR, :GL, :GD, :GP, :GU, :GT, :GG, :GN, :GW, :GY, :HT, :HM, :VA, :HN, :HK, :HU, :IS, :IN, :ID, :IR, :IQ, :IE, :IM, :IL, :IT, :JM, :JP, :JE, :JO, :KZ, :KE, :KI, :KP, :KR, :KW, :KG, :LA, :LV, :LB, :LS, :LR, :LY, :LI, :LT, :LU, :MO, :MK, :MG, :MW, :MY, :MV, :ML, :MT, :MH, :MQ, :MR, :MU, :YT, :MX, :FM, :MD, :MC, :MN, :ME, :MS, :MA, :MZ, :MM, :NA, :NR, :NP, :NL, :AN, :NC, :NZ, :NI, :NE, :NG, :NU, :NF, :MP, :NO, :OM, :PK, :PW, :PS, :PA, :PG, :PY, :PE, :PH, :PN, :PL, :PT, :PR, :QA, :RE, :RO, :RU, :RW, :BL, :SH, :KN, :LC, :MF, :PM, :VC, :WS, :SM, :ST, :SA, :SN, :RS, :SC, :SL, :SG, :SK, :SI, :SB, :SO, :ZA, :GS, :ES, :LK, :SD, :SR, :SJ, :SZ, :SE, :CH, :SY, :TW, :TJ, :TZ, :TH, :TL, :TG, :TK, :TO, :TT, :TN, :TR, :TM, :TC, :TV, :UG, :UA, :AE, :GB, :US, :UM, :UY, :UZ, :VU, :VE, :VN, :VG, :VI, :WF, :EH, :YE, :ZM, :ZW ]
+ STATE_ABBREVIATIONS = [ :AL, :AK, :AZ, :AR, :CA, :CO, :CT, :DE, :DC, :FL, :GA, :HI, :ID, :IL, :IN, :IA, :KS, :KY, :LA, :ME, :MD, :MA, :MI, :MN, :MS, :MO, :MT, :NE, :NV, :NH, :NJ, :NM, :NY, :NC, :ND, :OH, :OK, :OR, :PA, :RI, :SC, :SD, :TN, :TX, :UT, :VT, :VA, :WA, :WV, :WI, :WY ]
+
+ attr_accessor :geographical_area, :default_color, :gradient
+
+ data_type :numeric
+
+ def initialize(options={})
+ @chart_type = 't'
+ @show_legend = false
+ @default_color = 'FFFFFF'
+ super(options)
+ end
+
+
+ # Called to add map-specific parameters to the query_params hash.
+ def add_map_parameters
+ @params[:chtm] = geographical_area.to_s
+ @params[:chld] = @region_codes.join('') unless @region_codes.empty?
+ unless @default_color.empty?
+ @params[:chco] = @params[:chco].empty? ? @default_color : "#{@default_color},#{@params[:chco]}"
+ end
+ end
+
+
+ #
+ # ACCESSORS.
+ #
+ def data(code, data)
+ raise ArgumentError.new("data should be an integer value") unless data.is_a?(Numeric)
+ @data << data
+ region_code(code)
+ end
+
+ def geographical_area=(area)
+ raise ArgumentError.new("area must be one of :#{GEOGRAPHICAL_AREAS.join(', :')}") unless GEOGRAPHICAL_AREAS.include?(area)
+ @geographical_area = area
+ end
+
+ def gradient=(gradient_colors)
+ if gradient_colors.is_a?(Array)
+ gradient_colors.each { |gradient_color| color(gradient_color) }
+ elsif gradient_colors.is_a?(String)
+ color(gradient_colors)
+ else
+ raise ArgumentError.new("gradient must be a single RGB value as a String or an Array of String RGB values")
+ end
+ end
+
+ private
+ def region_code(code)
+ @region_codes ||= []
+
+ # Ensure a proper region code is being added.
+ raise ArgumentError.new("geographical area must be defined before adding data") unless @geographical_area
+
+ if @geographical_area == :usa
+ raise ArgumentError.new("geographical area must be a US state abbreviation") unless STATE_ABBREVIATIONS.include?(code)
+ else
+ raise ArgumentError.new("geographical area must be an ISO-3166-1 alpha-2 code") unless ISO_3166_1_ALPHA_2.include?(code)
+ end
+
+ @region_codes << code
+ end
+
+ end
+end
@@ -6,7 +6,7 @@ def self.included(mod)#:nodoc:
mod.send(:extend,ClassMethods)
end
- def data(legend,series, color = nil)
+ def data(legend, series, color = nil)
raise ArgumentError.new("Invalid value for series data") unless series.send(self.class.get_data_type)
@data << series
legend(legend)
View
@@ -1,5 +1,5 @@
require 'gchartrb'
-
+=begin
# Pie Chart
GoogleChart::PieChart.new do |pc|
pc.data "Apples", 40
@@ -187,6 +187,30 @@
puts "\nSparklines"
puts sp.to_url
end
+=end
+
+# Maps.
+mc = GoogleChart::MapChart.new do |chart|
+ chart.title = "Hospital Procedural Compliance Nationally"
+ chart.title_color = '000000'
+ chart.title_font_size = 18
+
+ chart.width = 440
+ chart.height = 220
+ chart.geographical_area = :usa
+
+ chart.data :NY, 100
+ chart.data :TX, 50
+ chart.data :CA, 25
+
+ chart.default_color = 'FFFFFF'
+ chart.gradient = [ 'FF0000', '00FF00', '0000FF' ]
+
+ chart.fill(:solid, :color => 'EAF7FE')
+
+ puts "\nMap Chart"
+ puts chart.to_url
+end
__END__
# Line Style
Oops, something went wrong.

0 comments on commit 367694f

Please sign in to comment.