-
Notifications
You must be signed in to change notification settings - Fork 4
/
visualiser.js
196 lines (177 loc) · 4.83 KB
/
visualiser.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
const visualiserID = '#my_dataviz';
/**
* @param {number[][]} rawData
*/
const visualiserRun = (rawData, tokens) => {
// set the dimensions and margins of the graph
containerWidth = window.innerWidth * 0.9;
containerWidth = containerWidth > 900 ? 900 : containerWidth;
let length = containerWidth * 0.7;
const margin = {
top: 120,
right: Math.floor((containerWidth - length) / 2),
bottom: 0,
left: Math.floor((containerWidth - length) / 2),
};
const width = containerWidth - margin.right - margin.left;
const height = width;
const data = prepareData(rawData, tokens);
const svg = visualiserInitialiseHeatmap(width, height, margin);
const [xScale, yScale, colorScale] = visualiserBuildScale(
svg,
width,
height,
Array.from(new Set(data.map((d) => d.layerTo))),
Array.from(new Set(data.map((d) => d.layerFrom))),
tokens
);
const [mouseover, mousemove, mouseleave] = visualiserCreateTooltip();
visualiserVisualise(
svg,
data,
xScale,
yScale,
colorScale,
mouseover,
mousemove,
mouseleave
);
};
/**
* @param {number[][]} rawData
* @return {{layerFrom: number, layerTo: number, attention: number}[]} parsedData
*/
const prepareData = (rawData) => {
const parsedData = [];
rawData.forEach((data, index) => {
data.forEach((attention, i) => {
parsedData.push({
layerFrom: index,
layerTo: i,
attention,
});
});
});
return parsedData;
};
/**
* @param {number} width
* @param {number} height
* @param {number} margin
*/
const visualiserInitialiseHeatmap = (width, height, margin) => {
document.querySelector(visualiserID).innerHTML = '';
// append the svg object to the body of the page
const svg = d3
.select(visualiserID)
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
return svg;
};
/**
* @param {number} width
* @param {number} height
* @param {number[]} xValues
* @param {number[]} yValues
* @param {string[]} tokens
*/
const visualiserBuildScale = (svg, width, height, xValues, yValues, tokens) => {
// Build X scales and axis:
const xScale = d3.scaleBand().range([0, width]).domain(xValues);
svg
.append('g')
.style('font-size', 'clamp(8px, 1.2vw, 17px)')
.call(
d3
.axisTop(xScale)
.tickSize(0)
.tickFormat((x) => tokens[x])
)
.selectAll('text')
.attr('font-family', 'Arial')
.attr('transform', 'rotate(45)')
.style('text-anchor', 'end');
svg.select('.domain').remove();
// Build Y scales and axis:
const yScale = d3.scaleBand().range([0, height]).domain(yValues);
svg
.append('g')
.style('font-size', 'clamp(8px, 1.2vw, 17px)')
.call(
d3
.axisLeft(yScale)
.tickSize(0)
.tickFormat((y) => tokens[y])
)
.selectAll('text')
.attr('font-family', 'Arial');
svg.select('.domain').remove();
// Build color scale
const maximum = 1;
const colorScale = d3
.scaleSequential()
.interpolator(d3.interpolateYlGnBu)
.domain([0, maximum]);
return [xScale, yScale, colorScale];
};
const visualiserCreateTooltip = () => {
// create a tooltip
const tooltip = d3
.select('visualiserID')
.append('div')
.style('opacity', 0)
.attr('class', 'tooltip')
.style('background-color', 'white')
.style('border', 'solid')
.style('border-width', '2px')
.style('border-radius', '5px')
.style('padding', '5px');
// Three function that change the tooltip when user hover / move / leave a cell
const mouseover = (event, d) => {
tooltip.style('opacity', 1);
d3.select(this).style('stroke', 'black').style('opacity', 1);
};
const mousemove = (event, d) => {
tooltip
.html('The exact value of<br>this cell is: ' + d.value)
.style('left', event.x / 2 + 'px')
.style('top', event.y / 2 + 'px');
};
const mouseleave = (event, d) => {
tooltip.style('opacity', 0);
d3.select(this).style('stroke', 'none').style('opacity', 0.8);
};
return [mouseover, mousemove, mouseleave];
};
/**
* @param {{layerFrom: number, layerTo: number, attention: number}} data
*/
const visualiserVisualise = (
svg,
data,
xScale,
yScale,
colorScale,
mouseover,
mousemove,
mouseleave
) => {
svg
.selectAll()
.data(data, (d) => d.layerFrom + ':' + d.layerTo)
.join('rect')
.attr('x', (d) => xScale(d.layerTo))
.attr('y', (d) => yScale(d.layerFrom))
.attr('width', xScale.bandwidth())
.attr('height', yScale.bandwidth())
.style('fill', (d) => colorScale(d.attention))
.style('stroke-width', 4)
.style('stroke', 'none')
.style('opacity', 0.8);
// .on('mouseover', mouseover)
// .on('mousemove', mousemove)
// .on('mouseleave', mouseleave);
};