remove unnecessary imports
[mygpo.git] / mygpo / web / templatetags / charts.py
blob6d688917279154f5090c9fb6297cff6f67d683d1
1 from django import template
2 from django.utils.safestring import mark_safe
4 register = template.Library()
6 @register.filter
7 def vertical_bar(value, max):
8 if max == 0:
9 return ''
11 ratio = float(value) / float(max) * 100
12 if ratio > 40:
13 left, right = '<span>'+str(value)+'</span>', ''
14 else:
15 left, right = '&nbsp;', '<span>'+str(value)+'</span>'
16 s = '<div class="barbg"><div class="bar" style="width: %s">%s</div>%s</div>' % (ratio, left, right)
17 return mark_safe(s)
19 @register.filter
20 def format_diff(value):
21 if value > 1:
22 s = '<img src="/media/better.png" title="+%s">' % value
23 elif value < -1:
24 s = '<img src="/media/worse.png" title="%s">' % value
25 else:
26 s = ''
28 return mark_safe(s)
30 @register.filter
31 def timeline(data):
32 s = '<script type="text/javascript" src="http://www.google.com/jsapi"></script>\n'
33 s += '<script type="text/javascript">\n'
34 s += 'google.load("visualization", "1", {"packages":["annotatedtimeline"]});\n'
35 s += 'google.setOnLoadCallback(drawChart);\n'
36 s += 'function drawChart() {\n'
37 s += 'var data = new google.visualization.DataTable();\n'
38 s += 'data.addColumn("date", "Date");\n'
39 s += 'data.addColumn("number", "Listeners");\n'
40 s += 'data.addColumn("string", "title1");\n'
41 s += 'data.addColumn("string", "text1");\n'
42 s += 'data.addRows([\n'
44 for r in data:
45 if 'episode' in r and r['episode']:
46 episode = '"%s"' % r['episode'].title if r['episode'].title else '"Unnamed Episode"'
47 episode_ = '"released"'
48 else:
49 episode = 'undefined'
50 episode_ = 'undefined'
52 s += '[new Date(%d, %d, %d), %d, %s, %s],\n' % (r['date'].year, r['date'].month-1, r['date'].day, r['listeners'], episode, episode_)
54 s += ']);\n'
55 s += 'var chart = new google.visualization.AnnotatedTimeLine(document.getElementById("chart_div"));\n'
56 s += 'chart.draw(data, {displayAnnotations: true});\n'
57 s += '}\n'
58 s += '</script>\n'
60 return mark_safe(s)
63 @register.filter
64 def pie_chart(parts):
65 parts = [
66 'cht=p',
67 'chs=250x100',
68 'chl=%s' % '|'.join(parts.iterkeys()),
69 'chd=t:%s' % ','.join([ repr(x) for x in parts.itervalues() ])
72 s = '<img src="http://chart.apis.google.com/chart?%s"' % '&'.join(parts)
74 return mark_safe(s)