Searches features using Awesomplete. Adapted from https://bl.ocks.org/mbostock/5144735.
xxxxxxxxxx
<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>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v1.min.js