Skip to content

Instantly share code, notes, and snippets.

@rubin2ma
Last active February 20, 2019 02:49
Show Gist options
  • Save rubin2ma/f0db7c8f92e648135bb7d760ca29880a to your computer and use it in GitHub Desktop.
Save rubin2ma/f0db7c8f92e648135bb7d760ca29880a to your computer and use it in GitHub Desktop.
Group Project Bioinformatics Final Copy!
license: mit

https://gist.github.com/4544529 (demo) was my first d3 adventure, where I scraped signatures from a White House petition and showed them as a pie chart.

Here, I show them as a US heat map (based on one of Mike Bostock's map demos). You can click on a state, and see how many signatures came from it.

Darker color means more signatures, but since the numbers range from a few dozens to >4500, the scale is logarithmic. I don't know yet how to use log() interpolators (I promise to learn), so I just did it quick and dirty with linear() and masking tape.

forked from thedod's block: Clickable US heat-map of petition signature count

forked from Craftbd's block: Group Project Bioinformatics

forked from rubin2ma's block: Group Project Bioinformatics

forked from Craftbd's block: Group Project Bioinformaticsnew

forked from rubin2ma's block: Group Project Bioinformaticsnew3

forked from scresawn's block: Group Project Bioinformatics Final Copy!

Year name UFO (Population / Sightings) Medium Income Poverty Level (%) Mental Health (18+ %) Marijuana use (18+ %) Alcohol Dependence (18+ %)
2013 Alabama 57493.79762 42849 18.29 4.99 23.47741019 21.78534459
2013 Alaska 14170.75 72237 9.09 4.32 11.69511111 19.38926316
2013 Arizona 28310.32906 48510 18.21 5.16 15.23357306 16.72256892
2013 Arkansas 53793.87273 40511 19.11 5.2 19.88447987 24.48584711
2013 California 53391.64763 60190 16.51 3.53 14.4062875 18.85798555
2013 Colorado 36328.29655 58823 12.67 4.36 8.803690713 16.85277937
2013 Connecticut 25685.73571 67098 10.4 3.46 15.16429536 18.15120202
2013 Delaware 77116.25 57846 12.03 3.81 15.76561864 20.22111957
2013 District_of_Columbia 324582.5 67572 17.8 3.45 9.762462687 12.11268519
2013 Florida 36195.97412 46036 16.61 3.61 16.92571312 20.36675077
2013 Georgia 69824.74126 47829 18.47 3.43 17.1557 20.65037963
2013 Hawaii 26537.37736 68020 10.55 3.39 17.005 18.81886667
2013 Idaho 19901.37037 46783 15.29 5.05 21.92934459 19.55146386
2013 Illinois 74448.00578 56210 14.33 3.29 17.16469933 20.2096146
2013 Indiana 45304.15172 47529 15.45 4.26 17.55244667 18.91427443
2013 Iowa 30613.16832 52229 12.26 4.38 26.49555556 20.12974026
2013 Kansas 52596.74545 50972 13.6 4.17 21.45252222 17.34185928
2013 Kentucky 45365.74227 43399 18.19 4.57 19.41307048 22.83298964
2013 Louisiana 90713.76471 44964 19.19 3.72 21.97697156 21.17415982
2013 Maine 16208.2439 46974 13.59 4.35 9.778658088 21.10948413
2013 Maryland 51575.03478 72483 9.87 3.19 15.33302062 18.6495674
2013 Massachusetts 34393.77436 66768 11.49 3.77 10.66299287 18.28355571
2013 Michigan 58229.30588 48273 16.65 4.45 12.5887859 20.17795214
2013 Minnesota 43348.168 60702 10.93 3.9 18.05918605 19.69498188
2013 Mississippi 76679.02564 37963 23.27 4.56 23.18946512 22.15882222
2013 Missouri 54933.73636 46931 15.41 4.6 16.35627162 19.71277036
2013 Montana 19138 46972 16.13 4.74 12.73238125 16.16810317
2013 Nebraska 88979 51440 12.81 4.44 19.73528421 16.89055856
2013 Nevada 398066.2857 51230 15.56 4.21 17.13255183 18.48512171
2013 New Hampshire 47238.82143 64230 8.43 4.39 10.86651639 15.97246988
2013 New Jersey 54596.08589 70165 11.22 3.09 20.82262033 19.24855616
2013 New Mexico 24823.72619 43872 21.51 4.38 13.89405667 18.28165351
2013 New York 78069.62698 57369 15.53 3.56 15.04662376 18.54616808
2013 North Carolina 49957.30964 45906 17.43 3.74 19.89536117 20.55716112
2013 North Dakota 16089.31111 55759 11.38 3.87 22.87379688 16.26581111
2013 Ohio 32049.92244 48981 15.53 4.71 18.80229708 18.68099194
2013 Oklahoma 47560.67901 45690 16.27 5.25 21.71324157 19.92245876
2013 Oregon 20661.84737 50251 16.36 4.97 10.25210649 17.69982511
2013 Pennsylvania 41769.07843 52007 13.23 4.06 17.7090741 18.47680853
2013 Rhode Island 35101.1 55902 13.72 4.28 9.848191589 15.72770896
2013 South Carolina 30760.60645 44163 18.05 4.26 20.07599163 21.23080531
2013 South Dakota 56328.13333 48947 13.66 3.82 28.29138333 16.6419902
2013 Tennessee 51958.568 44297 17.35 4.23 23.88183883 23.28479286
2013 Texas 107180.2632 51704 17.11 3.57 23.59464488 20.08205865
2013 Utah 32614.19101 59770 12.44 5.16 26.09151339 24.97649145
2013 Vermont 9502.121212 52578 11.81 4.69 9.359134328 16.94762162
2013 Virginia 40503.39216 62666 11.36 4.17 19.18988889 17.78976824
2013 Washington 21572.77399 58405 13.88 5.39 10.11702886 19.05190489
2013 West Virginia 31952.25862 41253 17.93 5.38 20.11817935 19.48286842
2013 Wisconsin 32263.22472 51467 13.16 3.89 20.61152509 16.06317179
2013 Wyoming 34275.52941 58752 10.65 4.07 20.82725 16.6618
2014 Alabama 39058.17742 42830 17.8 4.99 23.47741019 21.78534459
2014 Alaska 17132.67442 71583 11.9 4.32 11.69511111 19.38926316
2014 Arizona 22325.55814 50068 21.2 5.16 15.23357306 16.72256892
2014 Arkansas 87262.11765 41262 18.4 5.2 19.88447987 24.48584711
2014 California 44666.06236 61933 15.8 3.53 14.4062875 18.85798555
2014 Colorado 27018.42424 61303 12.3 4.36 8.803690713 16.85277937
2014 Connecticut 37809.18947 70048 8.6 3.46 15.16429536 18.15120202
2014 Delaware 25268.86486 59716 11 3.81 15.76561864 20.22111957
2014 District_of_Columbia 94143.57143 71648 19 3.45 9.762462687 12.11268519
2014 Florida 31124.7903 47463 16.7 3.61 16.92571312 20.36675077
2014 Georgia 45643.57919 49321 16.8 3.43 17.1557 20.65037963
2014 Hawaii 20526.7971 69592 10.8 3.39 17.005 18.81886667
2014 Idaho 24022.52941 47861 12.4 5.05 21.92934459 19.55146386
2014 Illinois 76592.52381 57444 13.7 3.29 17.16469933 20.2096146
2014 Indiana 45172.82877 49446 14.6 4.26 17.55244667 18.91427443
2014 Iowa 35318.52273 53712 10.3 4.38 26.49555556 20.12974026
2014 Kansas 42637.64706 52504 12.1 4.17 21.45252222 17.34185928
2014 Kentucky 49584.91011 42958 20 4.57 19.41307048 22.83298964
2014 Louisiana 41872.79279 44555 23.1 3.72 21.97697156 21.17415982
2014 Maine 25107.90566 49462 14.6 4.35 9.778658088 21.10948413
2014 Maryland 41439.54861 73971 9.8 3.19 15.33302062 18.6495674
2014 Massachusetts 34090.4596 69160 13.6 3.77 10.66299287 18.28355571
2014 Michigan 54184.51913 49847 14.8 4.45 12.5887859 20.17795214
2014 Minnesota 52942.80583 61481 8.3 3.9 18.05918605 19.69498188
2014 Mississippi 68009.09091 39680 22.1 4.56 23.18946512 22.15882222
2014 Missouri 42089.79167 48363 10.4 4.6 16.35627162 19.71277036
2014 Montana 18265.48214 46328 12 4.74 12.73238125 16.16810317
2014 Nebraska 110655.5882 52686 11.8 4.44 19.73528421 16.89055856
2014 Nevada 257546.6364 51450 17 4.21 17.13255183 18.48512171
2014 New Hampshire 53149.72 66532 7.2 4.39 10.86651639 15.97246988
2014 New Jersey 52810.6568 71919 11.3 3.09 20.82262033 19.24855616
2014 New Mexico 19110.31193 44803 20 4.38 13.89405667 18.28165351
2014 New York 60672.35385 58878 14 3.56 15.04662376 18.54616808
2014 North Carolina 35607.16487 46556 17.1 3.74 19.89536117 20.55716112
2014 North Dakota 23122 59029 9.7 3.87 22.87379688 16.26581111
2014 Ohio 41706.5036 49308 15.6 4.71 18.80229708 18.68099194
2014 Oklahoma 41693.53763 47529 17.3 5.25 21.71324157 19.92245876
2014 Oregon 17715.94196 51075 14.4 4.97 10.25210649 17.69982511
2014 Pennsylvania 34108.17333 53234 12.5 4.06 17.7090741 18.47680853
2014 Rhode Island 28499.45946 54891 11.3 4.28 9.848191589 15.72770896
2014 South Carolina 18429.12214 45238 16.5 4.26 20.07599163 21.23080531
2014 South Dakota 40598.14286 50979 12.8 3.82 28.29138333 16.6419902
2014 Tennessee 44826.4589 44361 17.3 4.23 23.88183883 23.28479286
2014 Texas 87767.91857 53035 16.4 3.57 23.59464488 20.08205865
2014 Utah 41434.30986 60922 10.2 5.16 26.09151339 24.97649145
2014 Vermont 20899.46667 54166 9.3 4.69 9.359134328 16.94762162
2014 Virginia 45952.33149 64902 10.2 4.17 19.18988889 17.78976824
2014 Washington 19433.04683 61366 12 5.39 10.11702886 19.05190489
2014 West Virginia 33609.34545 41059 20.6 5.38 20.11817935 19.48286842
2014 Wisconsin 39440.93836 52622 10.9 3.89 20.61152509 16.06317179
2014 Wyoming 27792.47619 57055 9.7 4.07 20.82725 16.6618
2015 Alabama 77045.63492 44765 13.5 4.21 13.27482923 41.52638889
2015 Alaska 19938.08108 73355 16.3 4.18 7.182538835 41.10008333
2015 Arizona 24435.71685 51492 9.2 4.25 10.26017612 37.56457923
2015 Arkansas 74446.325 41995 17.2 5.2 12.07712753 46.61016406
2015 California 48742.425 64500 16.1 3.79 9.381769424 37.9825034
2015 Colorado 33634.68519 63909 13.9 3.9 6.668303398 4.109709798
2015 Connecticut 42173.29412 71346 9.9 3.43 9.373274869 41.63477907
2015 Delaware 31469.2 61255 9.1 3.99 9.576469697 45.14621429
2015 District_of_Columbia 223459 75628 11.1 3.78 5.876291304 28.15722917
2015 Florida 43166.12793 49426 16.6 3.86 11.38722213 45.29640022
2015 Georgia 76114.91045 51244 16.2 3.99 11.80078769 41.85667143
2015 Hawaii 50898.46429 73486 18.1 3.43 10.97582308 36.58607692
2015 Idaho 35166.55319 48275 10.9 4.79 11.50333793 41.6996
2015 Illinois 70544.21429 59588 12.3 3.42 10.86465508 41.62432792
2015 Indiana 83705.92405 50532 10.9 4.71 10.47928877 38.73047076
2015 Iowa 32185.53608 54736 13.5 4.45 13.78125551 44.06119718
2015 Kansas 63189.58696 53906 10.4 4.12 12.58443723 38.25006579
2015 Kentucky 63208.72857 45215 14.2 4.96 12.20603994 49.23102778
2015 Louisiana 77816 45727 19.5 4.27 12.23903927 39.95994017
2015 Maine 22921.60345 51494 18.6 4.96 6.396471154 44.34886667
2015 Maryland 52130.28696 75847 12.3 3.31 9.992870216 40.57915541
2015 Massachusetts 46151.29252 70628 9.6 4.03 7.445793538 43.57698397
2015 Michigan 54794.00552 51084 11.5 4.53 8.388002959 40.33742886
2015 Minnesota 36068.65132 63488 12.8 4.32 10.91506647 47.01874786
2015 Mississippi 74734.75 40593 7.8 4.72 14.72442365 41.51469444
2015 Missouri 60762.04 50238 19.1 4.87 10.4906931 44.73972059
2015 Montana 16919.22951 49509 9.8 4.39 9.429968182 35.76884483
2015 Nebraska 631255 54996 11.9 4.44 13.47830142 37.26353922
2015 Nevada 262159.8182 52431 10.3 4.33 10.70554412 39.35010811
2015 New Hampshire 57830.91304 70303 13 4.65 7.570755682 34.16546154
2015 New Jersey 69266.82946 72222 7.3 3.46 11.85669098 44.03913793
2015 New Mexico 27737.70667 45382 11.2 4.23 8.669464583 36.50300877
2015 New York 66939.60339 60850 19.7 3.74 9.091268877 37.18688512
2015 North Carolina 53952.6129 47830 14.2 4.85 11.3637241 43.87385652
2015 North Dakota 75683.5 60557 15.3 4.04 13.52488393 36.06635714
2015 Ohio 54229.39252 51075 10.7 4.74 11.41566519 39.623657
2015 Oklahoma 57461.97059 48568 13.6 4.59 12.9651904 45.00560345
2015 Oregon 18984.12264 54148 14.2 4.83 6.810485738 39.02932212
2015 Pennsylvania 53522.61088 55702 11.9 4 11.12973499 43.64527474
2015 Rhode Island 21991.8125 58073 12.3 4.98 6.683648734 33.00051563
2015 South Carolina 46617.46667 47238 11.8 4.35 11.90332488 43.22786404
2015 South Dakota 50465.82353 53017 14.3 3.7 15.66702727 37.46463043
2015 Tennessee 60505.10092 47275 13.9 4.52 13.21981038 50.17518939
2015 Texas 149888.7377 55653 14.7 3.84 15.42752093 46.00019551
2015 Utah 38839.37662 62912 14.7 5.17 15.4128801 51.20211017
2015 Vermont 13913.06667 56990 9.3 5.28 6.25341 34.74116667
2015 Virginia 56158.30201 66262 10.7 3.98 10.31943112 38.48485092
2015 Washington 26717.5 64129 10.9 4.72 7.138483202 45.15090625
2015 West Virginia 29694.40323 42019 11.4 5.53 11.62074367 39.91472826
2015 Wisconsin 44368.39231 55638 14.5 4.15 11.34243517 36.77260828
2015 Wyoming 30871.31579 60214 11.4 4.3 12.73973913 34.47223529
2016 Alabama 91760.37736 46257 16.2 4.21 13.27482923 41.52638889
2016 Alaska 18094.97561 76440 12.6 4.18 7.182538835 41.10008333
2016 Arizona 30135.0913 53558 16.1 4.25 10.26017612 37.56457923
2016 Arkansas 72884.09756 44334 16 5.2 12.07712753 46.61016406
2016 California 68859.67895 67739 13.9 3.79 9.381769424 37.9825034
2016 Colorado 28268.08673 65685 8.5 3.9 6.668303398 4.109709798
2016 Connecticut 50372.56338 73433 9.8 3.43 9.373274869 41.63477907
2016 Delaware 52892.5 61757 11.6 3.99 9.576469697 45.14621429
2016 District_of_Columbia 136234 75506 16.3 3.78 5.876291304 28.15722917
2016 Florida 58392.17847 50860 13 3.86 11.38722213 45.29640022
2016 Georgia 85209.67769 53559 15.4 3.99 11.80078769 41.85667143
2016 Hawaii 49260.58621 74511 9.3 3.43 10.97582308 36.58607692
2016 Idaho 42078.5 51807 11.1 4.79 11.50333793 41.6996
2016 Illinois 85916.36913 60960 12.1 3.42 10.86465508 41.62432792
2016 Indiana 79916.3012 52314 11.8 4.71 10.47928877 38.73047076
2016 Iowa 43537.40278 56247 9.8 4.45 13.78125551 44.06119718
2016 Kansas 59332.42857 54935 11.2 4.12 12.58443723 38.25006579
2016 Kentucky 86999.4902 46659 15.2 4.96 12.20603994 49.23102778
2016 Louisiana 97534.70833 45146 20.2 4.27 12.23903927 39.95994017
2016 Maine 19580.57353 53079 12.7 4.96 6.396471154 44.34886667
2016 Maryland 62671.32292 78945 7.1 3.31 9.992870216 40.57915541
2016 Massachusetts 63072.02778 75297 9.6 4.03 7.445793538 43.57698397
2016 Michigan 72469.34307 52492 11.1 4.53 8.388002959 40.33742886
2016 Minnesota 68147.55556 65599 8.7 4.32 10.91506647 47.01874786
2016 Mississippi 93397.6875 41754 21.1 4.72 14.72442365 41.51469444
2016 Missouri 54891.89189 51746 13 4.87 10.4906931 44.73972059
2016 Montana 21719.16667 50027 11.7 4.39 9.429968182 35.76884483
2016 Nebraska 476779 56927 9.6 4.44 13.47830142 37.26353922
2016 Nevada 245004.8333 55180 10.1 4.33 10.70554412 39.35010811
2016 New Hampshire 88986.33333 70936 6.4 4.65 7.570755682 34.16546154
2016 New Jersey 70987.84921 76126 9.4 3.46 11.85669098 44.03913793
2016 New Mexico 26679.67949 46748 17.8 4.23 8.669464583 36.50300877
2016 New York 93579.56872 62909 11.9 3.74 9.091268877 37.18688512
2016 North Carolina 47414.8972 50584 13.6 4.85 11.3637241 43.87385652
2016 North Dakota 42108.44444 60656 11.1 4.04 13.52488393 36.06635714
2016 Ohio 81219.39161 52334 13.7 4.74 11.41566519 39.623657
2016 Oklahoma 91245.60465 49176 14.6 4.59 12.9651904 45.00560345
2016 Oregon 24960.15244 57532 11.8 4.83 6.810485738 39.02932212
2016 Pennsylvania 65560.13846 56907 11.1 4 11.12973499 43.64527474
2016 Rhode Island 32012.90909 60596 11.4 4.98 6.683648734 33.00051563
2016 South Carolina 53345.36559 49501 14.1 4.35 11.90332488 43.22786404
2016 South Dakota 39338.81818 54467 14.5 3.7 15.66702727 37.46463043
2016 Tennessee 59920.66667 48547 14.9 4.52 13.21981038 50.17518939
2016 Texas 188260.7838 56565 13.8 3.84 15.42752093 46.00019551
2016 Utah 34672.92045 65977 8.6 5.17 15.4128801 51.20211017
2016 Vermont 28390.63636 57677 9.6 5.28 6.25341 34.74116667
2016 Virginia 72515.58621 68114 11.4 3.98 10.31943112 38.48485092
2016 Washington 32105.72687 67106 11 4.72 7.138483202 45.15090625
2016 West Virginia 63141.44828 43385 18 5.53 11.62074367 39.91472826
2016 Wisconsin 49816.44828 56811 10.7 4.15 11.34243517 36.77260828
2016 Wyoming 39033.4 59882 10.9 4.3 12.73973913 34.47223529
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="histograms.js"></script>
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
#states {
stroke: #050505;
stroke-width: 1.5px;
}
#states text {
fill: gold;
stroke: none;
text-anchor:middle;
font-size: 10px;
font-weight:bold;
}
#states .active {
fill: steelblue !important;
}
#states text.active {
font: 10px sans-serif;
font-size: 10px;
font-weight:bold;
fill: #fff;
stroke-width: .5px;
fill: black !important;
stroke: #ffffff;
text-align:center;
}
svg {
font: 10px sans-serif;
font-weight:bold;
}
.background path {
fill: none;
stroke: lightgreen;
stroke-opacity: .4;
shape-rendering: crispEdges;
}
.foreground path {
fill: none;
stroke: rebeccapurple;
stroke-opacity: .7;
stroke-width: 1.5;
}
.brush .extent {
fill-opacity: .3;
stroke: #fff;
shape-rendering: crispEdges;
}
.axis line,
.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
text-shadow: 0 1px 0 #ffffff;
cursor: move;
}
</style>
<body>
<script>
var m = [30, 10, 10, 10],
w = 960 - m[1] - m[3],
h = 500 - m[0] - m[2];
var xGraph = d3.scale.ordinal().rangePoints([0, w], 1),
yGraph = {},
dragging = {};
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
background,
foreground;
var svgGraph = d3.select("body").append("svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
d3.csv("Final_Data3.csv", function(error, stateGraph) {
// Extract the list of dimensions and create a scale for each.
xGraph.domain(dimensions = d3.keys(stateGraph[0]).filter(function(d) {
return d != "name" && (yGraph[d] = d3.scale.linear()
.domain(d3.extent(stateGraph, function(p) { return +p[d]; }))
.range([h, 0]));
}));
// Create path for the background and foreground variables
// Add green background lines for context.
background = svgGraph.append("g")
.attr("class", "background")
.selectAll("path")
.data(stateGraph)
.enter().append("path")
.attr("d", path);
// Add rebeccapurple foreground lines for focus.
foreground = svgGraph.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(stateGraph)
.enter().append("path")
.attr("id", function(d) { return d.name.replace(" ","")})
.attr("d", path);
// Add a group element for each dimension.
var g = svgGraph.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + xGraph(d) + ")"; })
// Add behavior drag animation for moving the axis
// https://github.com/d3/d3-3.x-api-reference/blob/master/Behaviors.md
.call(d3.behavior.drag()
.on("dragstart", function(d) {
dragging[d] = this.__origin__ = xGraph(d);
background.attr("visibility", "hidden");
})
.on("drag", function(d) {
dragging[d] = Math.min(w, Math.max(0, this.__origin__ += d3.event.dx));
foreground.attr("d", path);
dimensions.sort(function(a, b) { return position(a) - position(b); });
xGraph.domain(dimensions);
g.attr("transform", function(d) { return "translate(" + position(d) + ")"; })
})
.on("dragend", function(d) {
delete this.__origin__;
delete dragging[d];
transition(d3.select(this)).attr("transform", "translate(" + xGraph(d) + ")");
transition(foreground)
.attr("d", path);
background
.attr("d", path)
.transition()
.delay(500)
.duration(0)
.attr("visibility", null);
}));
// Add an axis and title.
g.append("g")
.attr("class", "axis")
.each(function(d) { d3.select(this).call(axis.scale(yGraph[d])); })
.append("text")
.attr("text-anchor", "middle")
.attr("y", -9)
.text(String);
// Start of brush code
// Add and store a brush for each axis.
g.append("g")
.attr("class", "brush")
.each(function(d) { d3.select(this).call(yGraph[d].brush = d3.svg.brush().y(yGraph[d]).on("brushstart", brushstart).on("brush", brush)); })
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
});
function position(d) {
var v = dragging[d];
return v == null ? xGraph(d) : v;
}
function transition(g) {
return g.transition().duration(500);
}
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p) { return [position(p), yGraph[p](d[p])]; }));
}
// When brushing, don’t trigger axis dragging.
function brushstart() {
d3.event.sourceEvent.stopPropagation();
}
// Handles a brush event, toggling the display of foreground lines.
// We can filter a selection using .filter. A function is usually passed into .filter which returns true if the element should be included. .filter returns the filtered selection
function brush() {
var actives = dimensions.filter(function(p) { return !yGraph[p].brush.empty(); }),
extents = actives.map(function(p) { return yGraph[p].brush.extent(); });
foreground.style("display", function(d) {
return actives.every(function(p, i) {
return extents[i][0] <= d[p] && d[p] <= extents[i][1];
}) ? null : "none";
});
}
// .extent Returns the minimum and maximum value in the given (array, brush, domain) using natural order.
<!-- This is where the code starts for the map drawning -->
var width = 960,
height =500,
centered;
var projection = d3.geo.albersUsa()
.scale(width)
.translate([0, 0]);
var pathMap = d3.geo.path()
.projection(projection);
var svgMap = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svgMap.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", click);
var gMap = svgMap.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.append("g")
.attr("id", "states");
UFO_color = [];
var color_by = "total";
var color = d3.scale.log()
.range(["rebeccapurple", "mintcream"]);
d3.csv("UFO_Color2.csv", function(data){
data.forEach(function(d){
d["total"] = +d["total"];
})
color.domain([d3.min(data, function(d){ return d.total}),
d3.max(data,function(d) {return d.total; })]);
d3.json("states.json", function(json) {
data.forEach(function(d){
//console.log('d:', d);
//console.log("json:", json);
json.features.forEach(function(s){
//console.log('s', s);
if (d.state == s.properties.abbr){
s.total = d.total
s.avg = d.avg;
}
})
})
var states = gMap.selectAll("pathMap")
.data(json.features)
.enter().append("path")
.attr("d", pathMap)
.attr("id", function(d) { return d.properties.abbr; })
.style("fill", function(d) { return color(d.total); })
.on("click", click)
var labels = gMap.selectAll("text")
.data(json.features)
.enter().append("text")
.attr("transform", function(d) { return "translate(" + pathMap.centroid(d) + ")"; })
.attr("id", function(d) { return 'label-'+d.properties.abbr; })
.attr("dy", ".35em")
.on("click", click)
.text(function(d) { return d.properties.abbr; });
});
})
function click(d) {
var x = 0,
y = 0,
k = 1;
if (d && centered !== d) {
var centroid = pathMap.centroid(d);
x = -centroid[0];
y = -centroid[1];
k = 4;
centered = d;
} else {
centered = null;
}
// Dr. Cresawns code.... THANKS! :P
// Visibility = boolean ... kinda
if (centered) { console.log(d.properties.name) }
else {
d3.selectAll(".foreground").selectAll("path").style("visibility", "visible");
}
//console.log("eachstate:", eachstate);
if (centered) {
d3.selectAll(".foreground").selectAll("path").style("visibility", "hidden");
d3.selectAll(".foreground").selectAll("path#" + d.properties.name.replace(" ","")).style("visibility", "visible");
}
gMap.selectAll("pathMap")
.classed("active", centered && function(d) { return d === centered; });
gMap.selectAll("text")
.text(function(d) { return d.properties.abbr; })
.classed("active",false);
if (centered) {
gMap.select("#label-"+centered.properties.abbr)
.text(function(d) { return d.properties.name+'[ '+ '1:' +( Math.round(d.total) + ' ]' +' '+ 'Avg Pop:' + Math.round(d.avg) ||'(none)'); })
.classed("active", centered && function(d) { return d === centered; });
}
//Zooming Function
gMap.transition()
.duration(1000)
.attr("transform", "scale(" + k + ")translate(" + x + "," + y + ")")
.style("stroke-width", 1.5 / k + "px");
}
//connect graph to state "each one", number of sighting to popup in each state.
</script>
</body>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
state total avg
al 14961.3179 4847467
ak 4267.611272 738296.75
az 6487.846264 6773311.5
ar 17487.75882 2972919
ca 13139.80789 38814992.5
co 7705.640157 5401653.75
ct 9174.589514 3587264.5
dc 9681.659794 939121
de 39113.48529 664929.25
fl 10030.98352 20082029
ga 16390.12036 10145484.5
hi 7928.134078 1419136
id 6971.939619 1645377.75
il 19117.42374 12846908.75
in 14575.14128 6602539
ia 8698.777933 3114162.5
ks 13309.85206 2901547.75
ky 14393.41938 4418779.75
la 17245.28519 4656227
me 5096.481801 1330181.75
md 12718.00745 5977463.5
ma 10437.00463 6763179
mi 14776.7377 9915191
mn 11862.26518 5468504.25
ms 19291.93226 2990249.5
mo 13049.91667 6068211.25
mt 4715.337156 1027943.5
ne 41947.69444 1887646.25
nv 69776.17683 2860823.25
nh 14605.31868 1329084
nj 15206.15545 8926013.25
nm 6018.468208 2082390
ny 18209.72599 19721133.25
nc 11403.52825 9989490.75
nd 7092.166667 744677.5
oh 11642.54342 11595973.25
ok 13649.90263 3890222.25
or 5067.158544 4003055.25
pa 11468.16906 12787008.5
ri 7127.611486 1054886.5
sc 7907.429675 4863069.25
sd 11402.85333 855214
tn 13383.77495 6571433.5
tx 30709.1839 27177627.75
ut 9143.344615 2971587
vt 3841.726994 626201.5
va 12830.56115 8339864.75
wa 6026.776461 7117623
wv 9036.642157 1843475
wi 10108.69737 5761957.5
wy 8119.381944 584595.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment