From 9c0002f0d42e8334e32adab0386b6bbf814461b6 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Tue, 21 Oct 2008 11:00:08 -0600 Subject: [PATCH 1/7] Added support for map type. --- .gitignore | 1 + docs/examples.html | 13 +++++++++++++ docs/examples.txt | 13 +++++++++++++ docs/render-examples.py | 1 + templatetags/charts.py | 21 +++++++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e99e36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/docs/examples.html b/docs/examples.html index d88d76c..c38fdb3 100644 --- a/docs/examples.html +++ b/docs/examples.html @@ -357,6 +357,19 @@

Chart tag examples

+ + Map chart + +
{% chart %}
+    {% chart-size "440x220" %}
+    {% chart-type "map" %}
+    {% chart-map-area "usa" %}
+    {% chart-map-data mapdata %}
+    {% chart-colors "fffffff" "ff0000" "0000ff" %}
+{% endchart %}
+ + + diff --git a/docs/examples.txt b/docs/examples.txt index 59a3887..2c68f02 100644 --- a/docs/examples.txt +++ b/docs/examples.txt @@ -337,4 +337,17 @@ Google-o-meter chart, with data scaling {% chart-data-scale "0,10" %} {% chart-data "5" %} {% endchart %} + +Map chart +--------- + +:: + + {% chart %} + {% chart-size "440x220" %} + {% chart-type "map" %} + {% chart-map-area "usa" %} + {% chart-map-data mapdata %} + {% chart-colors "fffffff" "ff0000" "0000ff" %} + {% endchart %} diff --git a/docs/render-examples.py b/docs/render-examples.py index 843962a..1e3960e 100755 --- a/docs/render-examples.py +++ b/docs/render-examples.py @@ -19,6 +19,7 @@ def render_examples(): 'data3' : [i**2 for i in range(20, 0, -1)], 'data4' : [sin(i/5.0)*5 for i in range(100)], 'venn' : [100, 80, 60, 30, 30, 30, 10], + 'mapdata': {'KS': 0, 'CA': 100, "MN": 50}, } examples = [] source = Path(__file__).parent.child("examples.txt").read_file() diff --git a/templatetags/charts.py b/templatetags/charts.py index 6ed12f9..eb17cd1 100644 --- a/templatetags/charts.py +++ b/templatetags/charts.py @@ -100,6 +100,9 @@ def img(self): return s def url(self): + if self.options.get('cht', None) == 't': + self.datasets.append(self.options.pop("_mapdata")) + # Figure out the chart's data range if not self.datarange: maxvalue = max(max(d) for d in self.datasets if d) @@ -266,6 +269,7 @@ def chart_type(arg): * 'pie-3d' * 'venn' * 'scatter' + * 'map' """ types = { @@ -283,6 +287,7 @@ def chart_type(arg): 'venn': 'v', 'scatter': 's', 'google-o-meter': 'gom', + 'map': 't', } return {"cht": types.get(arg, arg)} @@ -415,6 +420,22 @@ def chart_markers(dataset_index, iterable): return {"chm": smart_join("|", markers)} +@option("chart-map-area") +def chart_map_area(where): + return {'chtm': where} + +@option("chart-map-data") +def chart_map_data(data): + place_list = [] + value_list = [] + for (k, v) in data.items(): + place_list.append(k) + value_list.append(v) + return { + "chld": smart_join("", *place_list), + "_mapdata": value_list + } + # # {% axis %} # From e4c037349f435e30d01b944ff38654c9f7afffde Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Tue, 21 Oct 2008 11:07:38 -0600 Subject: [PATCH 2/7] Reorg to make the structure conform to best practices better. --- __init__.py => googlecharts/__init__.py | 0 {templatetags => googlecharts/templatetags}/__init__.py | 0 {templatetags => googlecharts/templatetags}/charts.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename __init__.py => googlecharts/__init__.py (100%) rename {templatetags => googlecharts/templatetags}/__init__.py (100%) rename {templatetags => googlecharts/templatetags}/charts.py (100%) diff --git a/__init__.py b/googlecharts/__init__.py similarity index 100% rename from __init__.py rename to googlecharts/__init__.py diff --git a/templatetags/__init__.py b/googlecharts/templatetags/__init__.py similarity index 100% rename from templatetags/__init__.py rename to googlecharts/templatetags/__init__.py diff --git a/templatetags/charts.py b/googlecharts/templatetags/charts.py similarity index 100% rename from templatetags/charts.py rename to googlecharts/templatetags/charts.py From accb071aca8aa0909a8b88bffaf1e5e3ba949808 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Tue, 21 Oct 2008 18:57:04 -0600 Subject: [PATCH 3/7] Removed python2.5-isms --- googlecharts/templatetags/charts.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/googlecharts/templatetags/charts.py b/googlecharts/templatetags/charts.py index eb17cd1..ab3066e 100644 --- a/googlecharts/templatetags/charts.py +++ b/googlecharts/templatetags/charts.py @@ -230,8 +230,11 @@ def option(tagname, multi=None, nodeclass=ChartOptionNode): def decorator(func): # Figure out how to validate the args to the tag args, varargs, varkw, defaults = inspect.getargspec(func) - max_args = len(args) if args else 0 - min_args = max_args - (len(defaults) if defaults else 0) + max_args = min_args = 0 + if args: + max_args = len(args) + if defaults: + min_args = max_args - len(defaults) unlimited = bool(varargs) @functools.wraps(func) @@ -380,7 +383,10 @@ def chart_range_marker(range_type, color, start, end): @option("chart-fill-area", multi="|") def chart_fill_area(color, startindex=0, endindex=0): - filltype = ("b" if (startindex or endindex) else "B") + if startindex or endindex: + filltype = "b" + else: + filltype = "B" return {"chm": smart_join(",", filltype, color, startindex, endindex, "0")} marker_types = { @@ -568,7 +574,10 @@ def encode_extended(values, value_range): _num2chars = [a+b for a in _encoding_chars for b in _encoding_chars] def num2chars(n, value_range): - return _num2chars[norm(n, value_range)] if n is not None else "__" + if n is not None: + return _num2chars[norm(n, value_range)] + else: + return '__' def norm(n, value_range): minvalue, maxvalue = value_range @@ -595,7 +604,7 @@ def smart_join(sep, *args): def urlencode(query, safe="/:,|"): q = functools.partial(quote_plus, safe=safe) - query = query.items() if hasattr(query, "items") else query + query = getattr(query, 'items', lambda: query)() qlist = ["%s=%s" % (q(k), q(v)) for (k,v) in query] return "&".join(qlist) From cb75b809f75bf0621e24105c247a27d49f6ffa4d Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Tue, 21 Oct 2008 19:04:58 -0600 Subject: [PATCH 4/7] Another python2.5 removal. --- googlecharts/templatetags/charts.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/googlecharts/templatetags/charts.py b/googlecharts/templatetags/charts.py index ab3066e..991bbca 100644 --- a/googlecharts/templatetags/charts.py +++ b/googlecharts/templatetags/charts.py @@ -95,7 +95,10 @@ def clone(self): def img(self): url = self.url() width, height = self.options["chs"].split("x") - alt = 'alt="%s" ' % escape(self.alt) if self.alt else '' + if self.alt: + alt = 'alt="%s" ' % escape(self.alt) + else: + alt = '' s = mark_safe('' % (escape(url), width, height, alt)) return s From 8dfc8a849e7c0d2b80f2b9c2dd05b79c32cab2f4 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 22 Oct 2008 10:40:20 -0600 Subject: [PATCH 5/7] functools is python2.5-only, grr. --- googlecharts/templatetags/charts.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/googlecharts/templatetags/charts.py b/googlecharts/templatetags/charts.py index 991bbca..d57aad2 100644 --- a/googlecharts/templatetags/charts.py +++ b/googlecharts/templatetags/charts.py @@ -1,6 +1,5 @@ import sys import inspect -import functools from django import template from django.conf import settings from django.utils.datastructures import SortedDict @@ -240,7 +239,6 @@ def decorator(func): min_args = max_args - len(defaults) unlimited = bool(varargs) - @functools.wraps(func) def template_tag_callback(parser, token): bits = iter(token.split_contents()) name = bits.next() @@ -252,7 +250,8 @@ def template_tag_callback(parser, token): raise template.TemplateSyntaxError("Too many arguments to '%s'" % name) return nodeclass(func, args, multi) - + template_tag_callback.__name__ = func.__name__ + template_tag_callback.__doc__ = func.__doc__ register.tag(tagname, template_tag_callback) return func @@ -606,7 +605,7 @@ def smart_join(sep, *args): from urllib import quote_plus def urlencode(query, safe="/:,|"): - q = functools.partial(quote_plus, safe=safe) + q = lambda v: quote_plus(v, safe=safe) query = getattr(query, 'items', lambda: query)() qlist = ["%s=%s" % (q(k), q(v)) for (k,v) in query] return "&".join(qlist) From 562c0e5a4d6e65b78900d016293defd1158bd10f Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 22 Oct 2008 10:40:38 -0600 Subject: [PATCH 6/7] Fixed an example in the docs. --- docs/examples.html | 4 ++-- docs/examples.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples.html b/docs/examples.html index c38fdb3..98a2d9e 100644 --- a/docs/examples.html +++ b/docs/examples.html @@ -365,9 +365,9 @@

Chart tag examples

{% chart-type "map" %} {% chart-map-area "usa" %} {% chart-map-data mapdata %} - {% chart-colors "fffffff" "ff0000" "0000ff" %} + {% chart-colors "ffffff" "ff0000" "0000ff" %} {% endchart %} - + diff --git a/docs/examples.txt b/docs/examples.txt index 2c68f02..758027c 100644 --- a/docs/examples.txt +++ b/docs/examples.txt @@ -348,6 +348,6 @@ Map chart {% chart-type "map" %} {% chart-map-area "usa" %} {% chart-map-data mapdata %} - {% chart-colors "fffffff" "ff0000" "0000ff" %} + {% chart-colors "ffffff" "ff0000" "0000ff" %} {% endchart %} From 9bbb63d5b89dbab11c172c63d1b2702af95105a2 Mon Sep 17 00:00:00 2001 From: palewire Date: Sat, 10 Jan 2009 04:33:50 +0800 Subject: [PATCH 7/7] Fixed some itty bitty problems with chart_markers Signed-off-by: Jacob Kaplan-Moss --- googlecharts/templatetags/charts.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/googlecharts/templatetags/charts.py b/googlecharts/templatetags/charts.py index d57aad2..b0432ba 100644 --- a/googlecharts/templatetags/charts.py +++ b/googlecharts/templatetags/charts.py @@ -409,24 +409,24 @@ def chart_marker(marker, color, dataset_index, data_point, size): marker = marker_types.get(marker, marker) return {"chm": smart_join(",", marker, color, dataset_index, data_point, size)} -@option("chart-makers", multi="|") +@option("chart-markers", multi="|") def chart_markers(dataset_index, iterable): """Provide an iterable yielding (type, color, point, size)""" try: it = iter(iterable) except TypeError: return {} - + markers = [] for m in it: try: - marker, color, point, size = m + marker, color, data_point, size = m except ValueError: continue - marker - marker_types.get(marker, marker) + marker = marker_types.get(marker, marker) markers.append(smart_join(",", marker, color, dataset_index, data_point, size)) - - return {"chm": smart_join("|", markers)} + + return {"chm": smart_join("|", *flatten(markers))} @option("chart-map-area") def chart_map_area(where):