-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd3.matrix.js
215 lines (183 loc) · 6.12 KB
/
d3.matrix.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
function Matrix(options) {
var margin = {top: 50, right: 50, bottom: 100, left: 100},
width = options.width,
height = options.height,
tooltip_canvas_selector = options.tooltip_canvas_selector,
tooltip_constructer = options.tooltip_constructer,
data = options.data,
container = options.container,
labelsData = options.labels,
startColor = options.start_color,
endColor = options.end_color,
widthLegend = options.widthLegend
if(!data){
throw new Error('Please pass data');
}
if(!Array.isArray(data) || !data.length || !Array.isArray(data[0])){
throw new Error('It should be a 2-D array');
}
var maxValue = d3.max(data, function(layer) { return d3.max(layer, function(d) { return d; }); });
var minValue = d3.min(data, function(layer) { return d3.min(layer, function(d) { return d; }); });
var numrows = data.length;
var numcols = data[0].length;
var svg = d3.select(container).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 + ")");
var background = svg.append("rect")
.style("stroke", "black")
.style("stroke-width", "2px")
.attr("width", width)
.attr("height", height);
var x = d3.scale.ordinal()
.domain(d3.range(numcols))
.rangeBands([0, width]);
var y = d3.scale.ordinal()
.domain(d3.range(numrows))
.rangeBands([0, height]);
var colorMap = d3.scale.linear()
.domain([minValue,maxValue])
.range([startColor, endColor]);
var row = svg.selectAll(".row")
.data(data)
.enter().append("g")
.attr("class", "row")
.attr("transform", function(d, i) { return "translate(0," + y(i) + ")"; });
var cell = row.selectAll(".cell")
.data(function(d) { return d; })
.enter().append("g")
.attr("class", "cell")
.attr("transform", function(d, i) { return "translate(" + x(i) + ", 0)"; });
var tooltip = d3.select(tooltip_canvas_selector).append("div").attr("class", "tooltip")
d3.selectAll(".cell").append('rect')
.attr("width", x.rangeBand())
.attr("height", y.rangeBand())
.style("stroke-width", 0)
.on("mouseover", function(d, i) {
tooltip
.style("visibility", "visible")
.html(tooltip_constructer(labelsData, d, i))
})
.on("mousemove", function(d, i) {
var xPos = d3.event.pageX - 20
var yPos = d3.event.pageY + 10
tooltip
.style("left", xPos + "px")
.style("top", yPos + "px")
})
.on("mouseout", function(d, i) {
tooltip.style("visibility", "hidden")
})
row.selectAll(".cell")
.data(function(d, i) { return data[i]; })
.style("fill", colorMap);
var labels = svg.append('g')
.attr('class', "labels");
var columnLabels = labels.selectAll(".column-label")
.data(labelsData)
.enter().append("g")
.attr("class", "column-label")
.attr("transform", function(d, i) { return "translate(" + x(i) + "," + height + ")"; });
columnLabels.append("line")
.style("stroke", "black")
.style("stroke-width", "1px")
.attr("x1", x.rangeBand() / 2)
.attr("x2", x.rangeBand() / 2)
.attr("y1", 0)
.attr("y2", 5);
columnLabels.append("text")
.attr("x", 0)
.attr("y", y.rangeBand() / 2)
.attr("dy", ".82em")
.style("font-size", function(d){ return calculate_legend_font_size() })
.attr("text-anchor", "end")
.attr("transform", "rotate(-60)")
.text(function(d, i) { return d; });
var rowLabels = labels.selectAll(".row-label")
.data(labelsData)
.enter().append("g")
.attr("class", "row-label")
.attr("transform", function(d, i) { return "translate(" + 0 + "," + y(i) + ")"; });
rowLabels.append("line")
.style("stroke", "black")
.style("stroke-width", "1px")
.attr("x1", 0)
.attr("x2", -5)
.attr("y1", y.rangeBand() / 2)
.attr("y2", y.rangeBand() / 2);
function calculate_legend_font_size(){
return 0.8 * width / data.length + "px"
}
rowLabels.append("text")
.attr("x", -8)
.attr("y", y.rangeBand() / 2)
.attr("dy", ".32em")
.attr("text-anchor", "end")
.style("font-size", function(d){ return calculate_legend_font_size() })
.text(function(d, i) { return d; });
var key = d3.select("#legend")
.append("svg")
.attr("width", widthLegend)
.attr("height", height + margin.top + margin.bottom);
var legend = key
.append("defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "100%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad");
legend
.append("stop")
.attr("offset", "0%")
.attr("stop-color", endColor)
.attr("stop-opacity", 1);
legend
.append("stop")
.attr("offset", "100%")
.attr("stop-color", startColor)
.attr("stop-opacity", 1);
key.append("rect")
.attr("width", widthLegend/2-10)
.attr("height", height)
.style("fill", "url(#gradient)")
.attr("transform", "translate(0," + margin.top + ")");
var y = d3.scale.linear()
.range([height, 0])
.domain([minValue, maxValue]);
var yAxis = d3.svg.axis()
.scale(y)
.orient("right");
key.append("g")
.attr("class", "y axis")
.attr("transform", "translate(41," + margin.top + ")")
.call(yAxis)
}
function masking(labels, mask_color){
d3.selectAll('.cell rect')
.attr('fill', function(d, i){
return is_mask(i, labels.length) ? mask_color : null
})
d3.selectAll('.cell text')
.style('fill', function(d, i){
return is_mask(i, labels.length) ? mask_color : d
})
}
function is_mask(i, num_rows){
var row_num = i % num_rows
var column_num = parseInt(i / num_rows)
return row_num >= column_num
}
function modify_diagonal_data(matrix){
var averages_per_column = matrix.map(function(column_data, i){
return column_data.reduce((a,b) => a + b, 0) / column_data.length
})
var average_of_matrix = parseInt(averages_per_column.reduce((a,b) => a + b, 0) / averages_per_column.length)
return matrix.map(function(column_data, i){
return column_data.map(function(cell, j){
return i === j ? average_of_matrix : cell
})
})
}