1 from django
import template
2 from django
.utils
.safestring
import mark_safe
3 from django
.contrib
.staticfiles
.storage
import staticfiles_storage
5 from mygpo
.utils
import format_time
6 from mygpo
.publisher
.utils
import colour_repr
9 register
= template
.Library()
12 def vertical_bar(value
, max_value
, display
=None):
16 if display
== 'ratio':
17 value_str
= '%d/%d' % (value
, max_value
)
19 value_str
= str(value
)
21 ratio
= min(float(value
) / float(max_value
), 1) * 100
23 left
, right
= '<span>'+ value_str
+'</span>', ''
25 left
, right
= ' ', '<span>'+ value_str
+'</span>'
26 s
= '<div class="barbg"><div class="bar" style="width: %.2d%%">%s</div>%s</div>' % (ratio
, left
, right
)
30 def format_diff(value
):
32 s
= '<img src="%s" title="+%s">' % \
33 (staticfiles_storage
.url('better.png'), value
)
35 s
= '<img src="%s" title="%s">' % \
36 (staticfiles_storage
.url('worse.png'), value
)
44 s
= '<script type="text/javascript" src="http://www.google.com/jsapi"></script>\n'
45 s
+= '<script type="text/javascript">\n'
46 s
+= 'google.load("visualization", "1", {"packages":["annotatedtimeline"]});\n'
47 s
+= 'google.setOnLoadCallback(drawChart);\n'
48 s
+= 'function drawChart() {\n'
49 s
+= 'var data = new google.visualization.DataTable();\n'
50 s
+= 'data.addColumn("date", "Date");\n'
51 s
+= 'data.addColumn("number", "Listeners");\n'
52 s
+= 'data.addColumn("string", "title1");\n'
53 s
+= 'data.addColumn("string", "text1");\n'
54 s
+= 'data.addRows([\n'
57 if 'episode' in r
and r
['episode']:
58 episode
= '"%s"' % r
['episode'].title
if r
['episode'].title
else '"Unnamed Episode"'
59 episode_
= '"released"'
62 episode_
= 'undefined'
64 s
+= '[new Date(%d, %d, %d), %d, %s, %s],\n' % (r
['date'].year
, r
['date'].month
-1, r
['date'].day
, r
['listeners'], episode
, episode_
)
67 s
+= 'var chart = new google.visualization.AnnotatedTimeLine(document.getElementById("chart_div"));\n'
68 s
+= 'chart.draw(data, {displayAnnotations: true});\n'
80 'chl=%s' % '|'.join(parts
.iterkeys()),
81 'chd=t:%s' % ','.join([ repr(x
) for x
in parts
.itervalues() ])
84 s
= '<img src="http://chart.apis.google.com/chart?%s"' % '&'.join(parts
)
90 def episode_heatmap_visualization(heatmap
):
92 display a visual heatmap using the Google Charts API
94 heatmap_data is expected as an array of numbers of users that have
95 played this part part of the episode. the length of the parts is
96 indicated by step_length; the duration of the whole episode is
97 therefore approximated by len(heatmap_data) * step_length
101 max_plays
= heatmap
.max_plays
104 # light blue dark blue
105 colours
= ( (198, 217, 253), (77, 137, 249) )
108 colours
= ( (210, 54, 28), (239, 236, 22), (15, 212, 18) )
112 # maximum number of labels that will be placed on the visualization
119 duration
= max(heatmap
.borders
)
122 for start
, end
, plays
in heatmap
.sections
:
124 if last_label
is None or (end
-last_label
) > (duration
/MAX_LABELS
):
126 axis_label
.append(format_time(end
))
129 rgb
= colour_repr(plays
, max_plays
, colours
)
130 part_colours
.append('%02x%02x%02x' % rgb
)
131 widths
.append( end
-start
)
134 'cht=bhs', # bar chart
135 'chco=%s' % ','.join(part_colours
), # colors
136 'chs=%dx50' % WIDTH
, # width corresponds to length, arbitrary height
137 'chds=0,%s' % duration
, # axis scaling from 0 to maximum duration
138 'chd=t:%s' % '|'.join([repr(w
) for w
in widths
]), # all block have the same width
139 'chxt=x', # visible axes
140 'chxr=0,0,%s' % duration
, # axis range for axis 0 (x): 0 - duration
141 'chxl=0:|%s' % '|'.join(axis_label
), # axis labels
142 'chxp=0,%s' % ','.join([repr(x
) for x
in axis_pos
]), # axis label positions
145 s
= '<img src="http://chart.apis.google.com/chart?%s" />' % '&'.join(parts
)
152 def subscriber_change(change
):
156 return '+{0:.1%}'.format(change
)
158 # we don't care about negative changes