Published
Edited
Sep 12, 2022
1 fork
Importers
3 stars
Insert cell
Insert cell
house_choro = {
const container = html`<div class="tooltip-carto"></div>`;

const div = d3.select(container).append("div").classed("tooltip", true);

const tooltip = new Tooltip(div);

function tooltipContents(datum) {
return `${datum}`;
}

const svg = d3
.select(container)
.append("svg")
.attr("viewBox", `0 0 700 525`)
.attr("width", 700)
.attr("height", 525);

const base_map = svg.append("g");

base_map
.append("g")
.attr("class", "state-outline")
.selectAll("path")
.data(house_carto.outlines)
.enter()
.append("path")
.attr("id", (d) => d.state)
.on("mouseover", (event, d) => {})
.on("mouseover", (event, d) => {})
.on("mouseout", (event, d) => {})
.attr("d", (t) => t.d)
.attr("transform", (t) =>
"translate(".concat(t.translate[0], ", ").concat(t.translate[1], ")")
)
.lower();

const tiles = svg.append("g");

const scale = 5;

tiles
.append("g")
.attr("class", "map")
.attr(
"transform",
"scale("
.concat(scale, ")translate(")
.concat(24 * scale, ",")
.concat(10 * scale, ")")
);

tiles
.append("g")
.attr("class", "district")
.selectAll("path")
.data(house_carto.districts)
.enter()
.append("path")
.attr("d", (t) =>
"M".concat(t.points.map((t) => t.join(" ")).join("L"), "Z")
)
.attr("fill", (d) =>
d3.scaleSequential([0, 1], d3.interpolateReds)(winprob[d.race] / 100)
)
.attr("fill-opacity", 1)
.on("mouseover", (event, d) => {
console.log(d3.select("#" + d.state));
d3.select(event.target).attr("class", "district outline");
d3.select("#" + d.state).attr("class", "state-outline dark");
tooltip.display(candidate[d.race], tooltipContents);
})
.on("mouseout", (event, d) => {
d3.select(event.target).attr("class", "district");
d3.select("#" + d.state).attr("class", "state-outline");
tooltip.hide();
})
.on("mousemove", (event, d) => {
tooltip.move(event);
});

const labels = svg.append("g");

labels
.selectAll("g.labels")
.data(["bg", "fg"])
.enter()
.append("g")
.attr("class", (t) => "labels ".concat(t))
.selectAll("text")
.data(house_carto.labels)
.enter()
.append("text")
.attr("transform", (t, e) =>
"translate(".concat(t.x, ", ").concat(t.y, ")")
)
.text((t, e) => t.state);

return container;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
css = html`
<style>
@font-face {
font-family: Comic Mono;
font-weight: normal;
src: url(https://cdn.jsdelivr.net/npm/comic-mono@0.0.1/ComicMono.ttf);
}

@font-face {
font-family: Comic Mono;
font-weight: bold;
src: url(https://cdn.jsdelivr.net/npm/comic-mono@0.0.1/ComicMono-Bold.ttf);
}

div.tooltip-carto > div.tooltip {
font-family: "Comic Mono", monospace;
font-size: 9pt;
position: fixed;
display: none;
padding: 12px 6px;
background: #fff;
border: 1px solid #333;
pointer-events: none;
}

h1 {
max-width: none;
font-family: "Comic Mono", monospace;
font-weight: bold;
}

.state-outline {
fill: white;
stroke: #c3c3c3;
stroke-width: 0.25;
}

.state-outline.dark {
fill: white;
stroke: black;
stroke-width: 3;
}

.district {
fill: white;
stroke: #c3c3c3;
stroke-width: 0.25;
}

.district.outline {
fill: white;
stroke: black;
stroke-width: 3;
}


path {
stroke-width: 0.25;
stroke: #c3c3c3;
}
g.labels {
pointer-events: none
}

g.labels text {
font-family: "Comic Mono", monospace;
font-size: 15px
}

@media (max-width: 767px) {
g.labels text {
font-size: 18px
}
}

g.labels.bg text {
stroke-width: 4;
stroke: #fff;
fill: #fff
}

g.labels.fg text {
stroke: none;
fill: #222
}
</style>`
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more