d292f4e44a905813427df53016b73570624678b4
1 from django
import template
2 from django
.utils
.safestring
import mark_safe
4 from mygpo
.utils
import format_time
5 from mygpo
.publisher
.utils
import colour_repr
8 register
= template
.Library()
11 def vertical_bar(value
, max_value
, display
=None):
15 if display
== 'ratio':
16 value_str
= '%d/%d' % (value
, max_value
)
18 value_str
= str(value
)
20 ratio
= min(float(value
) / float(max_value
), 1) * 100
22 left
, right
= '<span>'+ value_str
+'</span>', ''
24 left
, right
= ' ', '<span>'+ value_str
+'</span>'
25 s
= '<div class="barbg"><div class="bar" style="width: %.2d%%">%s</div>%s</div>' % (ratio
, left
, right
)
29 def format_diff(value
):
31 s
= '<img src="/media/better.png" title="+%s">' % value
33 s
= '<img src="/media/worse.png" title="%s">' % value
41 s
= '<script type="text/javascript" src="http://www.google.com/jsapi"></script>\n'
42 s
+= '<script type="text/javascript">\n'
43 s
+= 'google.load("visualization", "1", {"packages":["annotatedtimeline"]});\n'
44 s
+= 'google.setOnLoadCallback(drawChart);\n'
45 s
+= 'function drawChart() {\n'
46 s
+= 'var data = new google.visualization.DataTable();\n'
47 s
+= 'data.addColumn("date", "Date");\n'
48 s
+= 'data.addColumn("number", "Listeners");\n'
49 s
+= 'data.addColumn("string", "title1");\n'
50 s
+= 'data.addColumn("string", "text1");\n'
51 s
+= 'data.addRows([\n'
54 if 'episode' in r
and r
['episode']:
55 episode
= '"%s"' % r
['episode'].title
if r
['episode'].title
else '"Unnamed Episode"'
56 episode_
= '"released"'
59 episode_
= 'undefined'
61 s
+= '[new Date(%d, %d, %d), %d, %s, %s],\n' % (r
['date'].year
, r
['date'].month
-1, r
['date'].day
, r
['listeners'], episode
, episode_
)
64 s
+= 'var chart = new google.visualization.AnnotatedTimeLine(document.getElementById("chart_div"));\n'
65 s
+= 'chart.draw(data, {displayAnnotations: true});\n'
77 'chl=%s' % '|'.join(parts
.iterkeys()),
78 'chd=t:%s' % ','.join([ repr(x
) for x
in parts
.itervalues() ])
81 s
= '<img src="http://chart.apis.google.com/chart?%s"' % '&'.join(parts
)
87 def episode_heatmap_visualization(heatmap
):
89 display a visual heatmap using the Google Charts API
91 heatmap_data is expected as an array of numbers of users that have
92 played this part part of the episode. the length of the parts is
93 indicated by step_length; the duration of the whole episode is
94 therefore approximated by len(heatmap_data) * step_length
98 max_plays
= heatmap
.max_plays
101 # light blue dark blue
102 colours
= ( (198, 217, 253), (77, 137, 249) )
105 colours
= ( (210, 54, 28), (239, 236, 22), (15, 212, 18) )
109 # maximum number of labels that will be placed on the visualization
116 duration
= max(heatmap
.borders
)
119 for start
, end
, plays
in heatmap
.sections
:
121 if last_label
is None or (end
-last_label
) > (duration
/MAX_LABELS
):
123 axis_label
.append(format_time(end
))
126 rgb
= colour_repr(plays
, max_plays
, colours
)
127 part_colours
.append('%02x%02x%02x' % rgb
)
128 widths
.append( end
-start
)
131 'cht=bhs', # bar chart
132 'chco=%s' % ','.join(part_colours
), # colors
133 'chs=%dx50' % WIDTH
, # width corresponds to length, arbitrary height
134 'chds=0,%s' % duration
, # axis scaling from 0 to maximum duration
135 'chd=t:%s' % '|'.join([repr(w
) for w
in widths
]), # all block have the same width
136 'chxt=x', # visible axes
137 'chxr=0,0,%s' % duration
, # axis range for axis 0 (x): 0 - duration
138 'chxl=0:|%s' % '|'.join(axis_label
), # axis labels
139 'chxp=0,%s' % ','.join([repr(x
) for x
in axis_pos
]), # axis label positions
142 s
= '<img src="http://chart.apis.google.com/chart?%s" />' % '&'.join(parts
)
149 def subscriber_change(change
):
153 return '+{0:.1%}'.format(change
)
155 # we don't care about negative changes