/+junk/pygooglechart-py3k

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/pygooglechart-py3k

« back to all changes in this revision

Viewing changes to pygooglechart.py

  • Committer: gak
  • Date: 2009-03-15 07:51:08 UTC
  • Revision ID: git-v1:5cf090282c7aebd29821453475ef2a874b68b70b
 - #23 MapChart extensions (thanks to Andreas Schawo)

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
class UnknownChartType(PyGoogleChartException):
86
86
    pass
87
87
 
 
88
class UnknownCountryCodeException(PyGoogleChartException):
 
89
    pass
88
90
 
89
91
# Data Classes
90
92
# -----------------------------------------------------------------------------
954
956
        Chart.__init__(self, *args, **kwargs)
955
957
        self.geo_area = 'world'
956
958
        self.codes = []
957
 
 
 
959
        self.__areas = ('africa', 'asia', 'europe', 'middle_east', 'south_america', 'usa', 'world')
 
960
        self.__ccodes = ('AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ', 'AR',
 
961
                         'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF',
 
962
                         'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT',
 
963
                         'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI',
 
964
                         'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ',
 
965
                         'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER',
 
966
                         'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD',
 
967
                         'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR',
 
968
                         'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU',
 
969
                         'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE',
 
970
                         'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR',
 
971
                         'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT',
 
972
                         'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK',
 
973
                         'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV',
 
974
                         'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL',
 
975
                         'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH',
 
976
                         'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE',
 
977
                         'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH',
 
978
                         'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY',
 
979
                         'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN',
 
980
                         'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY',
 
981
                         'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE',
 
982
                         'YT', 'ZA', 'ZM', 'ZW')
 
983
        
958
984
    def type_to_url(self):
959
985
        return 'cht=t'
960
986
 
961
987
    def set_codes(self, codes):
962
 
        self.codes = codes
 
988
        '''Set the country code map for the data.
 
989
        Codes given in a list.
 
990
 
 
991
        i.e. DE - Germany
 
992
             AT - Austria
 
993
             US - United States
 
994
        '''
 
995
 
 
996
        codemap = ''
 
997
        
 
998
        for cc in codes:
 
999
            cc = cc.upper()
 
1000
            if cc in self.__ccodes:
 
1001
                codemap += cc
 
1002
            else:
 
1003
                raise UnknownCountryCodeException(cc)
 
1004
            
 
1005
        self.codes = codemap
 
1006
 
 
1007
    def set_geo_area(self, area):
 
1008
        '''Sets the geo area for the map.
 
1009
 
 
1010
        * africa
 
1011
        * asia
 
1012
        * europe
 
1013
        * middle_east
 
1014
        * south_america
 
1015
        * usa
 
1016
        * world
 
1017
        '''
 
1018
        
 
1019
        if area in self.__areas:
 
1020
            self.geo_area = area
 
1021
        else:
 
1022
            raise UnknownChartType('Unknown chart type for maps: %s' %area)
963
1023
 
964
1024
    def get_url_bits(self, data_class=None):
965
1025
        url_bits = Chart.get_url_bits(self, data_class=data_class)
968
1028
            url_bits.append('chld=%s' % ''.join(self.codes))
969
1029
        return url_bits
970
1030
 
 
1031
    def add_data_dict(self, datadict):
 
1032
        '''Sets the data and country codes via a dictionary.
 
1033
 
 
1034
        i.e. {'DE': 50, 'GB': 30, 'AT': 70}
 
1035
        '''
 
1036
 
 
1037
        self.set_codes(datadict.keys())
 
1038
        self.add_data(datadict.values())
 
1039
 
971
1040
 
972
1041
class GoogleOMeterChart(PieChart):
973
1042
    """Inheriting from PieChart because of similar labeling"""