9
9
< link rel ="stylesheet " href ="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css " integrity ="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T " crossorigin ="anonymous ">
10
10
< link href ="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/css/select2.min.css " rel ="stylesheet " />
11
11
< style >
12
+ body {
13
+ font-size : 16px ;
14
+ }
12
15
.c3-line {
13
16
stroke-width : 2px ;
14
17
}
18
+ # raceW span {
19
+ position : absolute;
20
+ bottom : 1.5em ;
21
+ right : 2em ;
22
+ font-size : 3em ;
23
+ font-weight : bolder;
24
+ color : lightslategray;
25
+ }
15
26
</ style >
16
27
</ head >
17
28
< body class ="container-fluid ">
@@ -70,8 +81,7 @@ <h3 id="instruction">Jak używać</h3>
70
81
<!-- <div id="namesNew"></div> -->
71
82
< br />
72
83
< h3 > Ranking najpopularniejszych imion wg kolejnych lat</ h3 >
73
- < div id ="raceW ">
74
- < span id ="year "> </ span >
84
+ < div id ="raceW " class ="position-relative ">
75
85
</ div >
76
86
77
87
</ div >
@@ -204,7 +214,7 @@ <h3>Ranking najpopularniejszych imion wg kolejnych lat</h3>
204
214
} )
205
215
206
216
d3 . csv ( 'https://raw.githubusercontent.com/codestance/babyNames/dev/raceW.csv' ) . then ( function ( dat ) {
207
- // console.log(dat)
217
+
208
218
function filterData ( year ) {
209
219
let filtered = dat . filter ( obj => + obj [ year ] !== 0 )
210
220
filtered . sort ( ( a , b ) => b [ year ] - a [ year ] ) ;
@@ -216,7 +226,6 @@ <h3>Ranking najpopularniejszych imion wg kolejnych lat</h3>
216
226
names . unshift ( 'x' )
217
227
return names
218
228
}
219
- // console.log('filtered',filterData(2000))
220
229
221
230
function valuesInYear ( year ) {
222
231
let datValues = filterData ( year ) . map ( d => + d [ year ] )
@@ -225,47 +234,64 @@ <h3>Ranking najpopularniejszych imion wg kolejnych lat</h3>
225
234
}
226
235
227
236
let raceChart = c3 . generate ( {
228
- bindto : '#raceW' ,
229
- data : {
230
- x : 'x' ,
231
- xFormat : '%Y' ,
232
- columns : [
233
- namesInYear ( 2000 ) ,
234
- valuesInYear ( 2000 )
235
- ] ,
236
- type : 'bar'
237
- } ,
238
- order : 'desc' ,
239
- axis : {
240
- rotated : true ,
241
- x : {
242
- type : 'category' ,
237
+ bindto : '#raceW' ,
238
+ data : {
239
+ x : 'x' ,
240
+ xFormat : '%Y' ,
241
+ columns : [
242
+ namesInYear ( 2000 ) ,
243
+ valuesInYear ( 2000 )
244
+ ] ,
245
+ names : {
246
+ value : 'liczba'
247
+ } ,
248
+ type : 'bar'
249
+ } ,
250
+ order : 'desc' ,
251
+ axis : {
252
+ rotated : true ,
253
+ x : {
254
+ type : 'category' ,
255
+ }
256
+ } ,
257
+ legend : {
258
+ show : false
259
+ }
260
+ } ) ;
261
+ const raceChartDiv = document . getElementById ( 'raceW' )
262
+ const span = document . createElement ( 'span' )
263
+ raceChartDiv . appendChild ( span )
264
+ index = 0
265
+ span . innerText = years [ index ]
266
+ const io = new IntersectionObserver ( ( entry , observer ) => {
267
+ entry = entry [ 0 ]
268
+ let animation
269
+ if ( entry . intersectionRatio > 0 ) {
270
+ animation =
271
+ setInterval ( ( ) => {
272
+ index ++
273
+ if ( index < years . length ) {
274
+ raceChart . load ( {
275
+ columns : [
276
+ namesInYear ( years [ index ] ) ,
277
+ valuesInYear ( years [ index ] )
278
+ ]
279
+ } )
280
+ span . innerText = years [ index ]
281
+ }
282
+ else {
283
+ index = 0
284
+ }
285
+ } , 4000 )
286
+
287
+ } else {
288
+ clearInterval ( animation )
243
289
}
244
- }
245
- } ) ;
246
-
247
- setTimeout ( function ( ) {
248
- raceChart . load ( {
249
- columns : [
250
- namesInYear ( 2001 ) ,
251
- valuesInYear ( 2001 )
252
- ] ,
253
290
} )
254
- } , 5000 )
291
+ io . observe ( raceChartDiv )
255
292
256
293
} )
257
294
258
-
259
-
260
-
261
- // setTimeout(function () {
262
- // race.load({
263
- // columns: [
264
- // ['value', 11000, 250, 200]
265
- // ]
266
- // });
267
- // }, 10000);
268
-
269
295
</ script >
270
296
</ body >
271
297
< footer >
0 commit comments