Public
Edited
Feb 28, 2023
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const container = html`<div class="tooltip-demo">
<style>
/*
* Be sure to set the tooltip's DOM element's styles!
*/
div.tooltip-demo > div.tooltip {
position: fixed;
display: none;
padding: 12px 6px;
background: #fff;
border: 1px solid #333;
pointer-events: none;
}
</style>
</div>`;
const width = 920;
const height = 560;

const projection = d3.geoAlbersUsa().fitSize([width, height], states);
const path = d3.geoPath().projection(projection);

const svg = d3.create("svg").attr("viewBox", [0, 0, 975, 610]);

svg
.selectAll(".state")
.data(states.features)
.join("path")
.attr("d", path)
.attr("class", "state")
.style("fill", function (d) {
//Get data value
const value = stateBuddhistProportions.get(d.properties.NAME);

if (value) {
//If value exists…
return colorScale(value);
} else {
//If value is undefined…
return "#ccc";
}
})
//.attr("fill", d => colorScale(d => +d.count.split(",").join("")))
.attr("fill-opacity", 1)
.attr("stroke", "black");

svg
.append("g")
.attr("transform", "translate(520,-5)")
.append(() =>
legend({
color,
title: "Proportion of Buddhists, thousands",
width: 340
})
);
const div = d3.select(container).append("div").classed("tooltip", true);

// const tooltip = svg.append("g");
const tooltip = new Tooltip(div);
// function that sets the tooltip's HTML when revealed
// can be passed the hovered selection's datum
function tooltipContents(datum) {
const { x, y } = datum;
return `x: ${x}, y: ${y}`;
}

svg
.selectAll(".state")
// .on("touchmove mousemove", function (event, d) {
// tooltip.call(
// callout, `${stateBuddhistProportions.get(d.properties.NAME)} ${d.properties.NAME}, ${data.get(d.value)}`
// );
// tooltip.attr("transform", `translate(${d3.pointer(event, this)})`);
// d3.select(this).attr("stroke", "gray").raise();
// })
// .on("touchend mouseleave", function () {
// tooltip.call(callout, null);
// d3.select(this).attr("stroke", null).lower();
// });
.on("mouseover", (event, d) => {
// reveal the tooltip.
// pass the current datum (if desired),
// and a render function that returns an HTML string (required)
tooltip.display(
d,
`${stateBuddhistProportions.get(d.properties.NAME)} ${
d.properties.NAME
}, ${data.get(d.value)}`
);
d3.select(this).attr("stroke", "gray").raise();
});
return svg.node();
// return container;
}
Insert cell
states = await FileAttachment("usState.json").json()
Insert cell
usstate = FileAttachment("usState.json").json()
Insert cell
data = d3
.csvParse(await FileAttachment("pop_state.csv").text(), d3.autoType)
// .map(d => ({
// ...d,
// value: +d.value.split(",").join("")
// }))
Insert cell
stateBuddhistProportions = new Map(data.map((d) => [d.state, d.value]))
Insert cell
min = d3.min(data, d => d.value)
Insert cell
//max = d3.format(",.2f")(d3.max(data, d => d.value))
max = d3.max(data, d => d.value)
Insert cell
Insert cell
color = d3.scaleQuantize([43434, 83385], d3.schemePurples[9])
Insert cell
d3 = require('d3@6')
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
import { callout } from "@d3/line-chart-with-tooltip"
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