Public
Edited
Mar 19, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
customToolTip = ({ from_iso, to_iso, transit_time_in_day }) => {
const [origin] = d_prefecture_all.filter((d) => d.iso === from_iso);
const [destination] = d_prefecture_all.filter((d) => d.iso === to_iso);
const originMsg = `${origin.prefecture_en} (${origin.region_en})`;
const destinationMsg = `${destination.prefecture_en} (${destination.region_en})`;
return `${originMsg} to ${destinationMsg}\nThe transit time is ${transit_time_in_day} days.`;
}
Insert cell
colorContrast = ({ luminance = 60, ...options }) =>
Plot.initializer(options, (data, facets, channels, scales) => {
const contrast = (color) =>
d3.hcl(color).l > luminance ? "black" : "white";
return {
channels: {
fill: {
...channels.fill,
scale: undefined,
value:
channels.fill == null || // when options.fill is a constant color such as "red"
scales.color == null // when the scale isn't yet available (e.g. hexbin below)
? Array.from(data).fill(contrast(options.fill))
: Array.from(channels.fill.value, (value) =>
contrast(scales.color(value))
)
}
}
};
})
Insert cell
// Function to position the tooltip
hover = (tip, pos, text) => {
const side_padding = 10;
const vertical_padding = 5;
const vertical_offset = 15;

// Empty it out
tip.selectAll("*").remove();

// Append the text
tip
.style("text-anchor", "middle")
.style("pointer-events", "none")
.attr("transform", `translate(${pos[0]}, ${pos[1] + 7})`)
.selectAll("text")
.data(text)
.join("text")
.style("dominant-baseline", "ideographic")
.text((d) => d)
.attr("y", (d, i) => (i - (text.length - 1)) * 15 - vertical_offset)
.style("font-weight", (d, i) => (i === 0 ? "bold" : "normal"));

const bbox = tip.node().getBBox();

// Add a rectangle (as background)
tip
.append("rect")
.attr("y", bbox.y - vertical_padding)
.attr("x", bbox.x - side_padding)
.attr("width", bbox.width + side_padding * 2)
.attr("height", bbox.height + vertical_padding * 2)
.style("fill", "white")
.style("stroke", "#d3d3d3")
.lower();
}
Insert cell
// To generate a unique ID for each chart so that they styles only apply to that chart
id_generator = () => {
var S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return "a" + S4() + S4();
}
Insert cell
addTooltips = (chart, styles) => {
const stroke_styles = { stroke: "blue", "stroke-width": 3 };
const fill_styles = { fill: "blue", opacity: 0.5 };

// Workaround if it's in a figure
const type = d3.select(chart).node().tagName;
let wrapper =
type === "FIGURE" ? d3.select(chart).select("svg") : d3.select(chart);

// Workaround if there's a legend....
const svgs = d3.select(chart).selectAll("svg");
if (svgs.size() > 1) wrapper = d3.select([...svgs].pop());
wrapper.style("overflow", "visible"); // to avoid clipping at the edges

// Set pointer events to visibleStroke if the fill is none (e.g., if its a line)
wrapper.selectAll("path").each(function (data, index, nodes) {
// For line charts, set the pointer events to be visible stroke
if (
d3.select(this).attr("fill") === null ||
d3.select(this).attr("fill") === "none"
) {
d3.select(this).style("pointer-events", "visibleStroke");
if (styles === undefined) styles = stroke_styles;
}
});

if (styles === undefined) styles = fill_styles;

const tip = wrapper
.selectAll(".hover")
.data([1])
.join("g")
.attr("class", "hover")
.style("pointer-events", "none")
.style("text-anchor", "middle");

// Add a unique id to the chart for styling
const id = id_generator();

// Add the event listeners
d3.select(chart).classed(id, true); // using a class selector so that it doesn't overwrite the ID
wrapper.selectAll("title").each(function () {
// Get the text out of the title, set it as an attribute on the parent, and remove it
const title = d3.select(this); // title element that we want to remove
const parent = d3.select(this.parentNode); // visual mark on the screen
const t = title.text();
if (t) {
parent.attr("__title", t).classed("has-title", true);
title.remove();
}
// Mouse events
parent
.on("pointerenter pointermove", function (event) {
const text = d3.select(this).attr("__title");
const pointer = d3.pointer(event, wrapper.node());
if (text) tip.call(hover, pointer, text.split("\n"));
else tip.selectAll("*").remove();

// Raise it
d3.select(this).raise();
// Keep within the parent horizontally
const tipSize = tip.node().getBBox();
if (pointer[0] + tipSize.x < 0)
tip.attr(
"transform",
`translate(${tipSize.width / 2}, ${pointer[1] + 7})`
);
else if (pointer[0] + tipSize.width / 2 > wrapper.attr("width"))
tip.attr(
"transform",
`translate(${wrapper.attr("width") - tipSize.width / 2}, ${
pointer[1] + 7
})`
);
})
.on("pointerout", function (event) {
tip.selectAll("*").remove();
// Lower it!
d3.select(this).lower();
});
});

// Remove the tip if you tap on the wrapper (for mobile)
wrapper.on("touchstart", () => tip.selectAll("*").remove());

// Define the styles
chart.appendChild(html`<style>
.${id} .has-title { cursor: pointer; pointer-events: all; }
.${id} .has-title:hover { ${Object.entries(styles)
.map(([key, value]) => `${key}: ${value};`)
.join(" ")} }`);

return chart;
}
Insert cell
tooltipPlugin = (Plot) => {
const { plot } = Plot;
Plot.plot = ({ tooltip, ...options }) => addTooltips(plot(options), tooltip);
return Plot;
}
Insert cell
Plot = tooltipPlugin(await require("@observablehq/plot@latest")) //plot@0.5
Insert cell
Insert cell
d_prefecture_all = FileAttachment("prefecture_data.csv").csv({ typed: true })
Insert cell
d_prefecture_names = FileAttachment("prefecture_name_data.csv").csv()
Insert cell
d_carrier_all = FileAttachment("carrier_data.csv").csv({ typed: true })
Insert cell
Insert cell
d_prefecture_iso = [...new Set(d_prefecture_all.map((d) => d.iso))]
Insert cell
d_carrier_names = [...new Set(d_carrier_all.map((d) => d.carrier_name))]
Insert cell
d_tt_by_carrier = d3.group(d_carrier_all, (d) => d.carrier_name)
Insert cell
d_tt_selection = d_tt_by_carrier.get(selected_carrier)
Insert cell
d_tt_selection_domain = [
...new Set(d_tt_selection.map((d) => d.transit_time_in_day))
].sort()
Insert cell
d_tt_by_from_iso = d3.group(d_tt_selection, (d) => d.from_iso)
Insert cell
d_tt_matrix = {
const matrix = [];
for (const from_iso of d_prefecture_iso) {
let d_tt_by_to_iso = d3.group(
d_tt_by_from_iso.get(from_iso),
(d) => d.to_iso
);
const lane_matrix = [];
for (const to_iso of d_prefecture_iso) {
const [lane] = d_tt_by_to_iso.get(to_iso);
lane_matrix.push(lane.transit_time_in_day);
}
matrix.push(lane_matrix);
}

return matrix;
}
Insert cell
Insert cell
<style>
.plaque-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-items: stretch;
align-content: stretch;
width: 960px;
}
.plaque {
width: 28px;
border: 1px solid;
margin: 2px;
}
.plaque--num {
background-color: #eee8d5;
color: #002b36;
text-align: center;
font-weight: bold;
font-size: 0.8em
}
.plaque--text {
color: #002b36;
writing-mode: vertical-rl;
padding: 2px 0;
height: auto;
}
</style>
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more