capture program drop gmap /*================================================================================================================================ ! gmap v2.0 Thomas Roca, September 2014. This program draws heat maps within and HTML file, using Google geochart javascript library (see https://google-developers.appspot.com/chart/interactive/docs/gallery/geochart) A country identifier variable must exist in the dataset; this variable must be named "iso3" and contains country ISO 3166-1 alpha-3 codes. Many options are available to personalize the map (i.e. region, color, size ..) It uses 2 javascript files: *"jquery-latest.js" (http://code.jquery.com/jquery-1.9.1.js) *"jsapi" (https://www.google.com/jsapi) How to use it? see help gmap // e.g. of command gmap PTS iso3 year using test.html, time(2012) History: machine dependancy fixed *===============================================================================================================================*/ program define gmap, rclass sortpreserve version 9.0 syntax varlist(min=3 max=3) /// variable to display iso3 !NEED TO FIX THAT using /// filename [, /// options zone(integer 0) /// 0=World;1=Europe;2=North America;3=South America;4=Africa;5=Asia;6=Asia Pacific;7=Oceania ->[default World] color(integer 1) /// Type in 1=Blue | 2=Green | 3=Purple | 4=Yellow ->[default Blue] color1_rgb(string) /// r,g,b color2_rgb(string) /// r,g,b width(integer 850) /// Width of the map ->[default 850px] height(integer 700) /// height of the map ->[default 700px] title(str) /// title of the map ->[default My gmap Title] legend(str) /// yes or no ->[default no] time(integer 2000) /// the actual value of time id (j) ] quietly { capture cd "`dir'" *=============================================================================== * Generate folders that will host HTML, java & image files needed *=============================================================================== capture mkdir gmap cd "`c(pwd)'/gmap" *=============================================================================== * Copy from ado directory the java & image files needed *=============================================================================== *List of files to copy into gmap/. local listJava jsapi jquery-latest.js *Search for gmap.ado and store its path findfile gmap.ado, all local folder=subinstr(`"`r(fn)'"',"gmap.ado","",.) *Copy the js files needed foreach file in `listJava' { capture copy `folder'/`file' `file' } *store directory capture local dir=subinstr(`"`c(pwd)'"',"/gmap","",.) // unix capture local dir=subinstr(`"`c(pwd)'"',"\gmap","",.) // Windows *=============================================================================== * Set parameters *=============================================================================== gettoken ucmd filename : using local filename `filename' *Store var1 and var2 one of them is the iso3 coutry code (string) the other is a number local Var: word 1 of `varlist' local id: word 2 of `varlist' local j: word 3 of `varlist' qui sum `Var' if `j'==`time' if `r(N)'==0 error 2000 noisily di as text "....." noisily di as text " Variable to map:`Var', counntry id:`id', Time id:`j', selected time:`time'" *setting default option if missing("`title'") local title="a stata command" if missing("`label'") local label="My data label" if missing("`legend'") local legend="legend: 'none'," if "`legend'"=="yes" local legend="" if "`legend'"=="no" local legend="legend: 'none'," local label="`: var label `Var''" *======================= Set region option ===================================== #delimit ; if `zone'==0 local ZONE="world" ; if `zone'==1 local ZONE="150" ; if `zone'==2 local ZONE="021" ; if `zone'==3 local ZONE="005" ; if `zone'==4 local ZONE="002" ; if `zone'==5 local ZONE="142" ; if `zone'==6 local ZONE="035" ; if `zone'==7 local ZONE="009" ; if `zone'==0 local ZONEstr="world" ; if `zone'==1 local ZONEstr="Europe" ; if `zone'==2 local ZONEstr="North America" ; if `zone'==3 local ZONEstr="South America" ; if `zone'==4 local ZONEstr="Africa" ; if `zone'==5 local ZONEstr="Asia" ; if `zone'==6 local ZONEstr="Asia Pacific" ; if `zone'==7 local ZONEstr="Oceania" ; #delimit cr *=============================================================================== *Set color options local colorFormatted=`color' if missing(`colorFormatted') local colorFormatted=1 if `colorFormatted'==1 { local color1="#E0F2F7" local color2="#045FB4" } if `colorFormatted'==2 { local color1="#BCF5A9" local color2="#04B431" } if `colorFormatted'==3 { local color1="#F8E0F7" local color2="#610B5E" } if `colorFormatted'==4 { local color1="#F5F6CE" local color2="#FFBF00" } *default values if missing( "`color1_rgb'") | missing( "`color2_rgb'") { local color1="#E0F2F7" local color2="#045FB4" } if !missing( "`color1_rgb'") { local color1="rgb(`color1_rgb')" } if !missing( "`color2_rgb'") { local color2="rgb(`color2_rgb')" } *=============================================================================== local iso3list AFG ALB ATA DZA ASM AND AGO ATG AZE ARG AUS AUT BHS BHR BGD ARM BRB BEL BMU BTN BOL BIH BWA BVT BRA BLZ IOT SLB VGB BRN BGR MMR BDI BLR KHM CMR CAN CPV CYM CAF LKA TCD CHL CHN TWN CXR CCK COL COM MYT COG COD COK CRI HRV CUB CYP CZE BEN DNK DMA DOM ECU SLV GNQ ETH ERI EST FRO FLK SGS FJI FIN ALA FRA GUF PYF ATF DJI GAB GEO GMB PSE DEU GHA GIB KIR GRC GRL GRD GLP GUM GTM GIN GUY HTI HMD VAT HND HKG HUN ISL IND IDN IRN IRQ IRL ISR ITA CIV JAM JPN KAZ JOR KEN PRK KOR KWT KGZ LAO LBN LSO LVA LBR LBY LIE LTU LUX MAC MDG MWI MYS MDV MLI MLT MTQ MRT MUS MEX MCO MNG MDA MNE MSR MAR MOZ OMN NAM NRU NPL NLD CUW ABW SXM BES NCL VUT NZL NIC NER NGA NIU NFK NOR MNP UMI FSM MHL PLW PAK PAN PNG PRY PER PHL PCN POL PRT GNB TLS PRI QAT REU ROU RUS RWA BLM SHN KNA AIA LCA MAF SPM VCT SMR STP SAU SEN SRB SYC SLE SGP SVK VNM SVN SOM ZAF ZWE ESP SSD SDN ESH SUR SJM SWZ SWE CHE SYR TJK THA TGO TKL TON TTO ARE TUN TUR TKM TCA TUV UGA UKR MKD EGY GBR GGY JEY IMN TZA USA VIR BFA URY UZB VEN WLF WSM YEM ZMB ĪLIST_ENDĪ *Count the number of iso3 id in the list local LoopEnd : word count `iso3list' *Store descriptive stats of the selected variable qui sum `Var' if `j'==`time' local min=`r(min)' local max=`r(max)' *Store variable label local label="`: var label `Var''" if missing("`label'") local label="`Var'" *Set loop count local as 0 local L=0 tempname page file open `page' using "page", r w file write `page' /// `" "' _n /// `" "' _n /// `" "' _n /// `"

"' _n /// `" Google geochart - `title'
"' _n /// `" Variable displayed: `Var'
"' _n /// `" Variable label: `label' "' _n /// `"
Region: `ZONEstr'
"' _n /// `" print as pdf "' _n /// `"

"' _n /// `" "' _n file close `page' capture erase "`filename'" capture !rename "page" "`filename'" // windows capture !mv "page" "`filename'" // unix capture erase "page" noisily di "----------------------" noisily di as txt "Done ! Open output web page: " `"{browse "`c(pwd)'/`filename'"}"' cd "`dir'" } end