Debian Codegraph @

plugins/playlistanalyzer/templates/concept_map.tmpl.html

raw
1 <!DOCTYPE html>
2 <head>
3 <meta charset="utf-8">
4 <meta name="name" content="Concept Map" />
5 <meta name="description" content="An abstract mapping for parameters. Works best if first tag is 'unique' among the tracklist, and the second tag applies to multiple tracks"/>
6 <meta name="mintags" content="2" />
7 <meta name="maxtags" content="2" />
8 <title>%(title)s</title>
9
10 <style>
11
12 svg {
13 font: 12px sans-serif;
14 }
15
16 text {
17 pointer-events: none;
18 }
19
20 .inner_node rect {
21 pointer-events: all;
22 }
23
24 .inner_node rect.highlight {
25 stroke: #315B7E;
26 stroke-width: 2px;
27 }
28
29 .outer_node circle {
30 fill: #fff;
31 stroke: steelblue;
32 stroke-width: 1.5px;
33 pointer-events: all;
34 }
35
36 .outer_node circle.highlight
37 {
38 stroke: #315B7E;
39 stroke-width: 2px;
40 }
41
42 .link {
43 fill: none;
44 }
45
46 </style>
47 </head>
48 <body>
49 <script src="d3.min.js"></script>
50 <script>
51 //
52 // Generated by the Exaile Playlist Analyzer plugin.
53 // (C) 2014 Dustin Spicuzza <[email protected]>
54 //
55 // This work is licensed under the Creative Commons Attribution 4.0
56 // International License. To view a copy of this license, visit
57 // https://creativecommons.org/licenses/by/4.0/.
58 //
59 // Inspired by https://www.findtheconversation.com/concept-map/
60 // Loosely based on http://bl.ocks.org/mbostock/4063550
61 //
62
63
64 var data = %(data)s;
65
66 // transform the data into a useful representation
67 // 1 is inner, 2, is outer
68
69 // need: inner, outer, links
70 //
71 // inner:
72 // links: { inner: outer: }
73
74
75 var outer = d3.map();
76 var inner = [];
77 var links = [];
78
79 var outerId = [0];
80
81 data.forEach(function(d){
82
83 if (d == null)
84 return;
85
86 i = { id: 'i' + inner.length, name: d[0], related_links: [] };
87 i.related_nodes = [i.id];
88 inner.push(i);
89
90 if (!Array.isArray(d[1]))
91 d[1] = [d[1]];
92
93 d[1].forEach(function(d1){
94
95 o = outer.get(d1);
96
97 if (o == null)
98 {
99 o = { name: d1, id: 'o' + outerId[0], related_links: [] };
100 o.related_nodes = [o.id];
101 outerId[0] = outerId[0] + 1;
102
103 outer.set(d1, o);
104 }
105
106 // create the links
107 l = { id: 'l-' + i.id + '-' + o.id, inner: i, outer: o }
108 links.push(l);
109
110 // and the relationships
111 i.related_nodes.push(o.id);
112 i.related_links.push(l.id);
113 o.related_nodes.push(i.id);
114 o.related_links.push(l.id);
115 });
116 });
117
118 data = {
119 inner: inner,
120 outer: outer.values(),
121 links: links
122 }
123
124 // sort the data -- TODO: have multiple sort options
125 outer = data.outer;
126 data.outer = Array(outer.length);
127
128
129 var i1 = 0;
130 var i2 = outer.length - 1;
131
132 for (var i = 0; i < data.outer.length; ++i)
133 {
134 if (i %% 2 == 1)
135 data.outer[i2--] = outer[i];
136 else
137 data.outer[i1++] = outer[i];
138 }
139
140 console.log(data.outer.reduce(function(a,b) { return a + b.related_links.length; }, 0) / data.outer.length);
141
142
143 // from d3 colorbrewer:
144 // This product includes color specifications and designs developed by Cynthia Brewer (http://colorbrewer.org/).
145 var colors = ["#a50026","#d73027","#f46d43","#fdae61","#fee090","#ffffbf","#e0f3f8","#abd9e9","#74add1","#4575b4","#313695"]
146 var color = d3.scale.linear()
147 .domain([60, 220])
148 .range([colors.length-1, 0])
149 .clamp(true);
150
151 var diameter = 960;
152 var rect_width = 40;
153 var rect_height = 14;
154
155 var link_width = "1px";
156
157 var il = data.inner.length;
158 var ol = data.outer.length;
159
160 var inner_y = d3.scale.linear()
161 .domain([0, il])
162 .range([-(il * rect_height)/2, (il * rect_height)/2]);
163
164 mid = (data.outer.length/2.0)
165 var outer_x = d3.scale.linear()
166 .domain([0, mid, mid, data.outer.length])
167 .range([15, 170, 190 ,355]);
168
169 var outer_y = d3.scale.linear()
170 .domain([0, data.outer.length])
171 .range([0, diameter / 2 - 120]);
172
173
174 // setup positioning
175 data.outer = data.outer.map(function(d, i) {
176 d.x = outer_x(i);
177 d.y = diameter/3;
178 return d;
179 });
180
181 data.inner = data.inner.map(function(d, i) {
182 d.x = -(rect_width / 2);
183 d.y = inner_y(i);
184 return d;
185 });
186
187
188 function get_color(name)
189 {
190 var c = Math.round(color(name));
191 if (isNaN(c))
192 return '#dddddd'; // fallback color
193
194 return colors[c];
195 }
196
197 // Can't just use d3.svg.diagonal because one edge is in normal space, the
198 // other edge is in radial space. Since we can't just ask d3 to do projection
199 // of a single point, do it ourselves the same way d3 would do it.
200
201
202 function projectX(x)
203 {
204 return ((x - 90) / 180 * Math.PI) - (Math.PI/2);
205 }
206
207 var diagonal = d3.svg.diagonal()
208 .source(function(d) { return {"x": d.outer.y * Math.cos(projectX(d.outer.x)),
209 "y": -d.outer.y * Math.sin(projectX(d.outer.x))}; })
210 .target(function(d) { return {"x": d.inner.y + rect_height/2,
211 "y": d.outer.x > 180 ? d.inner.x : d.inner.x + rect_width}; })
212 .projection(function(d) { return [d.y, d.x]; });
213
214
215 var svg = d3.select("body").append("svg")
216 .attr("width", diameter)
217 .attr("height", diameter)
218 .append("g")
219 .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
220
221
222 // links
223 var link = svg.append('g').attr('class', 'links').selectAll(".link")
224 .data(data.links)
225 .enter().append('path')
226 .attr('class', 'link')
227 .attr('id', function(d) { return d.id })
228 .attr("d", diagonal)
229 .attr('stroke', function(d) { return get_color(d.inner.name); })
230 .attr('stroke-width', link_width);
231
232 // outer nodes
233
234 var onode = svg.append('g').selectAll(".outer_node")
235 .data(data.outer)
236 .enter().append("g")
237 .attr("class", "outer_node")
238 .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
239 .on("mouseover", mouseover)
240 .on("mouseout", mouseout);
241
242 onode.append("circle")
243 .attr('id', function(d) { return d.id })
244 .attr("r", 4.5);
245
246 onode.append("circle")
247 .attr('r', 20)
248 .attr('visibility', 'hidden');
249
250 onode.append("text")
251 .attr('id', function(d) { return d.id + '-txt'; })
252 .attr("dy", ".31em")
253 .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
254 .attr("transform", function(d) { return d.x < 180 ? "translate(8)" : "rotate(180)translate(-8)"; })
255 .text(function(d) { return d.name; });
256
257 // inner nodes
258
259 var inode = svg.append('g').selectAll(".inner_node")
260 .data(data.inner)
261 .enter().append("g")
262 .attr("class", "inner_node")
263 .attr("transform", function(d, i) { return "translate(" + d.x + "," + d.y + ")"})
264 .on("mouseover", mouseover)
265 .on("mouseout", mouseout);
266
267 inode.append('rect')
268 .attr('width', rect_width)
269 .attr('height', rect_height)
270 .attr('id', function(d) { return d.id; })
271 .attr('fill', function(d) { return get_color(d.name); });
272
273 inode.append("text")
274 .attr('id', function(d) { return d.id + '-txt'; })
275 .attr('text-anchor', 'middle')
276 .attr("transform", "translate(" + rect_width/2 + ", " + rect_height * .75 + ")")
277 .text(function(d) { return d.name; });
278
279 // need to specify x/y/etc
280
281 d3.select(self.frameElement).style("height", diameter - 150 + "px");
282
283 function mouseover(d)
284 {
285 // bring to front
286 d3.selectAll('.links .link').sort(function(a, b){ return d.related_links.indexOf(a.id); });
287
288 for (var i = 0; i < d.related_nodes.length; i++)
289 {
290 d3.select('#' + d.related_nodes[i]).classed('highlight', true);
291 d3.select('#' + d.related_nodes[i] + '-txt').attr("font-weight", 'bold');
292 }
293
294 for (var i = 0; i < d.related_links.length; i++)
295 d3.select('#' + d.related_links[i]).attr('stroke-width', '5px');
296 }
297
298 function mouseout(d)
299 {
300 for (var i = 0; i < d.related_nodes.length; i++)
301 {
302 d3.select('#' + d.related_nodes[i]).classed('highlight', false);
303 d3.select('#' + d.related_nodes[i] + '-txt').attr("font-weight", 'normal');
304 }
305
306 for (var i = 0; i < d.related_links.length; i++)
307 d3.select('#' + d.related_links[i]).attr('stroke-width', link_width);
308 }
309
310 </script>
311
312
313
314
315
316
317