-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.html
139 lines (115 loc) · 3.34 KB
/
slider.html
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
<!DOCTYPE html>
<meta charset="utf-8" />
<!-- scales website to serve all device sizes -->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- keywords for browswer search results -->
<meta name="keywords" content="rockfish, sio, scripps" />
<title>Rockfish</title>
<!-- d3.js -->
<script src="d3/d3.min.js"></script>
<!-- slider script -->
<script src="https://unpkg.com/d3-simple-slider"></script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous" />
<div class="container">
<h2 class="pt-5">Rockfish population</h2>
<h4 class="pt-5">Year</h2>
<div class="row align-items-center">
<div class="col-sm-2">
<p id="value-new-york-times"></p>
</div>
<div class="col-sm">
<div id="slider-new-york-times"></div>
</div>
</div>
</div>
<script>
// Simple
var data = [0, 0.005, 0.01, 0.015, 0.02, 0.025];
// New York Times
var width = 565;
var height = 120;
var margin = {
top: 20,
right: 50,
bottom: 50,
left: 40
};
var dataNewYorkTimes = d3.range(1, 41).map(d => ({
year: d,
value: 10000 * Math.exp(-(d - 1) / 40),
}));
var svg = d3
.select('div#slider-new-york-times')
.append('svg')
.attr('width', width)
.attr('height', height);
var padding = 0.1;
var xBand = d3
.scaleBand()
.domain(dataNewYorkTimes.map(d => d.year))
.range([margin.left, width - margin.right])
.padding(padding);
var xLinear = d3
.scaleLinear()
.domain([
d3.min(dataNewYorkTimes, d => d.year),
d3.max(dataNewYorkTimes, d => d.year),
])
.range([
margin.left + xBand.bandwidth() / 2 + xBand.step() * padding - 0.5,
width -
margin.right -
xBand.bandwidth() / 2 -
xBand.step() * padding -
0.5,
]);
var y = d3
.scaleLinear()
.domain([0, d3.max(dataNewYorkTimes, d => d.value)])
.nice()
.range([height - margin.bottom, margin.top]);
var yAxis = g =>
g
.attr('transform', `translate(${width - margin.right},0)`)
.call(
d3
.axisRight(y)
.tickValues([1e4])
.tickFormat(d3.format('($.2s'))
)
.call(g => g.select('.domain').remove());
var slider = g =>
g.attr('transform', `translate(0,${height - margin.bottom})`).call(
d3
.sliderBottom(xLinear)
.step(1)
.ticks(4)
.default(9)
.on('onchange', value => draw(value))
);
var bars = svg
.append('g')
.selectAll('rect')
.data(dataNewYorkTimes);
var barsEnter = bars
.enter()
.append('rect')
.attr('x', d => xBand(d.year))
.attr('y', d => y(d.value))
.attr('height', d => y(0) - y(d.value))
.attr('width', xBand.bandwidth());
svg.append('g').call(yAxis);
svg.append('g').call(slider);
svg.select('.track-overlay').attr('stroke-width', 120); // Ensure drag zone covers everything
var draw = selected => {
barsEnter
.merge(bars)
.attr('fill', d => (d.year === selected ? '#bad80a' : '#e0e0e0'));
d3.select('p#value-new-york-times').text(
d3.format('$,.2r')(dataNewYorkTimes[selected - 1].value)
);
};
draw(9);
d3.select('p#value-color-picker').text(`#${num2hex(rgb)}`);
</script>