-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
909 lines (767 loc) · 27.5 KB
/
index.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Final_NAC</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.css' rel='stylesheet' />
<script src="https://unpkg.com/[email protected]/intersection-observer.js"></script>
<script src="https://unpkg.com/scrollama"></script>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Load color scale -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<style>
body {
margin:0;
padding:0;
font-family: sans-serif;
}
a, a:hover, a:visited {
color: '#0071bc';
}
#map {
top:0;
height: 100vh;
width:100vw;
position: fixed;
z-index: -5;
}
#header {
margin: 3vh auto;
width: 90vw;
padding: 2vh;
text-align: center;
}
#footer {
width: 100%;
min-height: 5vh;
padding-top: 2vh;
padding-bottom: 2vh;
text-align: center;
line-height: 25px;
font-size: 13px;
}
#features {
padding-top: 10vh;
padding-bottom: 10vh;
z-index: 100;
}
/*style of d3 part*/
#my_dataviz {
width: 100%;
min-height: 5vh;
padding-top: 2vh;
padding-bottom: 2vh;
text-align: center;
line-height: 25px;
font-size: 13px;
background-color: white;
}
#buttons1 {
width: 100%;
min-height: 5vh;
padding-top: 2vh;
padding-bottom: 2vh;
text-align: center;
line-height: 25px;
font-size: 13px;
background-color: white;
}
#buttons2 {
width: 100%;
min-height: 5vh;
padding-top: 2vh;
padding-bottom: 2vh;
text-align: center;
line-height: 25px;
font-size: 13px;
background-color: white;
}
.path {
stroke: #fff;
stroke-width: .5;
stroke-dasharray: 1;
fill: #afafaf;
}
#tooltip.hidden{
display: none;
}
#tooltip {
position: absolute;
text-align: center;
padding: 2px;
font: 12px sans-serif;
background: #fff;
border: 0px;
border-radius: 8px;
pointer-events: none;
opacity: 0.6;
}
/* END of style of d3 part*/
.centered {
width: 50vw;
margin: 0 auto;
}
.lefty {
width: 33vw;
margin-left: 5vw;
}
.righty {
width: 33vw;
margin-left: 62vw;
}
.light {
color: #444;
background-color: #fafafa;
}
.dark {
color: #fafafa;
background-color: #444;
}
.step {
padding-bottom: 50vh;
/* margin-bottom: 10vh; */
opacity: 0.25;
}
.step.active {
opacity: 0.9;
}
.step div {
padding: 25px 50px;
line-height: 25px;
font-size: 13px;
}
.step img {
width: 100%;
}
@media (max-width: 750px) {
#features {
width: 90vw;
margin: 0 auto;
}
}
/* Fix issue on mobile browser where scroll breaks */
.mapboxgl-canvas-container.mapboxgl-touch-zoom-rotate.mapboxgl-touch-drag-pan,
.mapboxgl-canvas-container.mapboxgl-touch-zoom-rotate.mapboxgl-touch-drag-pan .mapboxgl-canvas {
touch-action: unset;
}
/* Style for D3 part */
.bubbles {
stroke-width: 2px;
stroke: white;
}
.bubbles:hover {
stroke: black;
}
.text-anchor {
font-family : Arial;
font-size: 8px;
}
button {
display: inline-block;
height: 80px;
padding: 0;
margin: 0;
vertical-align: top;
width: 80px;
background-color: white;
border: 2px solid #fff;
}
.column_left{
float: left;
width: 25%;
text-align: center;
}
.column_center{
float: left;
width: 50%;
text-align: center;
}
.column_right{
float: right;
width: 25%;
text-align: left;
}
#intro{
width: 100%-10vh;
min-height: 5vh;
padding-top: 5vh;
padding-bottom: 5vh;
padding-right: 5vh;
padding-left: 5vh;
text-align: left;
line-height: 25px;
font-size: 16px;
background-color: white;
}
#footer{
width: 100%-10vh;
min-height: 5vh;
padding-top: 5vh;
padding-bottom: 5vh;
padding-right: 5vh;
padding-left: 5vh;
text-align: left;
line-height: 25px;
font-size: 16px;
background-color: white;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="story"></div>
<!-- Divs for D3 interactive part -->
<div id="intro">
<p><b>Want to learn more? Play with the bubble chart and map below.</b></p>
<p>This tool can be used to identify communities and indicators that should be prioritized to overcome this crisis without leaving anyone behind.</p>
<p>By clicking on the <b><span style="color:#fdd58b;">Eat</span>, <span style="color:#80a592;">Play</span>, <span style="color:#90a5b6;">Learn</span></b> icons, you can select which data to show in the bubble chart. You can also set the different races, and the color saturation will change according to the percentage of people of that race in each census tract.</p>
<p>If you hover over the bubbles or the map, census information will be shown on the top right. The neighborhood that you are selecting will be highlighted and will also allow you to explore disparities within the neighborhoods.</p>
<p>Finally, if you want to see the data for just one borough, you can hover over each borough's names in the legend.</p>
</div>
<div class = "row">
<div class="column_left">
<p><b> Show results</b></p>
<button id="Eat"><img src="img/icons-01.svg"> </button>
<button id="Play"><img src="img/icons-02.svg"></button>
<button id="Learn"><img src="img/icons-03.svg"></button>
</div>
<div class="column_center">
<p><b> Race (Opacity shows the relative % of the race in each census tract) </b></p>
<button id="White">White</button>
<button id="Black">Black</button>
<button id="Asian">Asian</button>
<button id="Other">Other</button>
<button id="Twoormore">Two or more</button>
<button id="Native">Native</button>
<button id="Islander">Islander</button>
<button id="Reset">Reset</button>
</div>
<div id = "tt" class="column_right">
</div>
</div>
<div id="my_dataviz"></div>
<div id="footer">
<p>Learn more about the data sources and methods <b> <a href="about.html">here </a> </b></p>
</div>
<script src="./config.js"></script>
<script>
var layerTypes = {
'fill': ['fill-opacity'],
'line': ['line-opacity'],
'circle': ['circle-opacity', 'circle-stroke-opacity'],
'symbol': ['icon-opacity', 'text-opacity'],
'raster': ['raster-opacity'],
'fill-extrusion': ['fill-extrusion-opacity']
}
var alignments = {
'left': 'lefty',
'center': 'centered',
'right': 'righty'
}
function getLayerPaintType(layer) {
var layerType = map.getLayer(layer).type;
return layerTypes[layerType];
}
function setLayerOpacity(layer) {
var paintProps = getLayerPaintType(layer.layer);
paintProps.forEach(function(prop) {
map.setPaintProperty(layer.layer, prop, layer.opacity);
});
}
var story = document.getElementById('story');
var features = document.createElement('div');
features.classList.add(alignments[config.alignment]);
features.setAttribute('id', 'features');
var header = document.createElement('div');
if (config.title) {
var titleText = document.createElement('h1');
titleText.innerText = config.title;
header.appendChild(titleText);
}
if (config.subtitle) {
var subtitleText = document.createElement('h2');
subtitleText.innerText = config.subtitle;
header.appendChild(subtitleText);
}
if (config.byline) {
var bylineText = document.createElement('p');
bylineText.innerText = config.byline;
header.appendChild(bylineText);
}
if (header.innerText.length > 0) {
header.classList.add(config.theme);
header.setAttribute('id', 'header');
story.appendChild(header);
}
config.chapters.forEach((record, idx) => {
var container = document.createElement('div');
var chapter = document.createElement('div');
if (record.title) {
var title = document.createElement('h3');
title.innerText = record.title;
chapter.appendChild(title);
}
if (record.image) {
var image = new Image();
image.src = record.image;
chapter.appendChild(image);
}
if (record.description) {
var story = document.createElement('p');
story.innerHTML = record.description;
chapter.appendChild(story);
}
container.setAttribute('id', record.id);
container.classList.add('step');
if (idx === 0) {
container.classList.add('active');
}
chapter.classList.add(config.theme);
container.appendChild(chapter);
features.appendChild(container);
});
story.appendChild(features);
var footer = document.createElement('div');
if (config.footer) {
var footerText = document.createElement('p');
footerText.innerHTML = config.footer;
footer.appendChild(footerText);
}
if (footer.innerText.length > 0) {
footer.classList.add(config.theme);
footer.setAttribute('id', 'footer');
story.appendChild(footer);
}
mapboxgl.accessToken = config.accessToken;
const transformRequest = (url) => {
const hasQuery = url.indexOf("?") !== -1;
const suffix = hasQuery ? "&pluginName=journalismScrollytelling" : "?pluginName=journalismScrollytelling";
return {
url: url + suffix
}
}
var map = new mapboxgl.Map({
container: 'map',
style: config.style,
center: config.chapters[0].location.center,
zoom: config.chapters[0].location.zoom,
bearing: config.chapters[0].location.bearing,
pitch: config.chapters[0].location.pitch,
scrollZoom: false,
transformRequest: transformRequest
});
var marker = new mapboxgl.Marker();
if (config.showMarkers) {
marker.setLngLat(config.chapters[0].location.center).addTo(map);
}
// instantiate the scrollama
var scroller = scrollama();
map.on("load", function() {
// setup the instance, pass callback functions
scroller
.setup({
step: '.step',
offset: 0.5,
progress: true
})
.onStepEnter(response => {
var chapter = config.chapters.find(chap => chap.id === response.element.id);
response.element.classList.add('active');
map.flyTo(chapter.location);
if (config.showMarkers) {
marker.setLngLat(chapter.location.center);
}
if (chapter.onChapterEnter.length > 0) {
chapter.onChapterEnter.forEach(setLayerOpacity);
}
})
.onStepExit(response => {
var chapter = config.chapters.find(chap => chap.id === response.element.id);
response.element.classList.remove('active');
if (chapter.onChapterExit.length > 0) {
chapter.onChapterExit.forEach(setLayerOpacity);
}
});
});
// setup resize event
window.addEventListener('resize', scroller.resize);
</script>
<script>
// set the dimensions and margins of the graph
var margin = {top: 10, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.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 + ")");
// X axis
var x = d3.scaleLinear()
.domain([0, 1])
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height + margin.top + 20) + ")")
.style("text-anchor", "middle")
.text("Social Vulnerability Index");
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 100])
.range([ height, 0]);
svg.append("text")
.attr("class", "ylabel")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Percentage of houshold receiving Snap");
svg.append("g")
.call(d3.axisLeft(y));
//Boroughs and Colors
var allgroups = ["Manhattan", "Bronx", "Brooklyn", "Queens", "Staten Island"]
var myColor = d3.scaleOrdinal()
.domain(allgroups)
.range(d3.schemeSet2);
// Add a scale for bubble size
var z = d3.scaleLinear()
.domain([0, 5000])
.range([ 4, 40]);
//Read the data
d3.json("https://raw.githubusercontent.com/NaroaCS/data/main/data-viz/combined_data.geojson", function(data) {
console.log(data)
// Add a scale for bubble color
svg.append('g')
.selectAll("dot")
.data(data.features)
.enter().append("circle")
.attr("class", function(d) { return "bubbles " + d.properties.boro_name.replace(" ", "_") +" "+ d.properties.ntacode})
.attr("cx", function (d) { return x(d.properties.Total_x); } )
.attr("cy", function (d) { return y(d.properties.snap_perc); } )
.attr("r", function (d) { return z(d.properties.Total_y*d.properties.PercU18/100); } )
.style("fill", function (d) { return myColor(d.properties.boro_name.replaceAll("\\s", "")); } )
.style("opacity", 0.75)
.on("mouseover", showTooltip )
.on("mousemove", moveTooltip )
.on("mouseleave", hideTooltip )
});
// Append a svg object to the body for map
var svg3 = d3.select("#my_dataviz")
.append("svg")
.attr("width", width)
.attr("height", height);
//Projection
var albersProjection = d3.geoAlbers()
.scale(60000)
.rotate([73.9787, 0])
.center([0, 40.68])
.translate([width/2, height/2])
var path = d3.geoPath()
.projection(albersProjection)
// Loading data and map generation
d3.json("https://raw.githubusercontent.com/NaroaCS/data/main/data-viz/home_results.json", function(data) {
svg3.selectAll("path")
.data(data.features)
.enter().append("path")
.attr("class", function(d) { return "mappath " + d.properties.ntacode})
.attr("d", path)
.style("fill", function(d) { return myColor(d.properties.boro_name.replaceAll("\\s", ""));}) // or d.properties.Perc_white
.style('stroke','lightgray')
.on("mouseover", showTooltip )
.on("mousemove", moveTooltip )
.on("mouseleave", hideTooltip )
});
// Update Chart after clicking the Eat, Learn, Play buttons
function updateChart(epl){
svg.selectAll(".bubbles")
.transition()
.duration(5000)
//Update data
.attr("cy", function (d) {
if(epl == "Eat") {return y(d.properties.snap_perc);}
else if(epl == "Play") {return y((Math.log(d.properties.budsum+1))*15);}
else if(epl == "Learn") {return y(100-d.properties.PercentageU18WithAccess);}});
//Update y axis
d3.select(".ylabel")
.text(function (d){
if(epl == "Eat") {return "Percentage of houshold receiving Snap";}
else if(epl == "Play") {return "Open space access index";}
else if(epl == "Learn") {return "Youth percentage without Internet access";}
});
}
// Update Race after clicking the race buttons
function updateRace(race){
// Updating Bubble Chart
svg.selectAll(".bubbles")
.transition()
.duration(1000)
//Update opacity of bubbles
.style("opacity",function(d){
if(race=='White'){
if (d.properties.Perc_white=='nan'){opacity=0}
else {opacity= d.properties.Perc_white/100};
return opacity;
}
else if(race=='Black'){
if (d.properties.Perc_black_afam=='nan'){opacity=0}
else {opacity= d.properties.Perc_black_afam/99.7};
return opacity;
}
else if(race=='Asian'){
if (d.properties.Perc_asian=='nan'){opacity=0}
else {opacity= d.properties.Perc_asian/85.2};
return opacity;
}
else if(race=='Islander'){
if (d.properties.Perc_hawaii_pi=='nan'){opacity=0}
else {opacity= d.properties.Perc_hawaii_pi/6.6};
//these divisions (such as 6.6 here) are the maximum in the data and is used to normalize it, otherwise for some races it would all be white
return opacity;
}
else if(race=='Native'){
if (d.properties.Perc_amindian_alnative=='nan'){opacity=0}
else {opacity= d.properties.Perc_amindian_alnative/28};
return opacity;
}
else if(race=='Twoormore'){
if (d.properties.Perc_two_or_more=='nan'){opacity=0}
else {opacity= d.properties.Perc_two_or_more/51.1};
return opacity;
}
else if(race=='Other'){
if (d.properties.Perc_other_race=='nan'){opacity=0}
else {opacity= d.properties.Perc_other_race/74.5};
return opacity;
}
else if(race=='reset'){return 0.75;}
else { return 0.75;}
})
// Updating Map
svg3.selectAll("path")
.transition()
.duration(1000)
//Update opacity of map census tracts
.style("opacity",function(d){
if(race=='White'){
if (d.properties.Perc_white=='nan'){opacity=0}
else {opacity= d.properties.Perc_white/100};
return opacity;
}
else if(race=='Black'){
if (d.properties.Perc_black_afam=='nan'){opacity=0}
else {opacity= d.properties.Perc_black_afam/99.7};
return opacity;
}
else if(race=='Asian'){
if (d.properties.Perc_asian=='nan'){opacity=0}
else {opacity= d.properties.Perc_asian/85.2};
return opacity;
}
else if(race=='Islander'){
if (d.properties.Perc_hawaii_pi=='nan'){opacity=0}
else {opacity= d.properties.Perc_hawaii_pi/6.6};
return opacity;
}
else if(race=='Native'){
if (d.properties.Perc_amindian_alnative=='nan'){opacity=0}
else {opacity= d.properties.Perc_amindian_alnative/28};
return opacity;
}
else if(race=='Twoormore'){
if (d.properties.Perc_two_or_more=='nan'){opacity=0}
else {opacity= d.properties.Perc_two_or_more/51.1};
return opacity;
}
else if(race=='Other'){
if (d.properties.Perc_other_race=='nan'){opacity=0}
else {opacity= d.properties.Perc_other_race/74.5};
return opacity;
}
else if(race=='reset'){
return 0.75;
}
});
}
// The scale you use for bubble size
var size = d3.scaleSqrt()
.domain([0, 5000]) // What's in the data, let's say it is percentage
.range([4, 40]) // Size in pixel
// Add legend: circles
var valuesToShow = [100, 1000, 5000]
var xCircle = 130
var xLabel = 200
var yCircle = 100
var svg2 = d3.select("#my_dataviz")
.append("svg")
.attr("width", width/2)
.attr("height", height)
svg2.append("text")
.attr('x', xCircle)
.attr('y', 14)
.text("Number of youth in each census tract")
.attr('font-size', '12px')
.attr('text-anchor', 'middle')
svg2.selectAll("legend")
.data(valuesToShow)
.enter()
.append("circle")
.attr("cx", xCircle)
.attr("cy", function(d){ return yCircle - size(d) } )
.attr("r", function(d){ return size(d) })
.style("fill", "none")
.attr("stroke", "black")
// Add legend: segments
svg2.selectAll("legend")
.data(valuesToShow)
.enter()
.append("line")
.attr('x1', function(d){ return xCircle + size(d) } )
.attr('x2', xLabel)
.attr('y1', function(d){ return yCircle - size(d) } )
.attr('y2', function(d){ return yCircle - size(d) } )
.attr('stroke', 'black')
.style('stroke-dasharray', ('2,2'))
// Add legend: labels
svg2.selectAll("legend")
.data(valuesToShow)
.enter()
.append("text")
.attr('x', xLabel)
.attr('y', function(d){ return yCircle - size(d) } )
.text( function(d){ return d } )
.style("font-size", 10)
.attr('alignment-baseline', 'middle')
// What to do when one group is hovered
var highlight = function(d){
d3.selectAll(".bubbles").style("opacity", .05)
d3.selectAll("."+d.replace(" ", "_")).style("opacity", 0.75);
}
// And when it is not hovered anymore
var noHighlight = function(d){
d3.selectAll(".bubbles").style("opacity", 0.75)
}
// Add one dot in the legend for each name.
var size = 20
svg2.selectAll("myrect")
.data(allgroups)
.enter()
.append("circle")
.attr("cx", xCircle-50)
.attr("cy", function(d,i){ return yCircle +50 + i*(size+5)}) // 100 is where the first dot appears. 25 is the distance between dots
.attr("r", 7)
.style("fill", function(d){ return myColor(d)})
.on("mouseover", highlight)
.on("mouseleave", noHighlight);
// Add labels beside legend dots
svg2.selectAll("mylabels")
.data(allgroups)
.enter()
.append("text")
.attr("x", xCircle-50 + size*.8)
.attr("y", function(d,i){ return yCircle+50 + i*(size+5)}) // 100 is where the first dot appears. 25 is the distance between dots
.style("fill", function(d){ return myColor(d)})
.text(function(d){ return d})
.attr("text-anchor", "left")
.style("alignment-baseline", "middle")
.on("mouseover", highlight)
.on("mouseleave", noHighlight);
//Calling update functions when an Eat, Learn, Play button in selected
d3.select("#Eat").on("click", function() {
console.log("Eat")
updateChart("Eat");
});
d3.select("#Play").on("click", function() {
console.log("Play")
updateChart("Play");
});
d3.select("#Learn").on("click", function() {
console.log("Learn")
updateChart("Learn");
});
//Calling update functions when a Race button is selected
d3.select("#White").on("click", function() {
updateRace('White');
});
d3.select("#Black").on("click", function() {
updateRace('Black');
});
d3.select("#Asian").on("click", function() {
updateRace('Asian');
});
d3.select("#Islander").on("click", function() {
updateRace('Islander');
});
d3.select("#Native").on("click", function(){
updateRace('Native');
})
d3.select("#Twoormore").on("click", function(){
updateRace('Twoormore');
})
d3.select("#Other").on("click", function(){
updateRace('Other');
})
d3.select("#Reset").on("click", function(){
updateRace('Reset');
})
// Create a tooltip div that is hidden by default:
var tooltip = d3.select("#tt")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "white")
.style("border-radius", "5px")
.style("padding", "10px")
.style("color", "black")
//Move to front function
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
//Definition of tooltip functions
var showTooltip = function(d) {
//Selects and hihlights the census tracts of the same ntacode in the bubble chart and map
svg.selectAll("."+d.properties.ntacode).style('stroke','black').moveToFront();
svg3.selectAll("."+d.properties.ntacode).style('stroke','black').style("stroke-width", 2).moveToFront();
//Definition of the poupup message
tooltip.style("opacity", 1)
.html("<b>" + d.properties.ntaname
+ "," +d.properties.boro_name +"</b><br>"
+ "White:" + d.properties.Perc_white+"%<br>"
+ "Black:" + d.properties.Perc_black_afam+"%<br>"
+ "Asian:" + d.properties.Perc_asian+"%<br>"
+ "Other:" + d.properties.Perc_other_race+"%<br>"
+ "Two or more:" + d.properties.Perc_two_or_more+"%<br>"
+ "Native:" + d.properties.Perc_amindian_alnative+"%<br>"
+ "Islander:" + d.properties.Perc_hawaii_pi+"%<br>"
)
.style("font-size","11px")
.style("background","#fafafa")
.style("left", (d3.mouse(this)[0]+30) + "px")
.style("top", (d3.mouse(this)[1]+30) + "px");
}
//Moving tooltip
var moveTooltip = function(d) {
tooltip.style("left", (d3.mouse(this)[0]+30) + "px")
.style("top", (d3.mouse(this)[1]+30) + "px")
}
//Setting back the default parameters
var hideTooltip = function(d) {
svg.selectAll("."+d.properties.ntacode).style('stroke','white');
svg3.selectAll("."+d.properties.ntacode).style('stroke','lightgray').style("stroke-width", 1);
tooltip.style("opacity", 0)
}
</script>
</body>
</html>