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 # handle value == None
25 ratio
= min(float(value
) / float(max_value
), 1) * 100
30 left
, right
= '<span>'+ value_str
+'</span>', ''
32 left
, right
= ' ', '<span>'+ value_str
+'</span>'
33 s
= '<div class="barbg"><div class="bar" style="width: %.2d%%">%s</div>%s</div>' % (ratio
, left
, right
)
38 s
= '<script type="text/javascript" src="//www.google.com/jsapi"></script>\n'
39 s
+= '<script type="text/javascript">\n'
40 s
+= 'google.load("visualization", "1", {"packages":["annotatedtimeline"]});\n'
41 s
+= 'google.setOnLoadCallback(drawChart);\n'
42 s
+= 'function drawChart() {\n'
43 s
+= 'var data = new google.visualization.DataTable();\n'
44 s
+= 'data.addColumn("date", "Date");\n'
45 s
+= 'data.addColumn("number", "Listeners");\n'
46 s
+= 'data.addColumn("string", "title1");\n'
47 s
+= 'data.addColumn("string", "text1");\n'
48 s
+= 'data.addRows([\n'
51 if 'episode' in r
and r
['episode']:
52 episode
= '"%s"' % r
['episode'].title
if r
['episode'].title
else '"Unnamed Episode"'
53 episode_
= '"released"'
56 episode_
= 'undefined'
58 s
+= '[new Date(%d, %d, %d), %d, %s, %s],\n' % (r
['date'].year
, r
['date'].month
-1, r
['date'].day
, r
['listeners'], episode
, episode_
)
61 s
+= 'var chart = new google.visualization.AnnotatedTimeLine(document.getElementById("chart_div"));\n'
62 s
+= 'chart.draw(data, {displayAnnotations: true});\n'
74 'chl=%s' % '|'.join(parts
.iterkeys()),
75 'chd=t:%s' % ','.join([ repr(x
) for x
in parts
.itervalues() ])
78 s
= '<img src="http://chart.apis.google.com/chart?%s"' % '&'.join(parts
)
84 def episode_heatmap_visualization(heatmap
):
86 display a visual heatmap using the Google Charts API
88 heatmap_data is expected as an array of numbers of users that have
89 played this part part of the episode. the length of the parts is
90 indicated by step_length; the duration of the whole episode is
91 therefore approximated by len(heatmap_data) * step_length
95 max_plays
= heatmap
.max_plays
98 # light blue dark blue
99 colours
= ( (198, 217, 253), (77, 137, 249) )
102 colours
= ( (210, 54, 28), (239, 236, 22), (15, 212, 18) )
106 # maximum number of labels that will be placed on the visualization
113 duration
= max(heatmap
.borders
)
116 for start
, end
, plays
in heatmap
.sections
:
118 if last_label
is None or (end
-last_label
) > (duration
/MAX_LABELS
):
120 axis_label
.append(format_time(end
))
123 rgb
= colour_repr(plays
, max_plays
, colours
)
124 part_colours
.append('%02x%02x%02x' % rgb
)
125 widths
.append( end
-start
)
128 'cht=bhs', # bar chart
129 'chco=%s' % ','.join(part_colours
), # colors
130 'chs=%dx50' % WIDTH
, # width corresponds to length, arbitrary height
131 'chds=0,%s' % duration
, # axis scaling from 0 to maximum duration
132 'chd=t:%s' % '|'.join([repr(w
) for w
in widths
]), # all block have the same width
133 'chxt=x', # visible axes
134 'chxr=0,0,%s' % duration
, # axis range for axis 0 (x): 0 - duration
135 'chxl=0:|%s' % '|'.join(axis_label
), # axis labels
136 'chxp=0,%s' % ','.join([repr(x
) for x
in axis_pos
]), # axis label positions
139 s
= '<img src="http://chart.apis.google.com/chart?%s" />' % '&'.join(parts
)
146 def subscriber_change(change
):
150 return '+{0:.1%}'.format(change
)
152 # we don't care about negative changes