From d6524b72dc50f4417dfed551a140a865b1052df1 Mon Sep 17 00:00:00 2001 From: Felipe Vieira Date: Tue, 20 Mar 2012 22:26:02 +0000 Subject: [PATCH] melhorando api de graficos --- sigi/apps/diagnosticos/models.py | 12 ++++++++ sigi/apps/diagnosticos/views.py | 35 ++++++++++++++++------- sigi/templates/diagnosticos/graficos.html | 18 +++++++----- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/sigi/apps/diagnosticos/models.py b/sigi/apps/diagnosticos/models.py index 58aba16..7e1c8a0 100644 --- a/sigi/apps/diagnosticos/models.py +++ b/sigi/apps/diagnosticos/models.py @@ -171,6 +171,18 @@ class Pergunta(BaseSchema): for row in cursor.fetchall() ] + def total_anwsers(self): + from django.db import connection, transaction + cursor = connection.cursor() + + cursor.execute(""" + SELECT sum(1) + FROM diagnosticos_resposta + WHERE schema_id=%s + """, [self.id]) + + return cursor.fetchone() + class Meta: ordering = ('title',) verbose_name, verbose_name_plural = 'pergunta', 'perguntas' diff --git a/sigi/apps/diagnosticos/views.py b/sigi/apps/diagnosticos/views.py index 9c49e97..aa85539 100644 --- a/sigi/apps/diagnosticos/views.py +++ b/sigi/apps/diagnosticos/views.py @@ -237,9 +237,16 @@ def graficos(request): return render_to_response('diagnosticos/graficos.html', context) +def percentage(fraction, population): + try: + return "%.0f%%" % ((float(fraction) / float(population)) * 100) + except ValueError: + return '' + def grafico_api(request): graph_url = "http://chart.apis.google.com/chart" - graph_params = QueryDict("chxt=y&chbh=a&chco=A2C180,3D7930") + #graph_params = QueryDict("chxt=y&chbh=a&chco=A2C180,3D7930") + graph_params = QueryDict("") graph_params = graph_params.copy() # to make it mutable width = request.REQUEST.get('width', '300') @@ -249,22 +256,29 @@ def grafico_api(request): pergunta_slug = request.REQUEST.get('id', None) pergunta = get_object_or_404(Pergunta, name=pergunta_slug) - if pergunta.datatype == 'many': - choices = [r[1] for r in pergunta.get_choices()] + if pergunta.datatype == 'one': + total = sum([r[1] for r in pergunta.group_choices()]) + choices = [str(r[1]) for r in pergunta.group_choices()] + legend = [percentage(r[1],total) + " " + str(r[0]) for r in pergunta.group_choices()] graph_params.update({ - 'cht': 'bvg', - 'chxt' : 'y', - 'chd': 't:' + ",".join(choices) + 'cht': 'p', + 'chd': 't:' + ",".join(choices), + 'chdl': '' + "|".join(legend), }) - elif pergunta.datatype == 'one': + elif pergunta.datatype == 'many': + total = sum([r[1] for r in pergunta.group_choices()]) + percent = [str(float(r[1])*100/total) for r in pergunta.group_choices()] choices = [str(r[1]) for r in pergunta.group_choices()] + legend = [str(r[0]) for r in pergunta.group_choices()] graph_params.update({ - 'cht': 'p', - 'chd': 't:' + ",".join(choices) + 'cht': 'bvg', + 'chxt': 'y', + 'chd': 't:' + ",".join(percent), + 'chdl': '' + "|".join(legend), + 'chl': '' + "|".join(choices), }) response = { - "version": "1.0", "type": "photo", "width": width, "height": height, @@ -276,5 +290,4 @@ def grafico_api(request): json = simplejson.dumps(response) return HttpResponse(json, mimetype="application/json") - return redirect(response['url']) diff --git a/sigi/templates/diagnosticos/graficos.html b/sigi/templates/diagnosticos/graficos.html index b8c35e4..969d17f 100644 --- a/sigi/templates/diagnosticos/graficos.html +++ b/sigi/templates/diagnosticos/graficos.html @@ -43,13 +43,17 @@ jQuery(document).ready(function () {