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. 18
      sigi/templates/diagnosticos/graficos.html

12
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'

35
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'])

18
sigi/templates/diagnosticos/graficos.html

@ -43,13 +43,17 @@ jQuery(document).ready(function () {
</form>
<div id="perguntas">
<ul>
{% for pergunta in perguntas%}
<li>
<a id="{{pergunta.name}}" href="/sigi/diagnosticos/grafico_api/?id={{pergunta.name}}">{{pergunta.title}}</a>
<div id="output_{{pergunta.name}}" class="grafico">
<img style="display:none"/>
</div>
</li>
{% for pergunta in perguntas %}
<li>
{% 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">
<img style="display:none"/>
</div>
{% else %}
<p>{{pergunta.title}}</p>
{% endif %}
</li>
{% endfor %}
</ul>
</div>

Loading…
Cancel
Save