forked from vcccaat/visualization-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegend.js
61 lines (56 loc) · 2.02 KB
/
legend.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
function drawLegend(countArr, legend, legendColorScale) {
const legendWidth = legend.attr('width');
const legendHeight = legend.attr('height');
const legendMinMax = d3.extent(legendColorScale.domain());
const barHeight = 25;
const stepSize = 4;
// Extend the minmax by 1 in either direction to expose more features
// create pixel and bar scale
const pixelScale = d3
.scaleLinear()
.domain([0, legendWidth - 40])
.range([legendMinMax[0] - 1, legendMinMax[1] + 1]);
const barScale = d3
.scaleLinear()
.domain([legendMinMax[0] - 1, legendMinMax[1] + 1])
.range([0, legendWidth - 40]);
const barAxis = d3.axisBottom(barScale);
// Check if we're using a quantile scale
if (legendColorScale.hasOwnProperty('quantiles')) {
// Use the quantile breakpoints plus the min and max of the scale as tick values
barAxis.tickValues(legendColorScale.quantiles().concat(legendMinMax));
}
legend
.append('g')
.attr('class', 'colorbar axis')
.attr('transform', 'translate(' + 20 + ',' + (barHeight + 5) + ')')
.call(barAxis);
// Draw rects of color down the bar
let bar = legend.append('g').attr('transform', 'translate(' + 20 + ',' + 0 + ')');
for (let i = 0; i < legendWidth - 40; i = i + stepSize) {
bar
.append('rect')
.attr('x', i)
.attr('y', 0)
.attr('width', stepSize)
.attr('height', barHeight)
.style('fill', legendColorScale(pixelScale(i))); // pixels => countData => color
}
// Put lines in to mark actual min and max of our data
// bar
// .append('line')
// .attr('stroke', 'white')
// .attr('stroke-width', 3)
// .attr('x1', barScale(legendMinMax[0]))
// .attr('x2', barScale(legendMinMax[0]))
// .attr('y1', 0)
// .attr('y1', barHeight + 4).
// bar
// .append('line')
// .attr('stroke', 'white')
// .attr('stroke-width', 3)
// .attr('x1', barScale(legendMinMax[1]))
// .attr('x2', barScale(legendMinMax[1]))
// .attr('y1', 0)
// .attr('y1', barHeight + 4);
}