From 102a895504fe217d9ea9d8aac5e72750ac90aaa4 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Wed, 15 May 2013 10:45:46 +0200 Subject: [PATCH] Fix empty interpolation and add tests for it --- pygal/graph/graph.py | 4 +- pygal/graph/i18n.py | 190 +++++++++++++++++++++++++++++++++++++ pygal/graph/stackedline.py | 4 +- pygal/test/test_config.py | 17 +++- 4 files changed, 209 insertions(+), 6 deletions(-) create mode 100644 pygal/graph/i18n.py diff --git a/pygal/graph/graph.py b/pygal/graph/graph.py index 11cd0408..8f9dfe8b 100644 --- a/pygal/graph/graph.py +++ b/pygal/graph/graph.py @@ -432,8 +432,10 @@ def _points(self, x_pos): serie.points = [ (x_pos[i], v) for i, v in enumerate(serie.values)] - if self.interpolate: + if serie.points and self.interpolate: serie.interpolated = self._interpolate(serie.values, x_pos) + else: + serie.interpolated = [] def _compute_secondary(self): # secondary y axis support diff --git a/pygal/graph/i18n.py b/pygal/graph/i18n.py new file mode 100644 index 00000000..fd6f4a1a --- /dev/null +++ b/pygal/graph/i18n.py @@ -0,0 +1,190 @@ +COUNTRIES = { + 'ad': 'Andorra', + 'ae': 'United Arab Emirates', + 'af': 'Afghanistan', + 'al': 'Albania', + 'am': 'Armenia', + 'ao': 'Angola', + 'aq': 'Antarctica', + 'ar': 'Argentina', + 'at': 'Austria', + 'au': 'Australia', + 'az': 'Azerbaijan', + 'ba': 'Bosnia and Herzegovina', + 'bd': 'Bangladesh', + 'be': 'Belgium', + 'bf': 'Burkina Faso', + 'bg': 'Bulgaria', + 'bh': 'Bahrain', + 'bi': 'Burundi', + 'bj': 'Benin', + 'bn': 'Brunei Darussalam', + 'bo': 'Bolivia, Plurinational State of', + 'br': 'Brazil', + 'bt': 'Bhutan', + 'bw': 'Botswana', + 'by': 'Belarus', + 'bz': 'Belize', + 'ca': 'Canada', + 'cd': 'Congo, the Democratic Republic of the', + 'cf': 'Central African Republic', + 'cg': 'Congo', + 'ch': 'Switzerland', + 'ci': "Cote d'Ivoire", + 'cl': 'Chile', + 'cm': 'Cameroon', + 'cn': 'China', + 'co': 'Colombia', + 'cr': 'Costa Rica', + 'cu': 'Cuba', + 'cv': 'Cape Verde', + 'cy': 'Cyprus', + 'cz': 'Czech Republic', + 'de': 'Germany', + 'dj': 'Djibouti', + 'dk': 'Denmark', + 'do': 'Dominican Republic', + 'dz': 'Algeria', + 'ec': 'Ecuador', + 'ee': 'Estonia', + 'eg': 'Egypt', + 'eh': 'Western Sahara', + 'er': 'Eritrea', + 'es': 'Spain', + 'et': 'Ethiopia', + 'fi': 'Finland', + 'fr': 'France', + 'ga': 'Gabon', + 'gb': 'United Kingdom', + 'ge': 'Georgia', + 'gf': 'French Guiana', + 'gh': 'Ghana', + 'gl': 'Greenland', + 'gm': 'Gambia', + 'gn': 'Guinea', + 'gq': 'Equatorial Guinea', + 'gr': 'Greece', + 'gt': 'Guatemala', + 'gu': 'Guam', + 'gw': 'Guinea-Bissau', + 'gy': 'Guyana', + 'hk': 'Hong Kong', + 'hn': 'Honduras', + 'hr': 'Croatia', + 'ht': 'Haiti', + 'hu': 'Hungary', + 'id': 'Indonesia', + 'ie': 'Ireland', + 'il': 'Israel', + 'in': 'India', + 'iq': 'Iraq', + 'ir': 'Iran, Islamic Republic of', + 'is': 'Iceland', + 'it': 'Italy', + 'jm': 'Jamaica', + 'jo': 'Jordan', + 'jp': 'Japan', + 'ke': 'Kenya', + 'kg': 'Kyrgyzstan', + 'kh': 'Cambodia', + 'kp': "Korea, Democratic People's Republic of", + 'kr': 'Korea, Republic of', + 'kw': 'Kuwait', + 'kz': 'Kazakhstan', + 'la': "Lao People's Democratic Republic", + 'lb': 'Lebanon', + 'li': 'Liechtenstein', + 'lk': 'Sri Lanka', + 'lr': 'Liberia', + 'ls': 'Lesotho', + 'lt': 'Lithuania', + 'lu': 'Luxembourg', + 'lv': 'Latvia', + 'ly': 'Libyan Arab Jamahiriya', + 'ma': 'Morocco', + 'mc': 'Monaco', + 'md': 'Moldova, Republic of', + 'me': 'Montenegro', + 'mg': 'Madagascar', + 'mk': 'Macedonia, the former Yugoslav Republic of', + 'ml': 'Mali', + 'mm': 'Myanmar', + 'mn': 'Mongolia', + 'mo': 'Macao', + 'mr': 'Mauritania', + 'mt': 'Malta', + 'mu': 'Mauritius', + 'mv': 'Maldives', + 'mw': 'Malawi', + 'mx': 'Mexico', + 'my': 'Malaysia', + 'mz': 'Mozambique', + 'na': 'Namibia', + 'ne': 'Niger', + 'ng': 'Nigeria', + 'ni': 'Nicaragua', + 'nl': 'Netherlands', + 'no': 'Norway', + 'np': 'Nepal', + 'nz': 'New Zealand', + 'om': 'Oman', + 'pa': 'Panama', + 'pe': 'Peru', + 'pg': 'Papua New Guinea', + 'ph': 'Philippines', + 'pk': 'Pakistan', + 'pl': 'Poland', + 'pr': 'Puerto Rico', + 'ps': 'Palestine, State of', + 'pt': 'Portugal', + 'py': 'Paraguay', + 're': 'Reunion', + 'ro': 'Romania', + 'rs': 'Serbia', + 'ru': 'Russian Federation', + 'rw': 'Rwanda', + 'sa': 'Saudi Arabia', + 'sc': 'Seychelles', + 'sd': 'Sudan', + 'sg': 'Singapore', + 'sh': 'Saint Helena, Ascension and Tristan da Cunha', + 'si': 'Slovenia', + 'sk': 'Slovakia', + 'sl': 'Sierra Leone', + 'sm': 'San Marino', + 'sn': 'Senegal', + 'so': 'Somalia', + 'sr': 'Suriname', + 'st': 'Sao Tome and Principe', + 'sv': 'El Salvador', + 'sy': 'Syrian Arab Republic', + 'sz': 'Swaziland', + 'td': 'Chad', + 'tg': 'Togo', + 'th': 'Thailand', + 'tj': 'Tajikistan', + 'tl': 'Timor-Leste', + 'tm': 'Turkmenistan', + 'tn': 'Tunisia', + 'tr': 'Turkey', + 'tw': 'Taiwan, Province of China', + 'tz': 'Tanzania, United Republic of', + 'ua': 'Ukraine', + 'ug': 'Uganda', + 'us': 'United States', + 'uy': 'Uruguay', + 'uz': 'Uzbekistan', + 'va': 'Holy See (Vatican City State)', + 've': 'Venezuela, Bolivarian Republic of', + 'vn': 'Viet Nam', + 'ye': 'Yemen', + 'yt': 'Mayotte', + 'za': 'South Africa', + 'zm': 'Zambia', + 'zw': 'Zimbabwe' +} + + +def set_countries(countries): + global COUNTRIES + COUNTRIES = countries diff --git a/pygal/graph/stackedline.py b/pygal/graph/stackedline.py index 3f2b2dda..06b4d81d 100644 --- a/pygal/graph/stackedline.py +++ b/pygal/graph/stackedline.py @@ -50,5 +50,7 @@ def _points(self, x_pos): serie.points = [ (x_pos[i], v) for i, v in enumerate(accumulation)] - if self.interpolate: + if serie.points and self.interpolate: serie.interpolated = self._interpolate(accumulation, x_pos) + else: + serie.interpolated = [] diff --git a/pygal/test/test_config.py b/pygal/test/test_config.py index 52f3502b..2f1199bb 100644 --- a/pygal/test/test_config.py +++ b/pygal/test/test_config.py @@ -152,11 +152,20 @@ def test_interpolation(Chart): assert len(q(".legend")) == 2 +def test_no_data_interpolation(Chart): + chart = Chart(interpolate='cubic') + q = chart.render_pyquery() + assert q(".text-overlay text").text() == "No data" + + +def test_no_data_with_empty_serie_interpolation(Chart): + chart = Chart(interpolate='cubic') + chart.add('Serie', []) + q = chart.render_pyquery() + assert q(".text-overlay text").text() == "No data" + + def test_logarithmic_bad_interpolation(): - try: - import scipy - except ImportError: - return line = Line(logarithmic=True, interpolate='cubic') line.add('_', [.001, .00000001, 1]) q = line.render_pyquery()