Prologue
Few weeks ago we’ve decided to make our translator directory a little bit cooler. Google Chart API was the tool for the job. Reading few posts about it before we’ve realized that they have really cool ability to create map-based charts. Generated charts look pretty and they can also be useful.
Problem
So we wanted to create a map showing where our translators are from and also showing how many translators we have in a certain country. This map lives now in our translator directory and the translators table less boring.
Implementation
Our application is written in Ruby using Ruby on Rails framework. We have UserProfile model that contains all relevant translators data. So we just need to a add a method to collect per country statistics from the database. I didn’t think long and used very basic ActiveRecord call to run arbitrary query to get it done.
class UserProfile < ActiveRecord
........
def self.get_country_stats
self.connection.select_all("select count(1) as cnt, country from user_profiles group by country")
end
........
end
This simply returns an array of result set rows. Now in controller we can use that method together with some pretty simple and not very readable code to generate parameters we pass to Google in order to generate our chart. We will need to generate three important parameters to be used in the view: chld=<country or state codes> chd=<color levels> chds=<minimum, maximum levels> chld and chd represent two arrays with the same number of items so that corresponding items match. Items of chld are ISO country codes and we have a very handy class CountryLanguageCodes to generate them by country name. Items of chld are just counts of UserProfiles per country. We have to provide chds parameter with maximum and minimum counts in order to let Google to normalize the colors.
class PeopleController < ActiveRecord
.........
def index
..........
@chld = ""
@chd_arr = []
@chmax = 0
@chmin = 10000000
UserProfile.get_country_stats.each do |row|
code = CountryLanguageCodes.code_by_country(row['country'])
next unless code
@chld << CountryLanguageCodes.code_by_country(row['country'])
cc = row['cnt'].to_i
@chd_arr << cc @chmax = cc if cc > @chmax
@chmin = cc if cc < @chmin
end
@chd = "t:" + @chd_arr.join(',')
@chds = "#{@chmin},#{@chmax}"
........
end
............
end
Hard part is over and now we can simply pass generated parameter names to Google to generate our map.
<img src="http://chart.apis.google.com/chart?cht=t&chs=440x220&chds=&chd=&chco=FFFFFF,FF0000,FFFF00,00FF00&chld=&chtm=world&chf=bg,s,EAF7FE" width="440" height="220" alt="Country graph" border="0" />
Caching
Certainly using ‘group by’ query can affect your system performance . It may be pretty heavy on the database if you have many users. So I would recommend to use Rails fragment caching if performance becomes a problem. We didn’t have to do any of that yet. In our case we don’t see much impact on the system performance. MySQL does pretty good job caching count query results and the parameter generation code isn’t that slow. Vincent Pearson – ProWSS Engineer April 28, 2009
Links
- Google Chart API – http://code.google.com/apis/chart/
- ISO Country Codes – http://www.iso.org/iso/english_country_names_and_code_elements
- 99translations – http://99translations.com
Tags: chart, google api, i18n, maps
Sala de leos…
http://www.osinterioresmodernos.com Interior idéia Design…
hello…
really good article. Ready to hear more next week,my blog http://debbieqqq.onsugar.com/Adressing-Wedding-invites-Envelopes-17840597 Many Thanks….
Great One…
Thanks. My link, do you like http://sallybxsalmon.tumblr.com/ ,Thanks again….
Great One…
Thanks. My link, do you like http://ghjrdfe.alwahy.com/ ,Thanks again….
Great One…
Thanks. My link, do you like http://bdfzswf.clipsy.org/ ,Thanks again….
You did a great job…
Hi there thanks for the quality post, i had a good read,http://yanjiren.blogage.de/entries/2011/6/22/Where-can-i-find-a-dress-for-my-cousins-wedding , thank you for your article,My problem has been resolved….
Great One…
I must say, its worth it! My link, http://deborah11.blog-host.nl/,thanks haha…
Hello…
My life,vist it http://d.hatena.ne.jp/juhuacha8/20110902/1314946382 ,Thanks….
Hello…
My life,vist it http://juhuacha.blog-host.nl/_lilly_wedding_cakes-182027.html ,Thanks….
Hello…
My life,vist it http://www.gladyshardy.com/blog/zhangda/frog-prince-wedding-wedding-cake-toppers ,Thanks….
Hello…
My life,vist it http://www.thoughts.com/zhangda/dolphin-wedding-wedding-cake-toppers ,Thanks….