Browse Source

melhorando api de graficos

stable/1.0
Felipe Vieira 13 years ago
parent
commit
d6524b72dc
  1. 12
      sigi/apps/diagnosticos/models.py
  2. 35
      sigi/apps/diagnosticos/views.py
  3. 6
      sigi/templates/diagnosticos/graficos.html

12
sigi/apps/diagnosticos/models.py

@ -171,6 +171,18 @@ class Pergunta(BaseSchema):
for row in cursor.fetchall() 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: class Meta:
ordering = ('title',) ordering = ('title',)
verbose_name, verbose_name_plural = 'pergunta', 'perguntas' verbose_name, verbose_name_plural = 'pergunta', 'perguntas'

35
sigi/apps/diagnosticos/views.py

@ -237,9 +237,16 @@ def graficos(request):
return render_to_response('diagnosticos/graficos.html', return render_to_response('diagnosticos/graficos.html',
context) context)
def percentage(fraction, population):
try:
return "%.0f%%" % ((float(fraction) / float(population)) * 100)
except ValueError:
return ''
def grafico_api(request): def grafico_api(request):
graph_url = "http://chart.apis.google.com/chart" 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 graph_params = graph_params.copy() # to make it mutable
width = request.REQUEST.get('width', '300') width = request.REQUEST.get('width', '300')
@ -249,22 +256,29 @@ def grafico_api(request):
pergunta_slug = request.REQUEST.get('id', None) pergunta_slug = request.REQUEST.get('id', None)
pergunta = get_object_or_404(Pergunta, name=pergunta_slug) pergunta = get_object_or_404(Pergunta, name=pergunta_slug)
if pergunta.datatype == 'many': if pergunta.datatype == 'one':
choices = [r[1] for r in pergunta.get_choices()] 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({ graph_params.update({
'cht': 'bvg', 'cht': 'p',
'chxt' : 'y', 'chd': 't:' + ",".join(choices),
'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()] choices = [str(r[1]) for r in pergunta.group_choices()]
legend = [str(r[0]) for r in pergunta.group_choices()]
graph_params.update({ graph_params.update({
'cht': 'p', 'cht': 'bvg',
'chd': 't:' + ",".join(choices) 'chxt': 'y',
'chd': 't:' + ",".join(percent),
'chdl': '' + "|".join(legend),
'chl': '' + "|".join(choices),
}) })
response = { response = {
"version": "1.0",
"type": "photo", "type": "photo",
"width": width, "width": width,
"height": height, "height": height,
@ -276,5 +290,4 @@ def grafico_api(request):
json = simplejson.dumps(response) json = simplejson.dumps(response)
return HttpResponse(json, mimetype="application/json") return HttpResponse(json, mimetype="application/json")
return redirect(response['url'])

6
sigi/templates/diagnosticos/graficos.html

@ -45,10 +45,14 @@ jQuery(document).ready(function () {
<ul> <ul>
{% for pergunta in perguntas %} {% for pergunta in perguntas %}
<li> <li>
<a id="{{pergunta.name}}" href="/sigi/diagnosticos/grafico_api/?id={{pergunta.name}}">{{pergunta.title}}</a> {% if pergunta.datatype == 'many' or pergunta.datatype == 'one' %}
<a id="{{pergunta.name}}" href="/sigi/diagnosticos/grafico_api/?id={{pergunta.name}}"><p>{{pergunta.title}}</p></a>
<div id="output_{{pergunta.name}}" class="grafico"> <div id="output_{{pergunta.name}}" class="grafico">
<img style="display:none"/> <img style="display:none"/>
</div> </div>
{% else %}
<p>{{pergunta.title}}</p>
{% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>

Loading…
Cancel
Save