-
Notifications
You must be signed in to change notification settings - Fork 1
/
radial_plot.js
276 lines (224 loc) · 7.33 KB
/
radial_plot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var radialPath;
var similRadialPaths=[];
var cat_radial_scale;
var radial_plot_center;
//this is called when I click a pint in the scatter plot
function updateRadialPlot(targetSong,simil,this_artist,named=true){
//console.log("UPDATE RADIAL PLOT\n\n")
var song={"original":targetSong,"radial_data":[]}
//console.log("limits", cat_limits)
var targetSongNoCoords=targetSong[2];
for(var i=0;i<categories.length;i++){
var value=0
if (named){
value=norm_min_max(targetSongNoCoords[categories[i]], cat_limits[i][0], cat_limits[i][1])
}
else {
value=targetSongNoCoords[i]
}
song.radial_data.push(
{category: categories[i],value: value})
}
//console.log("radial plot updated with values:", song)
song.radial_data.push(song.radial_data[0])
let simil_songs=[]
for (var j=0;j<simil.length;j++){
let simil_song={"original":simil[j]["data"],"radial_data":[]}
let processed_song=simil[j]["data"][2]
for(var i=0;i<categories.length;i++){
var value=0
if (simil[j].length==0) continue;
if (named){
value=norm_min_max(processed_song[categories[i]], cat_limits[i][0], cat_limits[i][1])
}
else {
value=processed_song[i]
}
simil_song.radial_data.push(
{category: categories[i], value: value})
}
simil_song.radial_data.push(simil_song.radial_data[0])
simil_songs.push(simil_song)
}
console.log("simil songs",simil_songs)
radial=d3.radialLine()
.radius( function(d,i) {
// if is used for closure of plot
if (i>=cat_radial_scale.length) return cat_radial_scale[0](d.value)
return cat_radial_scale[i](d.value) } )
.angle((d, i) => (2*Math.PI / (categories.length)) * i)
.curve(d3.curveCatmullRom)
for (var simil_idx=0;simil_idx<simil.length;simil_idx++){
similRadialPaths[simil_idx]
.attr('transform', `translate(${radial_plot_center.x},${radial_plot_center.y})`)
.datum(simil_songs[simil_idx].original)
.transition()
.attr('d', radial(simil_songs[simil_idx].radial_data))
.attr('fill', 'none')
.attr('stroke', simil_colors[simil_idx])
.attr('stroke-width', 3)
}
radialPath
.attr('transform', `translate(${radial_plot_center.x},${radial_plot_center.y})`)
.datum(song.original)
.transition()
.attr('d', radial(song.radial_data))
.attr('fill', 'none')
.attr('stroke', '#d73027')
.attr('stroke-width', 4)
d3.selectAll(".radialPath").on("click", function(d) {
radialClick(d,this_artist)
})
.on("mouseover", function(d) {
d3.select(this).attr('stroke-width', 5)
})
.on("mouseout", function(d) {
d3.select(this).attr('stroke-width', 4)
});
d3.selectAll(".similRadialPath").on("click", function(d) {
radialClick(d,this_artist)
})
.on("mouseover", function(d) {
d3.select(this).attr('stroke-width', 4)
})
.on("mouseout", function(d) {
d3.select(this).attr('stroke-width', 3)
});
}
function radialClick(d,this_artist){
//console.log("radial click",d)
console.log("radial click",d)
onClick(this_artist,d.originalTarget.__data__)
}
function radialPlotMain() {
const svg = d3.select('#radial-graph')
.append('svg');
const div_height = document.getElementById("radial-graph").clientHeight;
const div_width = document.getElementById("radial-graph").clientWidth;
const radius = Math.min(div_width/2-(div_width*0.1), div_height/2-(div_height*0.15));
//console.log(radius);
radial_plot_center= {x: div_width/2, y: div_height/2};
center=radial_plot_center;
//initialize plot with empty data
var song=[]
for(var i=0;i<categories.length;i++){
song.push(
{category:categories[i],
value:0
})
}
song.push(song[0])
let simil_songs=[]
for (var j=0;j<K_nearest;j++){
let simil_song=[]
for(var i=0;i<categories.length;i++){
simil_song.push({category: categories[i], value: 0})
}
simil_song.push(simil_song[0])
simil_songs.push(simil_song)
}
//create circle plot
//secondo me questo non serve a niente per fare i cerchi
// ne fa sempre 5 indipendentemente da maxValue
const maxValue = 1;
const radialScale = d3.scaleLinear()
.domain([0, maxValue])
.range([0, radius])
let val, angle;
for (val = 0; val <= maxValue; val += maxValue / 5) {
const r = radialScale(val);
svg.append('circle')
.attr('cx', center.x)
.attr('cy', center.y)
.attr('r', r)
.style('stroke', '#aaa')
.style('fill', 'none');
}
//console.log("cat limits",cat_limits)
//create a linear scale for each category
cat_radial_scale=[]
for (let i=0;i<song.length-1;i++){
cat_radial_scale.push(d3.scaleLinear()
//.domain([cat_limits[i][0],cat_limits[i][1]])
.domain([0,1])
.range([0,radius])
)
}
//create the axis for the plot
axs=[]
for (let i=0; i<categories.length; i++){
n_ticks=0
if (i==0){
n_ticks=5
}
//create axis for this category
axis=d3.axisRight()
.scale(cat_radial_scale[i])
.ticks(n_ticks)
axs.push(axis)
//insert axis, rotate axis and rotate text of ticks
const angle = 180-(i * 360 / categories.length);
//to do, move tick label so they are visible
var tickTranslate=(angle/360)*20
if (angle%180==0) tickTranslate=0
tickTranslate=0
var axisSvg=svg.append('g')
.attr('transform', ` translate(${center.x},${center.y}) rotate(${angle})`)
.call(axis)
.style('fill-opacity', d => d === 0 ? 0.0 : 1.0)
.selectAll("text")
.attr("transform", "translate("+ (tickTranslate)+","+(tickTranslate)+") rotate(" + (-angle) + ")")
.style("text-anchor", "start");
//axisSvg.call(axis).filter(function (d) { return d === 0; }).remove()
svg.selectAll(".tick")
.each(function (d) {
if ( d === 0 ) {
this.remove();
}
});
const angle_rad=i * Math.PI*2 / categories.length;
const x = center.x + radius*1.3 * Math.sin(angle_rad);
const y = center.y + radius*1.3 * -Math.cos(angle_rad);
//console.log("label angle",angle_rad,categories[i])
let text_rotate=10
//if (i%2!=0) text_rotate=10
//console.log("text ",categories[i],"rotate",text_rotate)
//add label
svg.append('text')
.text(categories[i])
.style('text-anchor', 'middle')
.style("font-size", "10px")
//.attr('transform', `translate(${x},${y})`)
.attr('x', x)
.attr('y', y)
.attr("class", "radial-category-label")
svg.selectAll(".radial-category-label")
//.style("text-anchor", "end")
.attr('transform', (d,i)=>{
return 'rotate(0)';})
}
//create the plot
radial=d3.radialLine()
.radius( function(d,i) {
// if is used for closure of plot
if (i>=cat_radial_scale.length) return cat_radial_scale[0](d.value)
return cat_radial_scale[i](d.value) } )
.angle((d, i) => (2*Math.PI / (categories.length)) * i)
.curve(d3.curveCatmullRom)
//add similar songs to plot
for (let i=0; i<K_nearest; i++){
let similRadialPath=svg.append('path')
.datum(simil_songs[i])
.attr('d', radial)
.attr("class", "similRadialPath")
similRadialPaths.push(similRadialPath)
}
//add radial plot to page
radialPath=svg.append('path')
.datum(song)
.attr('d', radial)
.attr("class", "radialPath")
d3.selectAll(".radialPath").on("click", function(d) {
radialClick(d)
});
}