-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathindex.html
More file actions
330 lines (266 loc) · 8.04 KB
/
index.html
File metadata and controls
330 lines (266 loc) · 8.04 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
body{
max-width: 900px;
margin: 0px auto;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
h1{
margin: 50px;
}
h1{
text-align: center;
}
#container{
position: relative;
}
#sections{
width: 340px;
}
#sections > div{
background: white;
opacity: .2;
margin-bottom: 200px;
}
#sections > div:last-child{
margin-bottom: 30px;
}
#sections > div.graph-scroll-active{
opacity: 1;
}
#graph{
margin-left: 40px;
width: 500px;
position: -webkit-sticky;
position: sticky;
top: 0px;
float: right;
}
@media (max-width: 925px) {
#graph{
width: 100%;
margin-left: 0px;
float: none;
}
#sections{
width: auto;
position: relative;
margin: 0px auto;
}
#sections > div{
background: rgba(255,255,255,.5);
padding: 10px;
border-top: 1px solid;
border-bottom: 1px solid;
margin-bottom: 80vh;
}
pre{
overflow: hidden;
}
h1{
margin: 10px;
}
}
circle{
fill: steelblue;
}
.mono{
font-family: monospace;
}
svg{
background-color: #eee;
}
</style>
<h1><a href='https://github.com/1wheel/graph-scroll'>graph-scroll.js</a></h1>
<h1>Simple scrolling events for d3 graphics</h1>
<div id='container' class='container-1'>
<div id='graph'></div>
<div id='sections'>
<div>
<h3>Connect text and graphics</h3>
<span class='mono'>graph-scroll</span>
takes a selection of explanatory text sections and dispatches <span class='mono'>active</span> events as different sections are scrolled into to view. These active events are used to update a graph's state.
<pre>
d3.graphScroll()
.sections(d3.selectAll('#sections > div'))
.on('active', function(i){
console.log(i + 'th section active') })
</pre>
</div>
<div>
<h3>Highlight active text</h3>
The top most text section scrolled into view is classed <span class='mono'>graph-scroll-active</span>. This makes it easy to highlight the active section with css:
<pre>
#sections > div{
opacity: .3
}
#sections div.graph-scroll-active{
opacity: 1;
}
</pre>
</div>
<div>
<h3>Headers and footers</h3>
To support headers and intro images/text, the explanatory text and graphic are wrapped with a container element:
<pre>
<h1>Page Title</div>
<div id='container'>
<div id='graph'></div>
<div id='sections'>
<div>Section 0</div>
<div>Section 1</div>
<div>Section 2</div>
</div>
</div>
<h1>Footer</h1>
</pre>
and passed to <span class='mono'>graphScroll</span>
<pre>
d3.graphScroll()
.graph(d3.select('#graph'))
.container(d3.select('#container'))
<pre>
</div>
<div>
<h3>Sticky graph</h3>
When the graph starts to scroll out of view, <span class='mono'>postiion: sticky</span> keeps the graph element stuck to the top of the page while the text scrolls by.
<pre>
#container{
position: relative;
}
#sections{
width: 340px;
}
#graph{
margin-left: 40px;
width: 500px;
position: sticky;
top: 0px;
float: right;
}
</pre>
</div>
</div>
</div>
<h1>
Multiple graphs work too!
</h1>
<div id='container' class='container-2'>
<div id='graph'></div>
<div id='sections'>
<div>
<h3>Mobile</h3>
On mobile centering the graph and sections while adding a some padding for the first slide is a good option:
<pre>
@media (max-width: 925px) {
#graph{
width: 100%;
transform: translate(-50%, 0);
margin-left: 50%;
}
#sections{
position: relative;
margin: 0px auto;
padding-top: 400px;
}
}
</pre>
</div>
<div>
<h3>Examples</h3>
<p><a href='http://www.bloomberg.com/graphics/2015-auto-sales/'>Auto Sales</a></p>
<p><a href='http://www.bloomberg.com/graphics/2015-measles-outbreaks/'>Measles</a></p>
<p><a href='http://roadtolarissa.com/blog/2015/01/04/coloring-maps-with-d3/'>Coloring Maps</a></p>
<!--<p><a href='http://www.bloomberg.com/graphics/2015-fifa-scandal/'>FIFA Scandal</a></p>-->
<p><a href='http://www.bloomberg.com/graphics/2015-whats-warming-the-world/'>Global Warming</a></p>
<p><a href='http://www.bloomberg.com/politics/graphics/2015-hillary-womens-movement-timeline/'>Hillary Clinton’s Debt to Feminism
</a></p>
<p><a href='http://www.bloomberg.com/graphics/year-ahead-2016/'>The Year Ahead</a></p>
<p><a href='http://www.bloomberg.com/graphics/2015-pace-of-social-change/'>Pace of Social Change</a></p>
<p><a href='http://www.bloomberg.com/graphics/2015-red-meat-cancer/'>Red Meat</a></p>
</div>
<div>
<h3>More reading</h3>
<p><a href='http://bost.ocks.org/mike/scroll/'>How To Scroll</a></p>
<p><a href='http://vallandingham.me/scroller.html/'>So You Want to Build A Scroller</a></p>
<p><a href='https://source.opennews.org/en-US/articles/behind-scenes-fewer-helmets-more-deaths/'>Making “Fewer Helmets, More Deaths”</a></p>
<p><a href='http://www.nytimes.com/interactive/2015/10/27/world/greenland-is-melting-away.html'>Greenland Is Melting Away</a></p>
<p><a href='http://www.theguardian.com/us-news/ng-interactive/2015/oct/19/homan-square-chicago-police-detainees'>Homan Square</a></p>
</div>
<div>
<h3>Todos</h3>
<p>- Simple examples
<p>- Self explanatory graphics
<p>- Swiper
</div>
</div>
</div>
<h1 id='source'><a href='https://github.com/1wheel/graph-scroll'>contribute/view source on github</a></h1>
<script src="d3v4+jetpack.js"></script>
<script src="graph-scroll.js"></script>
<script>
var oldWidth = 0
function render(){
if (oldWidth == innerWidth) return
oldWidth = innerWidth
var width = height = d3.select('#graph').node().offsetWidth
var r = 40
if (innerWidth <= 925){
width = innerWidth
height = innerHeight*.7
}
// return console.log(width, height)
var svg = d3.select('#graph').html('')
.append('svg')
.attrs({width: width, height: height})
var circle = svg.append('circle')
.attrs({cx: 0, cy: 0, r: r})
var colors = ['orange', 'purple', 'steelblue', 'pink', 'black']
var gs = d3.graphScroll()
.container(d3.select('.container-1'))
.graph(d3.selectAll('container-1 #graph'))
.eventId('uniqueId1') // namespace for scroll and resize events
.sections(d3.selectAll('.container-1 #sections > div'))
// .offset(innerWidth < 900 ? innerHeight - 30 : 200)
.on('active', function(i){
var pos = [ {cx: width - r, cy: r},
{cx: r, cy: r},
{cx: width - r, cy: height - r},
{cx: width/2, cy: height/2} ][i]
circle.transition().duration(1000)
.attrs(pos)
.transition()
.style('fill', colors[i])
})
var svg2 = d3.select('.container-2 #graph').html('')
.append('svg')
.attrs({width: width, height: height})
var path = svg2.append('path')
var gs2 = d3.graphScroll()
.container(d3.select('.container-2'))
.graph(d3.selectAll('.container-2 #graph'))
.eventId('uniqueId2') // namespace for scroll and resize events
.sections(d3.selectAll('.container-2 #sections > div'))
.on('active', function(i){
var h = height
var w = width
var dArray = [
[[w/4, h/4], [w*3/4, h/4], [w*3/4, h*3/4], [w/4, h*3/4]],
[[0, 0], [w*3/4, h/4], [w*3/4, h*3/4], [w/4, h*3/4]],
[[w/2, h/2], [w, h/4], [w, h], [w/4, h]],
[[w/2, h/2], [w, h/4], [w, h], [w/4, h]],
[[w/2, h/2], [w, h/2], [0, 0], [w/4, h/2]],
[[w/2, h/2], [0, h/4], [0, h/2], [w/4, 0]],
].map(function(d){ return 'M' + d.join(' L ') })
path.transition().duration(1000)
.attr('d', dArray[i])
.style('fill', colors[i])
})
d3.select('#source')
.styles({'margin-bottom': window.innerHeight - 450 + 'px', padding: '100px'})
}
render()
d3.select(window).on('resize', render)
</script>