Searches features using Awesomplete. Adapted from https://bl.ocks.org/mbostock/5144735.
-
-
Save madams1/16b5964a9d94df492a498294b40da892 to your computer and use it in GitHub Desktop.
Kentucky Population Density - Searchable
This file contains 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
license: gpl-3.0 | |
height: 600 |
This file contains 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
.awesomplete [hidden] { | |
display: none; | |
} | |
.awesomplete .visually-hidden { | |
position: absolute; | |
clip: rect(0, 0, 0, 0); | |
} | |
.awesomplete { | |
display: inline-block; | |
position: relative; | |
} | |
.awesomplete > input { | |
display: block; | |
} | |
.awesomplete > ul { | |
position: absolute; | |
left: 0; | |
z-index: 1; | |
min-width: 100%; | |
box-sizing: border-box; | |
list-style: none; | |
padding: 0; | |
margin: 0; | |
background: #fff; | |
} | |
.awesomplete > ul:empty { | |
display: none; | |
} | |
.awesomplete > ul { | |
border-radius: .3em; | |
margin: .2em 0 0; | |
background: hsla(0,0%,100%,.9); | |
background: linear-gradient(to bottom right, white, hsla(0,0%,100%,.8)); | |
border: 1px solid rgba(0,0,0,.3); | |
box-shadow: .05em .2em .6em rgba(0,0,0,.2); | |
text-shadow: none; | |
} | |
@supports (transform: scale(0)) { | |
.awesomplete > ul { | |
transition: .3s cubic-bezier(.4,.2,.5,1.4); | |
transform-origin: 1.43em -.43em; | |
} | |
.awesomplete > ul[hidden], | |
.awesomplete > ul:empty { | |
opacity: 0; | |
transform: scale(0); | |
display: block; | |
transition-timing-function: ease; | |
} | |
} | |
/* Pointer */ | |
.awesomplete > ul:before { | |
content: ""; | |
position: absolute; | |
top: -.43em; | |
left: 1em; | |
width: 0; height: 0; | |
padding: .4em; | |
background: white; | |
border: inherit; | |
border-right: 0; | |
border-bottom: 0; | |
-webkit-transform: rotate(45deg); | |
transform: rotate(45deg); | |
} | |
.awesomplete > ul > li { | |
position: relative; | |
padding: .2em .5em; | |
cursor: pointer; | |
} | |
.awesomplete > ul > li:hover { | |
background: hsl(200, 40%, 80%); | |
color: black; | |
} | |
.awesomplete > ul > li[aria-selected="true"] { | |
background: hsl(205, 40%, 40%); | |
color: white; | |
} | |
.awesomplete mark { | |
background: hsl(65, 100%, 50%); | |
} | |
.awesomplete li:hover mark { | |
background: hsl(68, 100%, 41%); | |
} | |
.awesomplete li[aria-selected="true"] mark { | |
background: hsl(86, 100%, 21%); | |
color: inherit; | |
} | |
/*# sourceMappingURL=awesomplete.css.map */ |
This file contains 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
// Awesomplete - Lea Verou - MIT license | |
!function(){function t(t){var e=Array.isArray(t)?{label:t[0],value:t[1]}:"object"==typeof t&&"label"in t&&"value"in t?t:{label:t,value:t};this.label=e.label||e.value,this.value=e.value}function e(t,e,i){for(var n in e){var s=e[n],r=t.input.getAttribute("data-"+n.toLowerCase());"number"==typeof s?t[n]=parseInt(r):!1===s?t[n]=null!==r:s instanceof Function?t[n]=null:t[n]=r,t[n]||0===t[n]||(t[n]=n in i?i[n]:s)}}function i(t,e){return"string"==typeof t?(e||document).querySelector(t):t||null}function n(t,e){return o.call((e||document).querySelectorAll(t))}function s(){n("input.awesomplete").forEach(function(t){new r(t)})}var r=function(t,n){var s=this;this.isOpened=!1,this.input=i(t),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("aria-autocomplete","list"),n=n||{},e(this,{minChars:2,maxItems:10,autoFirst:!1,data:r.DATA,filter:r.FILTER_CONTAINS,sort:!1!==n.sort&&r.SORT_BYLENGTH,item:r.ITEM,replace:r.REPLACE},n),this.index=-1,this.container=i.create("div",{className:"awesomplete",around:t}),this.ul=i.create("ul",{hidden:"hidden",inside:this.container}),this.status=i.create("span",{className:"visually-hidden",role:"status","aria-live":"assertive","aria-relevant":"additions",inside:this.container}),this._events={input:{input:this.evaluate.bind(this),blur:this.close.bind(this,{reason:"blur"}),keydown:function(t){var e=t.keyCode;s.opened&&(13===e&&s.selected?(t.preventDefault(),s.select()):27===e?s.close({reason:"esc"}):38!==e&&40!==e||(t.preventDefault(),s[38===e?"previous":"next"]()))}},form:{submit:this.close.bind(this,{reason:"submit"})},ul:{mousedown:function(t){var e=t.target;if(e!==this){for(;e&&!/li/i.test(e.nodeName);)e=e.parentNode;e&&0===t.button&&(t.preventDefault(),s.select(e,t.target))}}}},i.bind(this.input,this._events.input),i.bind(this.input.form,this._events.form),i.bind(this.ul,this._events.ul),this.input.hasAttribute("list")?(this.list="#"+this.input.getAttribute("list"),this.input.removeAttribute("list")):this.list=this.input.getAttribute("data-list")||n.list||[],r.all.push(this)};r.prototype={set list(t){if(Array.isArray(t))this._list=t;else if("string"==typeof t&&t.indexOf(",")>-1)this._list=t.split(/\s*,\s*/);else if((t=i(t))&&t.children){var e=[];o.apply(t.children).forEach(function(t){if(!t.disabled){var i=t.textContent.trim(),n=t.value||i,s=t.label||i;""!==n&&e.push({label:s,value:n})}}),this._list=e}document.activeElement===this.input&&this.evaluate()},get selected(){return this.index>-1},get opened(){return this.isOpened},close:function(t){this.opened&&(this.ul.setAttribute("hidden",""),this.isOpened=!1,this.index=-1,i.fire(this.input,"awesomplete-close",t||{}))},open:function(){this.ul.removeAttribute("hidden"),this.isOpened=!0,this.autoFirst&&-1===this.index&&this.goto(0),i.fire(this.input,"awesomplete-open")},destroy:function(){i.unbind(this.input,this._events.input),i.unbind(this.input.form,this._events.form);var t=this.container.parentNode;t.insertBefore(this.input,this.container),t.removeChild(this.container),this.input.removeAttribute("autocomplete"),this.input.removeAttribute("aria-autocomplete");var e=r.all.indexOf(this);-1!==e&&r.all.splice(e,1)},next:function(){var t=this.ul.children.length;this.goto(this.index<t-1?this.index+1:t?0:-1)},previous:function(){var t=this.ul.children.length,e=this.index-1;this.goto(this.selected&&-1!==e?e:t-1)},goto:function(t){var e=this.ul.children;this.selected&&e[this.index].setAttribute("aria-selected","false"),this.index=t,t>-1&&e.length>0&&(e[t].setAttribute("aria-selected","true"),this.status.textContent=e[t].textContent,this.ul.scrollTop=e[t].offsetTop-this.ul.clientHeight+e[t].clientHeight,i.fire(this.input,"awesomplete-highlight",{text:this.suggestions[this.index]}))},select:function(t,e){if(t?this.index=i.siblingIndex(t):t=this.ul.children[this.index],t){var n=this.suggestions[this.index];i.fire(this.input,"awesomplete-select",{text:n,origin:e||t})&&(this.replace(n),this.close({reason:"select"}),i.fire(this.input,"awesomplete-selectcomplete",{text:n}))}},evaluate:function(){var e=this,i=this.input.value;i.length>=this.minChars&&this._list.length>0?(this.index=-1,this.ul.innerHTML="",this.suggestions=this._list.map(function(n){return new t(e.data(n,i))}).filter(function(t){return e.filter(t,i)}),!1!==this.sort&&(this.suggestions=this.suggestions.sort(this.sort)),this.suggestions=this.suggestions.slice(0,this.maxItems),this.suggestions.forEach(function(t){e.ul.appendChild(e.item(t,i))}),0===this.ul.children.length?this.close({reason:"nomatches"}):this.open()):this.close({reason:"nomatches"})}},r.all=[],r.FILTER_CONTAINS=function(t,e){return RegExp(i.regExpEscape(e.trim()),"i").test(t)},r.FILTER_STARTSWITH=function(t,e){return RegExp("^"+i.regExpEscape(e.trim()),"i").test(t)},r.SORT_BYLENGTH=function(t,e){return t.length!==e.length?t.length-e.length:t<e?-1:1},r.ITEM=function(t,e){return i.create("li",{innerHTML:""===e.trim()?t:t.replace(RegExp(i.regExpEscape(e.trim()),"gi"),"<mark>$&</mark>"),"aria-selected":"false"})},r.REPLACE=function(t){this.input.value=t.value},r.DATA=function(t){return t},Object.defineProperty(t.prototype=Object.create(String.prototype),"length",{get:function(){return this.label.length}}),t.prototype.toString=t.prototype.valueOf=function(){return""+this.label};var o=Array.prototype.slice;i.create=function(t,e){var n=document.createElement(t);for(var s in e){var r=e[s];if("inside"===s)i(r).appendChild(n);else if("around"===s){var o=i(r);o.parentNode.insertBefore(n,o),n.appendChild(o)}else s in n?n[s]=r:n.setAttribute(s,r)}return n},i.bind=function(t,e){if(t)for(var i in e){var n=e[i];i.split(/\s+/).forEach(function(e){t.addEventListener(e,n)})}},i.unbind=function(t,e){if(t)for(var i in e){var n=e[i];i.split(/\s+/).forEach(function(e){t.removeEventListener(e,n)})}},i.fire=function(t,e,i){var n=document.createEvent("HTMLEvents");n.initEvent(e,!0,!0);for(var s in i)n[s]=i[s];return t.dispatchEvent(n)},i.regExpEscape=function(t){return t.replace(/[-\\^$*+?.()|[\]{}]/g,"\\$&")},i.siblingIndex=function(t){for(var e=0;t=t.previousElementSibling;e++);return e},"undefined"!=typeof Document&&("loading"!==document.readyState?s():document.addEventListener("DOMContentLoaded",s)),r.$=i,r.$$=n,"undefined"!=typeof self&&(self.Awesomplete=r),"object"==typeof module&&module.exports&&(module.exports=r)}(); | |
//# sourceMappingURL=awesomplete.min.js.map |
This file contains 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> | |
<meta charset="utf-8"> | |
<head> | |
<style> | |
body { | |
font-family: sans-serif; | |
} | |
svg { | |
font: 10px sans-serif; | |
} | |
.caption { | |
font-weight: bold; | |
fill: #000; | |
} | |
.key path { | |
display: none; | |
} | |
.key line { | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
#search-container { | |
margin-left: 400px; | |
margin-top: 25px; | |
margin-bottom: 20px; | |
} | |
.description { | |
width: 200px; | |
height: 30px; | |
margin-left: 280px; | |
margin-top: -400px; | |
} | |
.county { | |
fill: none; | |
} | |
.county-border { | |
fill: none; | |
} | |
.state-border { | |
fill: none; | |
} | |
</style> | |
<link href="./awesomplete.css" rel="stylesheet"> | |
<script src="./awesomplete.min.js"></script> | |
</head> | |
<body> | |
<div id="search-container"> | |
</div> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<script src="//d3js.org/topojson.v1.min.js"></script> | |
<script> | |
let width = 960, | |
height = 600; | |
let color = d3.scaleThreshold() | |
.domain([30, 60, 120, 360]) | |
.range(["#ffffcc","#c2e699","#78c679","#31a354","#006837"]); | |
// A position encoding for the key only. | |
let x = d3.scaleLinear() | |
.domain([0, 390]) | |
.range([0, 240]); | |
let xAxis = d3.axisBottom() | |
.scale(x) | |
.tickSize(13) | |
.tickValues(color.domain()); | |
// Kentucky State Plane, Single Zone | |
// ftp://kygeonet.ky.gov/kygeodata/standards/Ky_StatePlane.pdf | |
let projection = d3.geoConicConformal() | |
.rotate([85 + 45 / 60, -36 - 20 / 60]) | |
.parallels([37 + 05 / 60, 38 + 40 / 60]) | |
.scale(7090) | |
.center([-0.04940, 0.906]) | |
.translate([width / 2, height / 2]); | |
let path = d3.geoPath() | |
.projection(projection); | |
let svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", 500); | |
let g = svg.append("g") | |
.attr("class", "key") | |
.attr("transform", "translate(60,460)"); | |
g.selectAll("rect") | |
.data(color.range().map(function(d, i) { | |
return { | |
x0: i ? x(color.domain()[i - 1]) : x.range()[0], | |
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1], | |
z: d | |
}; | |
})) | |
.enter().append("rect") | |
.attr("height", 8) | |
.attr("x", function(d) { return d.x0; }) | |
.attr("width", function(d) { return d.x1 - d.x0; }) | |
.style("fill", function(d) { return d.z; }); | |
g.call(xAxis).append("text") | |
.attr("class", "caption") | |
.attr("text-anchor", "start") | |
.attr("y", -6) | |
.text("Population per square mile"); | |
d3.json("ky-counties.json", function(error, ky) { | |
if (error) throw error; | |
let counties = topojson.feature(ky, ky.objects.counties), | |
countyNames = counties.features.map(c => c.properties.name.replace(" County", "")).sort(); | |
let countyShapes = svg.append("g") | |
.attr("class", "county") | |
.selectAll("path") | |
.data(counties.features) | |
.enter().append("path") | |
.attr("stroke", "#fff") | |
.attr("stroke-width", 0.5); | |
countyShapes | |
.attr("d", path) | |
.style("fill", function(d) { return color(d.density = d.properties.population / d.properties.area); }); | |
countyShapes | |
.on("mouseover", function(d) { | |
d3.select(".description").remove(); | |
countyShapes.attr("stroke", "#fff") | |
.attr("stroke-width", 0.5); | |
d3.select(this) | |
.raise() | |
.attr("stroke", "#000") | |
.attr("stroke-width", 2.5); | |
makeDescription(d); | |
}) | |
.on("mouseout", function(d) { | |
d3.select(this) | |
.lower() | |
.attr("stroke", "#fff") | |
.attr("stroke-width", 0.5); | |
d3.select(".description").remove(); | |
}); | |
handleSearching(countyNames, countyShapes); | |
d3.select("#county-input").node().focus(); | |
svg.append("path") | |
.datum(topojson.mesh(ky, ky.objects.counties, function(a, b) { return a === b; })) | |
.attr("class", "state-border") | |
.attr("d", path) | |
.attr("stroke", "#777") | |
.attr("stroke-width", 1) | |
.lower(); | |
svg.append("path") | |
.datum(topojson.mesh(ky, ky.objects.counties, function(a, b) { return a !== b; })) | |
.attr("class", "county-border") | |
.attr("d", path); | |
}); | |
function makeDescription(d) { | |
d3.select("body") | |
.append("div") | |
.attr("class", "description") | |
.html(`<strong>${d.properties.name.replace(" County", "")}</strong>: ${d.density.toFixed(0)}/mi.²`); | |
} | |
function handleSearching(names, shapes) { | |
d3.select("#search-container") | |
.append("input") | |
.attr("class", "awesomplete") | |
.attr("id", "county-input") | |
.attr("tabindex", "0") | |
.property("placeholder", "Search by county"); | |
new Awesomplete(d3.select("#county-input").node(), { | |
list: names | |
}); | |
d3.select("#county-input") | |
.on("awesomplete-selectcomplete", function() { | |
let searchedCounty = shapes.filter( | |
d => d.properties.name.replace(" County", "") === d3.select(this).property("value") | |
); | |
shapes | |
.attr("stroke", "#fff") | |
.attr("stroke-width", 0.5); | |
searchedCounty | |
.raise() | |
.attr("stroke", "#000") | |
.attr("stroke-width", 2.5); | |
d3.select(".description").remove(); | |
makeDescription(searchedCounty.data()[0]); | |
d3.select(this).property("value", ""); | |
}) | |
.on("blur", function() { | |
shapes | |
.attr("stroke", "#fff") | |
.attr("stroke-width", 0.5); | |
d3.select(".description").remove(); | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment