-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
834 lines (697 loc) · 35.2 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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script type="text/javascript" src="libs/d3.v5.min.js"></script>
<script type="text/javascript" src="libs/topojson.v3.min.js"></script>
<style></style>
</head>
<body onload="init()">
<h1>Flint Water Crisis and its Effect on Real Estate</h1>
<h2>Kalina Borkiewicz</h2>
<h3>CS498DDV Summer 2020</h3>
<p>The Flint water crisis started in April 2014, when lead from aging water pipes in Flint, Michigan leached into the water supply and exposed residents to the dangerous chemical. The effects of this crisis are wide-reaching and long-lasting. Here, we investigate the effect of the Flint water crisis on real estate in Genesee County, in which Flint resides.</p>
<hr>
<p>This first chart tracks individual home sales month-by-month, plotting the house's sales price against the house's distance to the affected water pipe boundary. At first glance, it appears that houses closer to the boundary are harder hit with lower sales prices. But if you compare against past and future sales, it becomes apparent that the houses closest to the water pipe boundary were in poorer areas to begin with. At the start of the crisis, the trend (orange line) for all houses in the county has been negatively affected about equally, not just the ones closest to the water pipe. However, as time has gone on, houses farther away from the pipe begin to recover in terms of sales price, while closeby houses remain low. </p>
<p>Move the slider to change the date, and see the data updated in the chart below. The best-fit line for the data is drawn for each month, starting from purple in the distant past and moving toward yellow closer to the present. Hover over a data point to see its exact value.</p>
<h3 id="distanceSliderText">May 2014: First month after start of Flint water crisis</h3><br/>
<svg id="colorbarDistLabel" width=520 height=20>
<text x="0" y="15" fill="#310080">2/99</text>
<text x="395" y="15" fill="orange">5/14</text>
<text x="485" y="15" fill="#e3d000">7/17</text>
</svg><br/>
<svg id="colorbarDist" width=500 height=30></svg><br/>
<input type="range" name="distanceSlider" id="distanceSlider" min="0" max="223" value="184" step="1" list="steplistDist" onchange="updateDistanceSliderText(this.value);" style="width: 500px;">
<datalist id="steplistDist">
<option value="1">1</option>
<option value="184">184</option>
<option value="222">222</option>
</datalist> <br/>
<svg id="bar_distxprice" width=1280 height=600>
<text x="1085" y="113" fill="#fdb130">May 2014: First month</text>
</svg>
<hr>
<p>Now we can look at this same data on a map. The approximate outline of the affected water pipeline is shown in blue, surrounding the center part of town. Houses that sold for low prices (under $50,000) are shown in shades of red, and more expensive houses are shown in green. Though the center of town had always had more "red" then the outskirts of town, as we move forward in time, this distinction becomes much more pronounced and there is less "mixing" between the red and green points. The richer neighborhoods slowly return to normal, while the poor neighborhoods remain affected.</p>
<div id="tooltip" style="opacity:0; position:absolute; background: white;"></div>
<h3 id="mapPriceSliderText">May 2014: First month after start of Flint water crisis</h3><br/>
<svg width=520 height=20>
<text x="0" y="15" fill="blue">2/99</text>
<text x="395" y="15" fill="blue">5/14</text>
<text x="485" y="15" fill="blue">7/17</text>
</svg><br/>
<input type="range" name="mapPriceSlider" id="mapPriceSlider" min="1" max="223" value="184" step="1" onchange="updateMapPriceSliderText(this.value);" style="width: 500px;" list="steplistMap">
<datalist id="steplistMap">
<option value="1">1</option>
<option value="184">184</option>
<option value="222">222</option>
</datalist> <br/>
<svg id="mapPrice" width=1280 height=600>
<svg id="colorbarMapPrice" width=50 height=500></svg>
<text x="35" y="45" fill="green">$100,000+</text>
<text x="35" y="273" fill="#e3d000">$50,000</text>
<text x="35" y="500" fill="#8b0000">$1</text>
<text x="0" y="525">Sales Price</text>
<line x1="590" y1="240" x2="900" y2="200" style="stroke:blue;stroke-width:2" />
<text x="905" y="205" fill="blue">Pipeline boundary</text>
</svg>
<hr>
<p>Data on lead levels in individual houses started being collected one year after the crisis began, in April 2015. At this time, there were many houses with extremely dangerous lead levels in their water. While no amount of lead can be considered "safe", the Environmental Protection Agency (EPA) recommends that drinking water contain less than 0.015 μg/L of lead. When numbers reach 0.05 μg/L, this becomes definitively dangerous. A year after the start of the crisis, many houses still had extremely high levels of lead in their water.</p>
<p>By the end of 2015, after bad press about the abysmal numbers, the lead levels got better. But as time went on, people and media forgot about Flint, MI, and the numbers started to creep up once again. Use the slider to move forward in time ad explore the data.</p>
<h3 id="leadSliderText">April 2015: One year after start of Flint water crisis</h3><br/>
<svg id="colorbarLeadLabel" width=520 height=20>
<text x="0" y="15" fill="#310080">4/15</text>
<text x="485" y="15" fill="#e3d000">8/17</text>
</svg><br/>
<svg id="colorbarLead" width=500 height=30></svg><br/>
<input type="range" name="leadSlider" id="leadSlider" min="195" max="223" value="195" step="1" onchange="updateLeadSliderText(this.value);" style="width: 500px;"><br/>
<svg id="bar_leadxprice" width=1280 height=600>
<text x="110" y="45" fill="#77ab29">EPA safe threshold</text>
<text x="355" y="45" fill="#8b0000">Children should not be exposed above this amount</text>
</svg><br/>
<hr>
<p>Once again, we can look at this same data on a map to get an idea for the physical distribution. Initially, the southwest region had the most dangerous, highest (reddest) lead levels. However once the lead levels improved (turned more green), it was the northwest region that started edging toward yellow. </p>
<div id="tooltip" style="opacity:0; position:absolute; background: white;"></div>
<h3 id="mapLeadSliderText">April 2015: One year after start of Flint water crisis</h3><br/>
<svg width=520 height=20>
<text x="0" y="15" fill="blue">4/15</text>
<text x="485" y="15" fill="blue">8/17</text>
</svg><br/>
<input type="range" name="mapLeadSlider" id="mapLeadSlider" min="195" max="223" value="195" step="1" onchange="updateMapLeadSliderText(this.value);" style="width: 500px;"><br/>
<svg id="mapLead" width=1280 height=600>
<svg id="colorbarMapLead" width=50 height=500></svg>
<text x="35" y="45" fill="#8b0000">0.05+</text>
<text x="35" y="136" fill="#f05918">0.04</text>
<text x="35" y="227" fill="#f2bf57">0.03</text>
<text x="35" y="318" fill="#d3e35f">0.02</text>
<text x="35" y="409" fill="#77ab29">0.01</text>
<text x="35" y="500" fill="green">0</text>
<text x="0" y="525">Lead Level (μg/L)</text>
<line x1="590" y1="240" x2="900" y2="200" style="stroke:blue;stroke-width:2" />
<text x="905" y="205" fill="blue">Pipeline boundary</text>
</svg>
<script>
var data = null;
var distanceGraph = null;
var leadGraph = null;
var mapLeadChart = null;
var mapPriceChart = null;
var selectedMonthDist = 184;
var selectedMonthLead = 195;
var selectedMonthMapLead = 195;
var selectedMonthMapPrice = 184;
var svgHeight = 500;
var svgWidth = 1000;
var projection = d3.geoEquirectangular().center([-83.6875,43.0125]).scale(74000);
var path = d3.geoPath().projection(projection);
var plasmaLead = d3.scaleSequential().domain([195,223]).interpolator(d3.interpolatePlasma);
var plasmaDist = d3.scaleSequential().domain([1,223]).interpolator(d3.interpolatePlasma);
var redGreenPrice = d3.scaleSequential().domain([0,100000]).interpolator(d3.interpolateRdYlGn);
var redGreenLead = d3.scaleSequential().domain([0.05,0]).interpolator(d3.interpolateRdYlGn);
var redGreenPriceInverse = d3.scaleSequential().domain([100000,0]).interpolator(d3.interpolateRdYlGn);
var redGreenLeadInverse = d3.scaleSequential().domain([0,0.05]).interpolator(d3.interpolateRdYlGn);
//https://stackoverflow.com/questions/17226576/creating-a-text-labeled-x-axis-with-an-ordinal-scaleDist-in-d3-js
var yScaleDist = d3.scalePow().exponent(0.5).domain([300000,0]).range([0,svgHeight]);
var yScaleLead = d3.scalePow().exponent(0.5).domain([3000000,0]).range([0,svgHeight])
var xScaleDist = d3.scalePow().exponent(0.5).domain([0,50000]).range([0,svgWidth]); //distance
var xScaleLead = d3.scalePow().exponent(0.5).domain([0,0.65]).range([0,svgWidth]); //lead
function filterByMonthDist(d){
var month = parseInt(d.Month);
return month === selectedMonthDist;
//return (parseInt(d.Month) <= selectedMonthDist);
}
function filterByMonthLead(d){
var month = parseInt(d.Month);
//return month === selectedMonthLead;
return (month >= 195 && month <= selectedMonthLead);
}
function filterByMonthMapLead(d){
var month = parseInt(d.Month);
var price = parseFloat(d.SalesPrice);
return (price > 0) && (month >= 195) && (month <= selectedMonthMapLead);
}
function filterByMonthMapPrice(d){
var month = parseInt(d.Month);
var price = parseFloat(d.SalesPrice);
return (price > 0) && (month <= selectedMonthMapPrice);
}
async function init(){
data = await d3.csv("https://raw.githubusercontent.com/kalinalinkalina/flint/master/data/flint_months_noNAs_noExpectedLead.csv");
////////////////// Draw Distance vs Price bar chart //////////////////
var colorBarDist = d3.select("#colorbarDist")
.selectAll("rect")
.data(d3.range(1,223), function(d) { return d; })
.enter()
.append("rect")
.attr("x", function(d,i){return 500/(223-1) * i;})
.attr("y", 0)
.attr("height", 50)
.attr("width", 500)
.style("fill", function(d,i){return plasmaDist(d);})
distanceGraph = d3.select("#bar_distxprice")
.append("g")
.attr("transform","translate(80,50)");
drawDistanceChart();
// Add axes
d3.select("#bar_distxprice")
.append("g")
.attr("transform", "translate(80,50)")
.call(d3.axisLeft(yScaleDist));
d3.select("#bar_distxprice")
.append("g")
.attr("transform", "translate(80,550)")
.call(d3.axisBottom(xScaleDist));
// Add axes labels
d3.select("#bar_distxprice")
.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", svgWidth / 2 + 80)
.attr("y", svgHeight + 90)
.text("Distance to Water Pipe Boundary (m)");
d3.select("#bar_distxprice")
.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 0)
.attr("x", -svgHeight/2 + 80)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("Sales Price of House ($)");
////////////////////////////////////////////////////////////////////////
////////////////// Draw Lead vs Price bar chart //////////////////
var colorBarLead = d3.select("#colorbarLead")
.selectAll("rect")
.data(d3.range(195,223), function(d) { return d; })
.enter()
.append("rect")
.attr("x", function(d,i){return 500/(223-195) * i;})
.attr("y", 0)
.attr("height", 50)
.attr("width", 500)
.style("fill", function(d,i){return plasmaLead(d);})
leadGraph = d3.select("#bar_leadxprice")
.append("g")
.attr("transform","translate(80,50)");
drawLeadChart();
// Add axes
ticks = xScaleLead.ticks();
ticks.push(0.015);
d3.select("#bar_leadxprice")
.append("g")
.attr("transform", "translate(80,50)")
.call(d3.axisLeft(yScaleLead));
d3.select("#bar_leadxprice")
.append("g")
.attr("transform", "translate(80,550)")
.call(d3.axisBottom(xScaleLead).tickValues(ticks));
// Add axes labels
d3.select("#bar_leadxprice")
.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", svgWidth / 2 + 80)
.attr("y", svgHeight + 90)
.text("Lead Level (μg/L)");
d3.select("#bar_leadxprice")
.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 0)
.attr("x", -svgHeight/2 + 80)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("Sales Price of House ($)");
////////////////// Draw Lead Map //////////////////
var colorBarMapPrice = d3.select("#colorbarMapLead")
.selectAll("rect")
.data(d3.range(0,500), function(d) { return d; })
.enter()
.append("rect")
.attr("x", 0)
.attr("y", function(d,i){return 30+500/(500-1) * i;})
.attr("height", 500)
.attr("width", 30)
.style("fill", function(d,i){return redGreenLeadInverse(d/10000);})
mapLeadChart = d3.select("#mapLead")
.append("g")
.attr("transform", "translate(80,50)");
drawMapLead();
////////////////////Draw Price Map ////////////////////////
var colorBarMapPrice = d3.select("#colorbarMapPrice")
.selectAll("rect")
.data(d3.range(1,1000), function(d) { return d; })
.enter()
.append("rect")
.attr("x", 0)
.attr("y", function(d,i){return 30+500/(1000-1) * i;})
.attr("height", 500)
.attr("width", 30)
.style("fill", function(d,i){return redGreenPriceInverse(d*100);})
mapPriceChart = d3.select("#mapPrice")
.append("g")
.attr("transform", "translate(80,50)");
drawMapPrice();
}
function addMarkers(){
//data.forEach(function(d){
//var marker = L.circleMarker([+d.Latitude, +d.Longitude]);
//marker.addTo(mapLead);
//})
}
async function drawDistanceChart(){
var div = d3.select("#tooltip");
var data_month = await data.filter(filterByMonthDist);
data_month = data_month.sort(function(a,b){
return a.Month - b.Month;
});
var dots = distanceGraph
.selectAll("circle")
.data(data_month);
dots
.enter()
.append("circle")
.attr("r", 3)
.attr("cx", function(d,i){return xScaleDist(d.DistToBoundary);})
.attr("cy", function(d,y){return yScaleDist(d.SalesPrice);})
.on('mouseover', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '.85');
//Makes the new div appear on hover:
div.transition()
.duration(50)
.style("opacity", 1);
let text = "Distance to Boundary: " + d.DistToBoundary + "m<br/>Sales Price: $" + d.SalesPrice;
div.html(text)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 15) + "px")
})
.on('mouseout', function (d, i) {
d3.select(this).transition()
.duration('500')
.attr('opacity', '1');
//Makes the new div disappear:
div.transition()
.duration('3000')
.style("opacity", 0);
});
dots
.transition().duration(30)
.attr("cx", function(d,i){return xScaleDist(d.DistToBoundary);})
.attr("cy", function(d,y){return yScaleDist(d.SalesPrice);});
dots.exit().remove();
// Draw regression line
var lg = calcLinear(data_month, "DistToBoundary", "SalesPrice", d3.min(data_month, function(d){ return d.DistToBoundary}), d3.min(data_month, function(d){ return d.SalesPrice}));
//await distanceGraph.selectAll("line").remove();
var line = distanceGraph
.append("line")
.data(data_month)
.attr("x1", xScaleDist(lg.ptA.x))
.attr("y1", yScaleDist(lg.ptA.y))
.attr("x2", xScaleDist(lg.ptB.x))
.attr("y2", yScaleDist(lg.ptB.y))
.style("stroke", function(d,i){return plasmaDist(d.Month);})
.style("stroke-width", function(d,i){
if(d.Month == 184){
return 5;
}
return 1;
});
}
async function drawLeadChart(){
var div = d3.select("#tooltip");
var data_month = await data.filter(filterByMonthLead);
data_month = data_month.sort(function(a,b){
return a.Month - b.Month;
});
// Draw safe line
var line = leadGraph
.append("line")
.attr("x1", xScaleLead(0.015))
.attr("y1", yScaleLead(0))
.attr("x2", xScaleLead(0.015))
.attr("y2", yScaleLead(3000000))
.style("stroke", "#77ab29")
.style("stroke-width", 5);
// Draw dangerous line
var line = leadGraph
.append("line")
.attr("x1", xScaleLead(0.05))
.attr("y1", yScaleLead(0))
.attr("x2", xScaleLead(0.05))
.attr("y2", yScaleLead(3000000))
.style("stroke", "#8b0000")
.style("stroke-width", 5);
var dots = leadGraph
.selectAll("circle")
.data(data_month);
dots
.enter()
.append("circle")
//.attr("class", "dot")
.attr("r", 3)
.attr("cx", function(d,i){return xScaleLead(d.LeadLevel);})
.attr("cy", function(d,i){return yScaleLead(d.SalesPrice);})
.on('mouseover', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '.85');
//Makes the new div appear on hover:
div.transition()
.duration(50)
.style("opacity", 1);
let text = "Lead Level: " + d.LeadLevel + "μg/L<br/>Sales Price: $" + d.SalesPrice;
div.html(text)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 15) + "px")
})
.on('mouseout', function (d, i) {
d3.select(this).transition()
.duration('500')
.attr('opacity', '1');
//Makes the new div disappear:
div.transition()
.duration('3000')
.style("opacity", 0);
});
// Voronoi
/*var voronoi = d3.voronoi()
//.x(function(d){ return xScaleLead(d.LeadLevel); })
//.y(function(d){ return yScaleLead(d.SalesPrice); })
.extent([0,0], [svgWidth, svgHeight]);
var vpath = leadGraph.append("g").selectAll("path");
vpath.data(voronoi.polygons(data_month)).enter().append("path")
.attr("stroke", "blue")
.attr("fill", "none")
.attr("d", function(d,i){return "M" + d.join("L") + "Z";});*/
dots
.transition().duration(0)
.attr("cx", function(d,i){return xScaleLead(d.LeadLevel);})
.attr("cy", function(d,i){return yScaleLead(d.SalesPrice);});
dots
.exit()
.remove();
leadGraph
//.update()
.selectAll("circle")
.style("fill", function(d,i){return plasmaLead(d.Month);});
}
async function drawMapLead(){
var div = d3.select("#tooltip");
var data_month = await data.filter(filterByMonthMapLead);
var geojson = d3.json("https://raw.githubusercontent.com/kalinalinkalina/flint/master/data/genesee_geo.json").then(function(json){
//projection.fitSize([svgWidth,svgHeight], json);
for(var i=0; i<data.length; i++){
var lat = data.Latitude;
var lon = data.Longitude;
json.features.push()
}
mapLeadChart
.selectAll("path")
.data(json.features)
.join("path")
.attr("d", path)
.attr("stroke-opacity", 0.1)
.attr("stroke", "#333");
});
/*
// Draw pipe boundary
var links = [{
type: "LineString",
coordinates: [
[-83.7317, 43.0754],
[-83.6493, 43.0737],
[-83.6414, 42.9909],
[-83.7317, 42.9909]
//[43.0754, -83.7317],
//[43.0737, -83.6493],
//[42.9909, -83.6414],
//[42.9909, -83.7317]
]
}];
var arcGroup = mapLeadChart.append('g');
var pathArcs = arcGroup
.selectAll("arc")
.data(links)
//.join("path")
//.attr("d", links)
.attr("stroke", "blue")
.attr("stroke-width", "2px");*/
// Sort the data so the values we want are drawn on top
//data = data.sort(function(a,b){
// if (a.SalesPrice < 100000) return -1;
// else return 1;
//});
data_month = data_month.sort(function(a,b){
return a.Month - b.Month;
});
var circles = mapLeadChart
.selectAll("circle")
.data(data_month);
circles
.enter()
.append("circle")
.attr("cx", function(d,i){return projection([d.Longitude, d.Latitude])[0];})
.attr("cy", function(d,i){return projection([d.Longitude, d.Latitude])[1];})
.attr("r", 3)
.style("fill", function(d,i){return redGreenLead(d.LeadLevel);})
.on('mouseover', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '.85');
//Makes the new div appear on hover:
div.transition()
.duration(50)
.style("opacity", 1);
let text = "Lead Level: " + d.LeadLevel + "μg/L<br/>Sales Price: $" + d.SalesPrice;
div.html(text)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 15) + "px")
})
.on('mouseout', function (d, i) {
d3.select(this).transition()
.duration('500')
.attr('opacity', '1');
//Makes the new div disappear:
div.transition()
.duration('3000')
.style("opacity", 0);
});
circles
.transition().duration(0)
.attr("cx", function(d,i){return projection([d.Longitude, d.Latitude])[0];})
.attr("cy", function(d,i){return projection([d.Longitude, d.Latitude])[1];})
.style("fill", function(d,i){return redGreenLead(d.LeadLevel);});
circles
.exit()
.remove();
// Draw pipeline boundary
var square = mapLeadChart
.append("rect")
.attr("x", 420)
.attr("y", 190)
.attr("width", 90)
.attr("height", 90)
.style("stroke", "blue")
.style("stroke-width", 3)
.style("stroke-opacity", 0.5)
.style("fill-opacity", 0);
}
async function drawMapPrice(){
var div = d3.select("#tooltip");
var data_month = await data.filter(filterByMonthMapPrice);
var geojson = d3.json("https://raw.githubusercontent.com/kalinalinkalina/flint/master/data/genesee_geo.json").then(function(json){
//projection.fitSize([svgWidth,svgHeight], json);
for(var i=0; i<data.length; i++){
var lat = data.Latitude;
var lon = data.Longitude;
json.features.push()
}
mapPriceChart
.selectAll("path")
.data(json.features)
.join("path")
.attr("d", path)
.attr("stroke-opacity", 0.1)
.attr("stroke", "#333");
});
// Sort the data so the values we want are drawn on top
//data = data.sort(function(a,b){
// if (a.SalesPrice < 100000) return -1;
// else return 1;
//});
data_month = data_month.sort(function(a,b){
return a.Month - b.Month;
});
var circles = mapPriceChart
.selectAll("circle")
.data(data_month);
circles
.enter()
.append("circle")
.attr("cx", function(d,i){return projection([d.Longitude, d.Latitude])[0];})
.attr("cy", function(d,i){return projection([d.Longitude, d.Latitude])[1];})
.attr("r", 3)
.style("fill", function(d,i){return redGreenPrice(d.SalesPrice);})
.on('mouseover', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '.85');
//Makes the new div appear on hover:
div.transition()
.duration(50)
.style("opacity", 1);
let text = "Sales Price: $" + d.SalesPrice;
div.html(text)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 15) + "px")
})
.on('mouseout', function (d, i) {
d3.select(this).transition()
.duration('500')
.attr('opacity', '1');
//Makes the new div disappear:
div.transition()
.duration('3000')
.style("opacity", 0);
});
circles
.transition().duration(0)
.attr("cx", function(d,i){return projection([d.Longitude, d.Latitude])[0];})
.attr("cy", function(d,i){return projection([d.Longitude, d.Latitude])[1];})
.style("fill", function(d,i){return redGreenPrice(d.SalesPrice);});
circles
.exit()
.remove();
// Draw pipeline boundary
var square = mapPriceChart
.append("rect")
.attr("x", 420)
.attr("y", 190)
.attr("width", 90)
.attr("height", 90)
.style("stroke", "blue")
.style("stroke-width", 3)
.style("stroke-opacity", 0.5)
.style("fill-opacity", 0);
}
// Listen to the sliders
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
d3.select("#distanceSlider").on("change", function(d){
selectedMonthDist = parseInt(this.value);
drawDistanceChart();
});
function updateDistanceSliderText(val){
var startDate = new Date(1998, 12, 12);
var monthsSinceStart = parseInt(val);
var currentDate = new Date(startDate.setMonth(startDate.getMonth()+monthsSinceStart));
var dateString = monthNames[currentDate.getMonth()] + " " + currentDate.getFullYear();
document.getElementById('distanceSliderText').innerHTML = dateString;
}
d3.select("#leadSlider").on("change", function(d){
selectedMonthLead = parseInt(this.value);
drawLeadChart();
});
function updateLeadSliderText(val){
var startDate = new Date(1998, 12, 12);
var monthsSinceStart = parseInt(val);
var currentDate = new Date(startDate.setMonth(startDate.getMonth()+monthsSinceStart));
var dateString = monthNames[currentDate.getMonth()] + " " + currentDate.getFullYear();
document.getElementById('leadSliderText').innerHTML = dateString;
}
d3.select("#mapLeadSlider").on("change", function(d){
selectedMonthMapLead = parseInt(this.value);
drawMapLead();
});
function updateMapLeadSliderText(val){
var startDate = new Date(1998, 12, 12);
var monthsSinceStart = parseInt(val);
var currentDate = new Date(startDate.setMonth(startDate.getMonth()+monthsSinceStart));
var dateString = monthNames[currentDate.getMonth()] + " " + currentDate.getFullYear();
document.getElementById('mapLeadSliderText').innerHTML = dateString;
}
d3.select("#mapPriceSlider").on("change", function(d){
selectedMonthMapPrice = parseInt(this.value);
drawMapPrice();
});
function updateMapPriceSliderText(val){
var startDate = new Date(1998, 12, 12);
var monthsSinceStart = parseInt(val);
var currentDate = new Date(startDate.setMonth(startDate.getMonth()+monthsSinceStart));
var dateString = monthNames[currentDate.getMonth()] + " " + currentDate.getFullYear();
document.getElementById('mapPriceSliderText').innerHTML = dateString;
}
// Returns an object with two points, where each point is an object with an x and y coordinate
function calcLinear(data, x, y, minX, minY){
/////////
//SLOPE//
/////////
// Let n = the number of data points
var n = data.length;
// Get just the points
var pts = [];
data.forEach(function(d,i){
var obj = {};
obj.x = parseFloat(d[x]);
obj.y = parseFloat(d[y]);
obj.mult = obj.x*obj.y;
if (obj.y > 0){
pts.push(obj);
}
});
// Let a equal n times the summation of all x-values multiplied by their corresponding y-values
// Let b equal the sum of all x-values times the sum of all y-values
// Let c equal n times the sum of all squared x-values
// Let d equal the squared sum of all x-values
var sum = 0;
var xSum = 0;
var ySum = 0;
var sumSq = 0;
pts.forEach(function(pt){
sum = sum + pt.mult;
xSum = xSum + pt.x;
ySum = ySum + pt.y;
sumSq = sumSq + (pt.x * pt.x);
});
var a = sum * n;
var b = xSum * ySum;
var c = sumSq * n;
var d = xSum * xSum;
// Plug the values that you calculated for a, b, c, and d into the following equation to calculate the slope
// slope = m = (a - b) / (c - d)
var m = (a - b) / (c - d);
/////////////
//INTERCEPT//
/////////////
// Let e equal the sum of all y-values
var e = ySum;
// Let f equal the slope times the sum of all x-values
var f = m * xSum;
// Plug the values you have calculated for e and f into the following equation for the y-intercept
// y-intercept = b = (e - f) / n
var b = (e - f) / n;
// return an object of two points
// each point is an object with an x and y coordinate
var pt1 = [minX, m * minX + b];
var pt2 = [(minY - b) / m, minY];
var slope = (pt2[1] - pt1[1]) / (pt2[0] - pt1[0]);
var xStart = 0;
var yStart = pt1[1] + (xStart - pt1[0]) * slope;
var start = [xStart, yStart];
var xEnd = 50000;
var yEnd = pt1[1] + (xEnd - pt1[0]) * slope;
var end = [xEnd, yEnd];
return {
ptA : {
x: start[0],
y: start[1]
},
ptB : {
x: end[0],
y: end[1]
}
}
}
</script>
</body>
</html>