Debian Codegraph @

plugins/playlistanalyzer/templates/bar_chart.tmpl.html

raw
1 <!DOCTYPE html>
2 <head>
3 <meta charset="utf-8" />
4 <meta name="name" content="Bar chart" />
5 <meta name="description" content="Simple bar chart showing frequency of tags in a playlist"/>
6 <meta name="mintags" content="1" />
7 <meta name="maxtags" content="1" />
8 <title>%(title)s</title>
9
10 <style>
11
12 .bar {
13 fill: steelblue;
14 }
15
16 .bar:hover {
17 fill: brown;
18 }
19
20 .axis {
21 font: 10px sans-serif;
22 }
23
24 .axis path,
25 .axis line {
26 fill: none;
27 stroke: #000;
28 shape-rendering: crispEdges;
29 }
30
31 .x.axis path {
32 display: none;
33 }
34
35 </style>
36 </head>
37 <body>
38 <script src="d3.min.js"></script>
39 <script>
40
41
42 //
43 // Generated by the Exaile Playlist Analyzer plugin.
44 // (C) 2014 Dustin Spicuzza <[email protected]>
45 //
46 // This work is licensed under the Creative Commons Attribution 4.0
47 // International License. To view a copy of this license, visit
48 // https://creativecommons.org/licenses/by/4.0/.
49 //
50 // Copied mostly verbatim from http://bl.ocks.org/mbostock/3885304
51 //
52
53 var data = %(data)s;
54 var taglist = %(tagdata)s;
55
56 // translate data into something d3 can use.
57 var vals = d3.map();
58
59 // simple frequency counter. probably more interesting things
60 // we can do with this..
61 data.forEach(function(d){
62
63 if (d == null)
64 return;
65
66 if (d[0] == null) d[0] = '<unknown>'
67
68 // some tags are array elements
69 if (Array.isArray(d[0]))
70 d[0].forEach(function(d2){
71 vals.set(d2, (vals.get(d2) || 0) + 1);
72 });
73 else
74 vals.set(d[0], (vals.get(d[0]) || 0) + 1);
75 });
76
77 data = vals.entries().sort(function(a,b){
78 return a.key < b.key ? -1 : 1;
79 });
80
81
82 // generic d3 code follows...
83
84 // cheating here because I'm lazy and don't want to calculate
85 // the label size
86 var margin = {top: 20, right: 20, bottom: 500, left: 40},
87 width = 960 - margin.left - margin.right,
88 height = 1000 - margin.top - margin.bottom;
89
90 var x = d3.scale.ordinal()
91 .rangeRoundBands([0, width], .1);
92
93 var y = d3.scale.linear()
94 .range([height, 0]);
95
96 var xAxis = d3.svg.axis()
97 .scale(x)
98 .orient("bottom");
99
100 var yAxis = d3.svg.axis()
101 .scale(y)
102 .orient("left")
103 .ticks(10);
104
105 var svg = d3.select("body").append("svg")
106 .attr("width", width + margin.left + margin.right)
107 .attr("height", height + margin.top + margin.bottom)
108 .append("g")
109 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
110
111 x.domain(data.map(function(d) { return d.key; }));
112 y.domain([0, d3.max(data, function(d) { return d.value; })]);
113
114 svg.append("g")
115 .attr("class", "x axis")
116 .attr("transform", "translate(0," + height + ")")
117 .call(xAxis)
118 .selectAll("text")
119 .style("text-anchor", "end")
120 .attr("dx", "-.8em")
121 .attr("dy", ".15em")
122 .attr("transform", function(d) {
123 return "rotate(-75)"
124 });
125
126 svg.append("g")
127 .attr("class", "y axis")
128 .call(yAxis)
129 .append("text")
130 .attr("transform", "rotate(-90)")
131 .attr("y", 6)
132 .attr("dy", ".71em")
133 .style("text-anchor", "end")
134 .text("Frequency");
135
136 svg.selectAll(".bar")
137 .data(data)
138 .enter().append("rect")
139 .attr("class", "bar")
140 .attr("x", function(d) { return x(d.key); })
141 .attr("width", x.rangeBand())
142 .attr("y", function(d) { return y(d.value); })
143 .attr("height", function(d) { return height - y(d.value); });
144
145 </script>