Created
March 18, 2020 20:17
-
-
Save Alex-Devoid/91427aebd7bb2f34a6aef60bbdde65d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <style> | |
| .counties { | |
| fill: none; | |
| } | |
| .states { | |
| fill: none; | |
| stroke: #fff; | |
| stroke-linejoin: round; | |
| } | |
| .d3-tip { | |
| line-height: 1; | |
| padding: 6px; | |
| background: rgba(0, 0, 0, 0.8); | |
| color: #fff; | |
| border-radius: 4px; | |
| font-size: 12px; | |
| } | |
| /* Creates a small triangle extender for the tooltip */ | |
| .d3-tip:after { | |
| box-sizing: border-box; | |
| display: inline; | |
| font-size: 10px; | |
| width: 100%; | |
| line-height: 1; | |
| color: rgba(0, 0, 0, 0.8); | |
| content: "\25BC"; | |
| position: absolute; | |
| text-align: center; | |
| } | |
| /* Style northward tooltips specifically */ | |
| .d3-tip.n:after { | |
| margin: -2px 0 0 0; | |
| top: 100%; | |
| left: 0; | |
| } | |
| </style> | |
| <svg viewBox="0 0 960 600"></svg> | |
| <defs> | |
| <marker id="arrow" markerWidth="10" markerHeight="10" refX="0" refY="3" orient="auto" markerUnits="strokeWidth"> | |
| <path d="M0,0 L0,6 L9,3 z" fill="black" /> | |
| </marker> | |
| </defs> | |
| <!-- <line x1="0" y1="0" x2="200" y2="50" stroke="red" stroke-width="2" marker-end="url(#arrow)"/> --> | |
| <!-- <path d="M20,70 T80,100 T160,80 T200,90" fill="white" stroke="red" stroke-width="2" marker-start="url(#arrow)" marker-mid="url(#arrow)" marker-end="url(#arrow)"/> --> | |
| </svg> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <script src="https://d3js.org/topojson.v2.min.js"></script> | |
| <script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.25.6/d3-legend.min.js"></script> | |
| <!-- <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script> --> | |
| <script type="text/javascript"> | |
| // d3.tip | |
| // Copyright (c) 2013 Justin Palmer | |
| // ES6 / D3 v4 Adaption Copyright (c) 2016 Constantin Gavrilete | |
| // Removal of ES6 for D3 v4 Adaption Copyright (c) 2016 David Gotz | |
| // | |
| // Tooltips for d3.js SVG visualizations | |
| d3.functor = function functor(v) { | |
| return typeof v === "function" ? v : function() { | |
| return v; | |
| }; | |
| }; | |
| d3.tip = function() { | |
| var direction = d3_tip_direction, | |
| offset = d3_tip_offset, | |
| html = d3_tip_html, | |
| node = initNode(), | |
| svg = null, | |
| point = null, | |
| target = null | |
| function tip(vis) { | |
| svg = getSVGNode(vis) | |
| point = svg.createSVGPoint() | |
| document.body.appendChild(node) | |
| } | |
| // Public - show the tooltip on the screen | |
| // | |
| // Returns a tip | |
| tip.show = function() { | |
| var args = Array.prototype.slice.call(arguments) | |
| if(args[args.length - 1] instanceof SVGElement) target = args.pop() | |
| var content = html.apply(this, args), | |
| poffset = offset.apply(this, args), | |
| dir = direction.apply(this, args), | |
| nodel = getNodeEl(), | |
| i = directions.length, | |
| coords, | |
| scrollTop = document.documentElement.scrollTop || document.body.scrollTop, | |
| scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft | |
| nodel.html(content) | |
| .style('position', 'absolute') | |
| .style('opacity', 1) | |
| .style('pointer-events', 'all') | |
| while(i--) nodel.classed(directions[i], false) | |
| coords = direction_callbacks[dir].apply(this) | |
| nodel.classed(dir, true) | |
| .style('top', (coords.top + poffset[0]) + scrollTop + 'px') | |
| .style('left', (coords.left + poffset[1]) + scrollLeft + 'px') | |
| return tip | |
| } | |
| // Public - hide the tooltip | |
| // | |
| // Returns a tip | |
| tip.hide = function() { | |
| var nodel = getNodeEl() | |
| nodel | |
| .style('opacity', 0) | |
| .style('pointer-events', 'none') | |
| return tip | |
| } | |
| // Public: Proxy attr calls to the d3 tip container. Sets or gets attribute value. | |
| // | |
| // n - name of the attribute | |
| // v - value of the attribute | |
| // | |
| // Returns tip or attribute value | |
| tip.attr = function(n, v) { | |
| if (arguments.length < 2 && typeof n === 'string') { | |
| return getNodeEl().attr(n) | |
| } else { | |
| var args = Array.prototype.slice.call(arguments) | |
| d3.selection.prototype.attr.apply(getNodeEl(), args) | |
| } | |
| return tip | |
| } | |
| // Public: Proxy style calls to the d3 tip container. Sets or gets a style value. | |
| // | |
| // n - name of the property | |
| // v - value of the property | |
| // | |
| // Returns tip or style property value | |
| tip.style = function(n, v) { | |
| // debugger; | |
| if (arguments.length < 2 && typeof n === 'string') { | |
| return getNodeEl().style(n) | |
| } else { | |
| var args = Array.prototype.slice.call(arguments); | |
| if (args.length === 1) { | |
| var styles = args[0]; | |
| Object.keys(styles).forEach(function(key) { | |
| return d3.selection.prototype.style.apply(getNodeEl(), [key, styles[key]]); | |
| }); | |
| } | |
| } | |
| return tip | |
| } | |
| // Public: Set or get the direction of the tooltip | |
| // | |
| // v - One of n(north), s(south), e(east), or w(west), nw(northwest), | |
| // sw(southwest), ne(northeast) or se(southeast) | |
| // | |
| // Returns tip or direction | |
| tip.direction = function(v) { | |
| if (!arguments.length) return direction | |
| direction = v == null ? v : d3.functor(v) | |
| return tip | |
| } | |
| // Public: Sets or gets the offset of the tip | |
| // | |
| // v - Array of [x, y] offset | |
| // | |
| // Returns offset or | |
| tip.offset = function(v) { | |
| if (!arguments.length) return offset | |
| offset = v == null ? v : d3.functor(v) | |
| return tip | |
| } | |
| // Public: sets or gets the html value of the tooltip | |
| // | |
| // v - String value of the tip | |
| // | |
| // Returns html value or tip | |
| tip.html = function(v) { | |
| if (!arguments.length) return html | |
| html = v == null ? v : d3.functor(v) | |
| return tip | |
| } | |
| // Public: destroys the tooltip and removes it from the DOM | |
| // | |
| // Returns a tip | |
| tip.destroy = function() { | |
| if(node) { | |
| getNodeEl().remove(); | |
| node = null; | |
| } | |
| return tip; | |
| } | |
| function d3_tip_direction() { return 'n' } | |
| function d3_tip_offset() { return [0, 0] } | |
| function d3_tip_html() { return ' ' } | |
| var direction_callbacks = { | |
| n: direction_n, | |
| s: direction_s, | |
| e: direction_e, | |
| w: direction_w, | |
| nw: direction_nw, | |
| ne: direction_ne, | |
| sw: direction_sw, | |
| se: direction_se | |
| }; | |
| var directions = Object.keys(direction_callbacks); | |
| function direction_n() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.n.y - node.offsetHeight, | |
| left: bbox.n.x - node.offsetWidth / 2 | |
| } | |
| } | |
| function direction_s() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.s.y, | |
| left: bbox.s.x - node.offsetWidth / 2 | |
| } | |
| } | |
| function direction_e() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.e.y - node.offsetHeight / 2, | |
| left: bbox.e.x | |
| } | |
| } | |
| function direction_w() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.w.y - node.offsetHeight / 2, | |
| left: bbox.w.x - node.offsetWidth | |
| } | |
| } | |
| function direction_nw() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.nw.y - node.offsetHeight, | |
| left: bbox.nw.x - node.offsetWidth | |
| } | |
| } | |
| function direction_ne() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.ne.y - node.offsetHeight, | |
| left: bbox.ne.x | |
| } | |
| } | |
| function direction_sw() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.sw.y, | |
| left: bbox.sw.x - node.offsetWidth | |
| } | |
| } | |
| function direction_se() { | |
| var bbox = getScreenBBox() | |
| return { | |
| top: bbox.se.y, | |
| left: bbox.e.x | |
| } | |
| } | |
| function initNode() { | |
| var node = d3.select(document.createElement('div')) | |
| node | |
| .style('position', 'absolute') | |
| .style('top', '0') | |
| .style('opacity', '0') | |
| .style('pointer-events', 'none') | |
| .style('box-sizing', 'border-box') | |
| return node.node() | |
| } | |
| function getSVGNode(el) { | |
| el = el.node() | |
| if(el.tagName.toLowerCase() === 'svg') | |
| return el | |
| return el.ownerSVGElement | |
| } | |
| function getNodeEl() { | |
| if(node === null) { | |
| node = initNode(); | |
| // re-add node to DOM | |
| document.body.appendChild(node); | |
| }; | |
| return d3.select(node); | |
| } | |
| // Private - gets the screen coordinates of a shape | |
| // | |
| // Given a shape on the screen, will return an SVGPoint for the directions | |
| // n(north), s(south), e(east), w(west), ne(northeast), se(southeast), nw(northwest), | |
| // sw(southwest). | |
| // | |
| // +-+-+ | |
| // | | | |
| // + + | |
| // | | | |
| // +-+-+ | |
| // | |
| // Returns an Object {n, s, e, w, nw, sw, ne, se} | |
| function getScreenBBox() { | |
| var targetel = target || d3.event.target; | |
| while ('undefined' === typeof targetel.getScreenCTM && 'undefined' === targetel.parentNode) { | |
| targetel = targetel.parentNode; | |
| } | |
| var bbox = {}, | |
| matrix = targetel.getScreenCTM(), | |
| tbbox = targetel.getBBox(), | |
| width = tbbox.width, | |
| height = tbbox.height, | |
| x = tbbox.x, | |
| y = tbbox.y | |
| point.x = x | |
| point.y = y | |
| bbox.nw = point.matrixTransform(matrix) | |
| point.x += width | |
| bbox.ne = point.matrixTransform(matrix) | |
| point.y += height | |
| bbox.se = point.matrixTransform(matrix) | |
| point.x -= width | |
| bbox.sw = point.matrixTransform(matrix) | |
| point.y -= height / 2 | |
| bbox.w = point.matrixTransform(matrix) | |
| point.x += width | |
| bbox.e = point.matrixTransform(matrix) | |
| point.x -= width / 2 | |
| point.y -= height / 2 | |
| bbox.n = point.matrixTransform(matrix) | |
| point.y += height | |
| bbox.s = point.matrixTransform(matrix) | |
| return bbox | |
| } | |
| return tip | |
| }; | |
| </script> | |
| <script> | |
| // Datasets | |
| // FIP codes | |
| // https://www2.census.gov/geo/docs/reference/codes/files/national_county.txt | |
| // https://en.wikipedia.org/wiki/Federal_Information_Processing_Standard_state_code | |
| // | |
| // Unemployment rates | |
| // https://www.bls.gov/lau/#tables | |
| // https://bl.ocks.org/mbostock/4060606 | |
| var svg = d3.select("svg"), | |
| width =850, | |
| height = 600; | |
| // var path = d3.geoPath(); | |
| var tip = d3.tip() | |
| .attr('class', 'd3-tip') | |
| .offset([-10, 0]) | |
| .direction('n') | |
| .html(function(d) { | |
| console.log(d); | |
| return "Cases: "+d.properties.cases+'<br/>'+ d.properties.per100000Cases+ 'cases per 100,000 people.' | |
| }); | |
| svg.call(tip); | |
| var projection = d3.geoAlbersUsa() | |
| .translate([width/2, height/2]) | |
| .scale([1000]); | |
| //Define default path generator | |
| var path = d3.geoPath() | |
| .projection(projection); | |
| d3.queue() | |
| .defer(d3.json, "acs2018_1yr_B01003_04000US55.geojson") | |
| .defer(d3.json, "https://d3js.org/us-10m.v1.json") | |
| // .defer(d3.tsv, "unemployment.tsv", function(d) { unemployment.set(d.id, +d.rate); }) | |
| .defer(d3.csv, "https://www.cdc.gov/coronavirus/2019-ncov/map-data-cases.csv") | |
| .await(ready); | |
| function ready(error, us1,us, data) { | |
| if (error) throw error; | |
| var casesMax = getMax(data,'Cases Reported') | |
| var pos = us1.features.map(function(e) { return e.properties.name; }).indexOf(casesMax.Name); | |
| var per100000max = (casesMax.number/us1.features[pos].properties.B01003001) * 100000 | |
| var x = d3.scaleLinear() | |
| .domain([0, per100000max]) | |
| .rangeRound([600, 860]); | |
| var color = d3.scaleLinear() | |
| .domain([0, Math.ceil(per100000max)]) | |
| .range(['white','#08306A']); | |
| svg.append("g") | |
| .attr("class", "legendLinear") | |
| .attr("transform", "translate(20,20)"); | |
| var legendLinear = d3.legendColor() | |
| .shapeWidth(30) | |
| .orient('horizontal') | |
| .scale(color); | |
| svg.select(".legendLinear") | |
| .call(legendLinear); | |
| function getMax(arr, prop) { | |
| var max; | |
| for (var i=0 ; i<arr.length ; i++) { | |
| var num = Number(arr[i][prop]) | |
| arr[i]['number'] = (!isNaN(num))? parseInt(arr[i][prop]): | |
| (arr[i][prop] === 'None')? 0: Number(arr[i][prop].split(' ')[2]); | |
| if (max == null || arr[i]['number'] > max['number']) | |
| max = arr[i]; | |
| } | |
| return max; | |
| } | |
| function getColor(obj,dataCorona){ | |
| for (var i = 0; i < dataCorona.length; i++) { | |
| if (dataCorona[i].Name === obj.properties.name) { | |
| var cases = Number(dataCorona[i]['Cases Reported']) | |
| if (dataCorona[i]['Cases Reported'] === 'None') { | |
| return 0; | |
| }else if (isNaN(cases)) { | |
| // console.log(cases.match(/\d+/)[0]); | |
| var c = dataCorona[i]['Cases Reported'].split(' ') | |
| var firstNum = Number(c[0]); | |
| var lastNum = Number(c[2]); | |
| // https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-88.php | |
| const median = arr => { | |
| const mid = Math.floor(arr.length / 2), | |
| nums = [...arr].sort((a, b) => a - b); | |
| return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; | |
| }; | |
| var medianCases = (median([firstNum,lastNum])/obj.properties.B01003001) * 100000 | |
| console.log(medianCases); | |
| return medianCases; | |
| }else { | |
| // console.log(cases); | |
| per100000Cases = (cases/obj.properties.B01003001) * 100000 | |
| return per100000Cases; | |
| } | |
| } | |
| } | |
| } | |
| var w = 70, h = 300; | |
| var key = d3.select("svg") | |
| // .append("svg") | |
| // .attr("x", 60) | |
| // .attr("y",30) | |
| // .attr("width", w) | |
| // .attr("height", h) | |
| // .attr("class", "legend"); | |
| var legend = key.append("defs") | |
| .append("svg:linearGradient") | |
| .attr("id", "gradient") | |
| .attr("x1", "100%") | |
| .attr("y1", "0%") | |
| .attr("x2", "100%") | |
| .attr("y2", "100%") | |
| .attr("spreadMethod", "pad"); | |
| legend.append("stop") | |
| .attr("offset", "0%") | |
| .attr("stop-color", '#08306A') | |
| .attr("stop-opacity", 1); | |
| legend.append("stop") | |
| .attr("offset", "100%") | |
| .attr("stop-color", '#FFFF') | |
| .attr("stop-opacity", 1); | |
| key.append("rect") | |
| .attr("transform", "translate(10,30)") | |
| .attr("width", 20) | |
| .attr("height", h) | |
| .style("fill", "url(#gradient)") | |
| // .attr("transform", "translate(0,10)"); | |
| var y = d3.scaleLinear() | |
| .range([h, 0]) | |
| .domain([0, Math.ceil(per100000max)]); | |
| var yAxis = d3.axisRight(y); | |
| key.append("g") | |
| .attr("class", "y axis") | |
| .attr("transform", "translate(30,30)") | |
| .call(yAxis) | |
| console.log(data); | |
| function legendState(state){ | |
| var posArizona = data.map(function(e) { return e.Name; }).indexOf(state); | |
| var posArizonaPop = us1.features.map(function(e) { return e.properties.name; }).indexOf(state); | |
| var ArizonaPer100000Cases = (data[posArizona]['Cases Reported']/us1.features[posArizonaPop].properties.B01003001) * 100000 | |
| return ArizonaPer100000Cases | |
| } | |
| key.append("line") | |
| .attr("class", "Arizona") | |
| .attr("x", 35) | |
| // .attr("y", function(d){ | |
| // | |
| // var apc = legendState(d["Name"]) | |
| // | |
| // return y(apc)+30 | |
| // | |
| // }) | |
| .attr("width", 12) | |
| .style("fill", "black") | |
| svg.append("g") | |
| .selectAll(".namesLeg") | |
| .data(data) | |
| .enter().append("line") | |
| .attr("class", function(d){ | |
| return "namesLeg " + d.Name; | |
| }) | |
| .attr("x1", 25) | |
| .attr("y1", function(d){ | |
| var n = ['American Samoa','Guam','Northern Marianas','Puerto Rico','Virgin Islands','Micronesia','Palau','Marshall Islands'].includes(d["Name"]); | |
| console.log(d["Name"]); | |
| if (!n) { | |
| var apc = legendState(d["Name"]) | |
| return y(apc)+30 | |
| } | |
| }) | |
| .attr("x2", 75) | |
| .attr("y2", function(d){ | |
| var n = ['American Samoa','Guam','Northern Marianas','Puerto Rico','Virgin Islands','Micronesia','Palau','Marshall Islands'].includes(d["Name"]); | |
| console.log(d["Name"]); | |
| if (!n) { | |
| var apc = legendState(d["Name"]) | |
| return y(apc)+30 | |
| } | |
| }) | |
| .attr("stroke-width", 2) | |
| .attr("stroke", "black") | |
| .attr("marker-end","url(#arrow)") | |
| .attr('opacity',0); | |
| svg.append("g") | |
| .selectAll(".namesLegText") | |
| .data(data).enter() | |
| .append('text') | |
| .attr("class", function(d){ | |
| return "namesLegText " + d.Name; | |
| }) | |
| .text(function(d){ return d['Name']}) | |
| .style("fill", "black") | |
| .attr('font-size', '12px') | |
| .attr('text-anchor', "start") | |
| .attr('y', function(d){ | |
| var n = ['American Samoa','Guam','Northern Marianas','Puerto Rico','Virgin Islands','Micronesia','Palau','Marshall Islands'].includes(d["Name"]); | |
| console.log(d["Name"]); | |
| if (!n) { | |
| var apc = legendState(d["Name"]) | |
| return y(apc)+28 | |
| } | |
| }).attr('x', 25) | |
| .attr('opacity',0); | |
| // add legend info | |
| svg.append('text') | |
| .text('Most Unequal') | |
| .attr('font-size', '12px') | |
| .attr('y', 40) | |
| .attr('x', 15) | |
| svg.append('text') | |
| .text('Gini Coefficient') | |
| .attr('font-size', '10px') | |
| .attr('y', 65) | |
| .attr('x', 85) | |
| var colorScale = d3.scaleLinear().domain([1,per100000max]) | |
| .range(d3.schemeBlues[9]) | |
| svg.append("g") | |
| .attr("class", "counties") | |
| .selectAll("path") | |
| // .data(topojson.feature(us, us.objects.states).features) | |
| // .data(topojson.feature(us, us.objects.counties).features) | |
| .data(us1.features) | |
| .enter().append("path") | |
| .attr('stroke',"grey") | |
| .attr("fill", function(d) { | |
| // console.log(d); | |
| var num = getColor(d,data) | |
| var pos1 = data.map(function(e) { return e.Name; }).indexOf(d.properties.name); | |
| d.properties.per100000Cases = num | |
| d.properties.cases = data[pos1]['Cases Reported'] | |
| return color(num); | |
| }) | |
| .attr("d", path) | |
| .on('mouseover',function(d){ | |
| d3.selectAll(".namesLeg").filter(`.${d.properties.name}`) | |
| .attr('opacity',1); | |
| d3.selectAll(".namesLegText").filter(`.${d.properties.name}`) | |
| .attr('opacity',1); | |
| return tip.show(d) | |
| } | |
| ) | |
| // .on('mouseover', function (d) { | |
| // var target = d3.select(this) | |
| // .attr('x', d3.event.offsetX) | |
| // .attr('y', d3.event.offsetY - 5) // 5 pixels above the cursor | |
| // .node(); | |
| // tip.show(d, target); | |
| // }) | |
| .on('mouseout', function(d){ | |
| d3.selectAll(".namesLeg").filter(`.${d.properties.name}`) | |
| .attr('opacity',0); | |
| d3.selectAll(".namesLegText").filter(`.${d.properties.name}`) | |
| .attr('opacity',0); | |
| return tip.hide(d) | |
| }) | |
| .append("title") // Tooltip | |
| .text(function(d) { return d.properties.B01003001 + "%"; }); | |
| // svg.append("path") | |
| // .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) | |
| // .attr("class", "states") | |
| // .attr("d", path); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment