Skip to content
Browse files

Adding support for Google's map chart type.

  • Loading branch information...
1 parent 26ba8ea commit 367694f4c4c0df6b7fbd42347e27f0241c4684a4 gregoryfoster committed Jun 22, 2008
View
7 gchartrb.gemspec
@@ -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
2 lib/google_chart.rb
@@ -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
11 lib/google_chart/base.rb
@@ -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
View
74 lib/google_chart/map_chart.rb
@@ -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
View
2 lib/google_chart/modules/data_array.rb
@@ -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
26 lib/test.rb
@@ -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
View
250 scripts/iso3166_en_code_lists.txt
@@ -0,0 +1,250 @@
+Source: http://www.iso.org/iso/iso3166_en_code_lists.txt
+
+This list states the country names (official short names in English) in alphabetical order as given in ISO 3166-1 and the corresponding ISO 3166-1-alpha-2 code elements. The list is updated whenever a change to the official code list in ISO 3166-1 is effected by the ISO 3166/MA. It lists 240 official short names and code elements. One line of text contains one entry. A country name and its code element are separated by a semicolon (;).
+
+AFGHANISTAN;AF
+ÅLAND ISLANDS;AX
+ALBANIA;AL
+ALGERIA;DZ
+AMERICAN SAMOA;AS
+ANDORRA;AD
+ANGOLA;AO
+ANGUILLA;AI
+ANTARCTICA;AQ
+ANTIGUA AND BARBUDA;AG
+ARGENTINA;AR
+ARMENIA;AM
+ARUBA;AW
+AUSTRALIA;AU
+AUSTRIA;AT
+AZERBAIJAN;AZ
+BAHAMAS;BS
+BAHRAIN;BH
+BANGLADESH;BD
+BARBADOS;BB
+BELARUS;BY
+BELGIUM;BE
+BELIZE;BZ
+BENIN;BJ
+BERMUDA;BM
+BHUTAN;BT
+BOLIVIA;BO
+BOSNIA AND HERZEGOVINA;BA
+BOTSWANA;BW
+BOUVET ISLAND;BV
+BRAZIL;BR
+BRITISH INDIAN OCEAN TERRITORY;IO
+BRUNEI DARUSSALAM;BN
+BULGARIA;BG
+BURKINA FASO;BF
+BURUNDI;BI
+CAMBODIA;KH
+CAMEROON;CM
+CANADA;CA
+CAPE VERDE;CV
+CAYMAN ISLANDS;KY
+CENTRAL AFRICAN REPUBLIC;CF
+CHAD;TD
+CHILE;CL
+CHINA;CN
+CHRISTMAS ISLAND;CX
+COCOS (KEELING) ISLANDS;CC
+COLOMBIA;CO
+COMOROS;KM
+CONGO;CG
+CONGO, THE DEMOCRATIC REPUBLIC OF THE;CD
+COOK ISLANDS;CK
+COSTA RICA;CR
+CÔTE D'IVOIRE;CI
+CROATIA;HR
+CUBA;CU
+CYPRUS;CY
+CZECH REPUBLIC;CZ
+DENMARK;DK
+DJIBOUTI;DJ
+DOMINICA;DM
+DOMINICAN REPUBLIC;DO
+ECUADOR;EC
+EGYPT;EG
+EL SALVADOR;SV
+EQUATORIAL GUINEA;GQ
+ERITREA;ER
+ESTONIA;EE
+ETHIOPIA;ET
+FALKLAND ISLANDS (MALVINAS);FK
+FAROE ISLANDS;FO
+FIJI;FJ
+FINLAND;FI
+FRANCE;FR
+FRENCH GUIANA;GF
+FRENCH POLYNESIA;PF
+FRENCH SOUTHERN TERRITORIES;TF
+GABON;GA
+GAMBIA;GM
+GEORGIA;GE
+GERMANY;DE
+GHANA;GH
+GIBRALTAR;GI
+GREECE;GR
+GREENLAND;GL
+GRENADA;GD
+GUADELOUPE;GP
+GUAM;GU
+GUATEMALA;GT
+GUERNSEY;GG
+GUINEA;GN
+GUINEA-BISSAU;GW
+GUYANA;GY
+HAITI;HT
+HEARD ISLAND AND MCDONALD ISLANDS;HM
+HOLY SEE (VATICAN CITY STATE);VA
+HONDURAS;HN
+HONG KONG;HK
+HUNGARY;HU
+ICELAND;IS
+INDIA;IN
+INDONESIA;ID
+IRAN, ISLAMIC REPUBLIC OF;IR
+IRAQ;IQ
+IRELAND;IE
+ISLE OF MAN;IM
+ISRAEL;IL
+ITALY;IT
+JAMAICA;JM
+JAPAN;JP
+JERSEY;JE
+JORDAN;JO
+KAZAKHSTAN;KZ
+KENYA;KE
+KIRIBATI;KI
+KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF;KP
+KOREA, REPUBLIC OF;KR
+KUWAIT;KW
+KYRGYZSTAN;KG
+LAO PEOPLE'S DEMOCRATIC REPUBLIC;LA
+LATVIA;LV
+LEBANON;LB
+LESOTHO;LS
+LIBERIA;LR
+LIBYAN ARAB JAMAHIRIYA;LY
+LIECHTENSTEIN;LI
+LITHUANIA;LT
+LUXEMBOURG;LU
+MACAO;MO
+MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF;MK
+MADAGASCAR;MG
+MALAWI;MW
+MALAYSIA;MY
+MALDIVES;MV
+MALI;ML
+MALTA;MT
+MARSHALL ISLANDS;MH
+MARTINIQUE;MQ
+MAURITANIA;MR
+MAURITIUS;MU
+MAYOTTE;YT
+MEXICO;MX
+MICRONESIA, FEDERATED STATES OF;FM
+MOLDOVA, REPUBLIC OF;MD
+MONACO;MC
+MONGOLIA;MN
+MONTENEGRO;ME
+MONTSERRAT;MS
+MOROCCO;MA
+MOZAMBIQUE;MZ
+MYANMAR;MM
+NAMIBIA;NA
+NAURU;NR
+NEPAL;NP
+NETHERLANDS;NL
+NETHERLANDS ANTILLES;AN
+NEW CALEDONIA;NC
+NEW ZEALAND;NZ
+NICARAGUA;NI
+NIGER;NE
+NIGERIA;NG
+NIUE;NU
+NORFOLK ISLAND;NF
+NORTHERN MARIANA ISLANDS;MP
+NORWAY;NO
+OMAN;OM
+PAKISTAN;PK
+PALAU;PW
+PALESTINIAN TERRITORY, OCCUPIED;PS
+PANAMA;PA
+PAPUA NEW GUINEA;PG
+PARAGUAY;PY
+PERU;PE
+PHILIPPINES;PH
+PITCAIRN;PN
+POLAND;PL
+PORTUGAL;PT
+PUERTO RICO;PR
+QATAR;QA
+REUNION;RE
+ROMANIA;RO
+RUSSIAN FEDERATION;RU
+RWANDA;RW
+SAINT BARTHÉLEMY;BL
+SAINT HELENA;SH
+SAINT KITTS AND NEVIS;KN
+SAINT LUCIA;LC
+SAINT MARTIN;MF
+SAINT PIERRE AND MIQUELON;PM
+SAINT VINCENT AND THE GRENADINES;VC
+SAMOA;WS
+SAN MARINO;SM
+SAO TOME AND PRINCIPE;ST
+SAUDI ARABIA;SA
+SENEGAL;SN
+SERBIA;RS
+SEYCHELLES;SC
+SIERRA LEONE;SL
+SINGAPORE;SG
+SLOVAKIA;SK
+SLOVENIA;SI
+SOLOMON ISLANDS;SB
+SOMALIA;SO
+SOUTH AFRICA;ZA
+SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS;GS
+SPAIN;ES
+SRI LANKA;LK
+SUDAN;SD
+SURINAME;SR
+SVALBARD AND JAN MAYEN;SJ
+SWAZILAND;SZ
+SWEDEN;SE
+SWITZERLAND;CH
+SYRIAN ARAB REPUBLIC;SY
+TAIWAN, PROVINCE OF CHINA;TW
+TAJIKISTAN;TJ
+TANZANIA, UNITED REPUBLIC OF;TZ
+THAILAND;TH
+TIMOR-LESTE;TL
+TOGO;TG
+TOKELAU;TK
+TONGA;TO
+TRINIDAD AND TOBAGO;TT
+TUNISIA;TN
+TURKEY;TR
+TURKMENISTAN;TM
+TURKS AND CAICOS ISLANDS;TC
+TUVALU;TV
+UGANDA;UG
+UKRAINE;UA
+UNITED ARAB EMIRATES;AE
+UNITED KINGDOM;GB
+UNITED STATES;US
+UNITED STATES MINOR OUTLYING ISLANDS;UM
+URUGUAY;UY
+UZBEKISTAN;UZ
+VANUATU;VU
+VENEZUELA;VE
+VIET NAM;VN
+VIRGIN ISLANDS, BRITISH;VG
+VIRGIN ISLANDS, U.S.;VI
+WALLIS AND FUTUNA;WF
+WESTERN SAHARA;EH
+YEMEN;YE
+ZAMBIA;ZM
+ZIMBABWE;ZW
View
27 scripts/process_country_codes.rb
@@ -0,0 +1,27 @@
+#!/usr/bin/env ruby
+
+# Constants.
+USAGE = 'ruby process_country_codes.rb [input_filename]'
+COUNTRY_CODE_REGEX = /^.+;(\w{2})$/ # Regex to pull country code.
+ROW_SEPARATOR = "\n"
+
+
+#
+# Main Program.
+#
+# Read command-line arguments.
+unless ARGV.length == 1
+ puts USAGE
+ exit
+end
+
+input_filename = File.dirname(__FILE__) + '/' + ARGV[0]
+
+# Process the file contents line by line.
+country_codes = []
+IO.foreach(input_filename, ROW_SEPARATOR) do |line|
+ match = line.match(COUNTRY_CODE_REGEX)
+ country_codes << match[1].to_sym unless match.nil?
+end
+
+puts "[ :#{country_codes.join(', :')} ]"

0 comments on commit 367694f

Please sign in to comment.
Something went wrong with that request. Please try again.