Andy
unread,Feb 5, 2009, 3:46:32 AM2/5/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Python Google Chart
I needed some more functionality for the MapChart.
Here are my changes to MapChart:
class MapChart(Chart):
def __init__(self, *args, **kwargs):
Chart.__init__(self, *args, **kwargs)
self.geo_area = 'world'
self.codes = []
self.__areas = ['africa', 'asia', 'europe', 'middle_east',
'south_america', 'usa', 'world']
self.__ccodes =
['AD','AE','AF','AG','AI','AL','AM','AN','AO','AQ','AR',
'AS','AT','AU','AW','AX','AZ','BA','BB','BD','BE','BF',
'BG','BH','BI','BJ','BL','BM','BN','BO','BR','BS','BT',
'BV','BW','BY','BZ','CA','CC','CD','CF','CG','CH','CI',
'CK','CL','CM','CN','CO','CR','CU','CV','CX','CY','CZ',
'DE','DJ','DK','DM','DO','DZ','EC','EE','EG','EH','ER',
'ES','ET','FI','FJ','FK','FM','FO','FR','GA','GB','GD',
'GE','GF','GG','GH','GI','GL','GM','GN','GP','GQ','GR',
'GS','GT','GU','GW','GY','HK','HM','HN','HR','HT','HU',
'ID','IE','IL','IM','IN','IO','IQ','IR','IS','IT','JE',
'JM','JO','JP','KE','KG','KH','KI','KM','KN','KP','KR',
'KW','KY','KZ','LA','LB','LC','LI','LK','LR','LS','LT',
'LU','LV','LY','MA','MC','MD','ME','MF','MG','MH','MK',
'ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV',
'MW','MX','MY','MZ','NA','NC','NE','NF','NG','NI','NL',
'NO','NP','NR','NU','NZ','OM','PA','PE','PF','PG','PH',
'PK','PL','PM','PN','PR','PS','PT','PW','PY','QA','RE',
'RO','RS','RU','RW','SA','SB','SC','SD','SE','SG','SH',
'SI','SJ','SK','SL','SM','SN','SO','SR','ST','SV','SY',
'SZ','TC','TD','TF','TG','TH','TJ','TK','TL','TM','TN',
'TO','TR','TT','TV','TW','TZ','UA','UG','UM','US','UY',
'UZ','VA','VC','VE','VG','VI','VN','VU','WF','WS','YE',
'YT','ZA','ZM','ZW']
def type_to_url(self):
return 'cht=t'
def set_codes(self, codes):
'''Set the country code map for the data.
Codes given in a list.
i.e. DE - Germany
AT - Austria
US - United States
'''
codemap = ''
for cc in codes:
cc = cc.upper()
if cc in self.__ccodes:
codemap += cc
else:
raise UnknownCountryCodeException(cc)
self.codes = codemap
def set_geo_area(self, area):
'''Sets the geo area for the map.
* africa
* asia
* europe
* middle_east
* south_america
* usa
* world
'''
if area in self.__areas:
self.geo_area = area
else:
raise UnknownChartType('Unknown chart type for maps: %s'
%area)
def get_url_bits(self, data_class=None):
url_bits = Chart.get_url_bits(self, data_class=data_class)
url_bits.append('chtm=%s' % self.geo_area)
if self.codes:
url_bits.append('chld=%s' % ''.join(self.codes))
return url_bits
#TODO: No function ? Can#t explain Exceptions
## def add_data(self, data):
## '''Overrides Chart.add_data(data) because only one data set
makes sense for MapCharts'''
##
## self.data = data
##
## return len(self.data) - 1 # return the "index" of the data
set
def add_data_dict(self, datadict):
'''Sets the data and country codes via a dictionary.
i.e. {'DE': 50, 'GB': 30, 'AT': 70}
'''
self.set_codes(datadict.keys())
self.add_data(datadict.values())
I also added 'UnknownCountryCodeException'.
Andy