diRows = transpose(diData)
labels = transpose(diLabels)
editions = transpose(diEditions)
// industry id -> name + colour (matches the survey page)
IND = ({76: "Construction", 80: "Manufacturing", 73: "All Businesses"})
INDUSTRIES = ["Manufacturing", "Construction", "All Businesses"]
COL = ({Manufacturing: "#30a4dc", Construction: "#3a6f8f", "All Businesses": "#a9a613"})
// minimum directional base for a DI point to be plotted as "solid".
// below this the index rests on too few directional responses to trust.
BASE_FLOOR = 30Diffusion Index
latestWave = d3.max(diRows, r => +r.wn)
latestQns = [...new Set(diRows.filter(r => +r.wn === latestWave).map(r => +r.qn))]
defaultQn = latestQns.length ? Math.min(...latestQns) : null
viewof selectedQn = {
// build grouped options: "Theme — Header (Subheader)"
const byTheme = d3.group(labels, d => d.theme);
const opts = [];
for (const [theme, rows] of byTheme) {
for (const r of rows.sort((a,b) =>
(a.header+a.subheader).localeCompare(b.header+b.subheader))) {
const sub = r.subheader && r.subheader.trim() ? " — " + r.subheader : "";
opts.push({qn: r.qn, label: `${r.theme}: ${r.header}${sub}`});
}
}
opts.sort((a,b) => a.label.localeCompare(b.label));
return Inputs.select(opts.map(o => o.qn), {
label: "Question",
format: qn => opts.find(o => o.qn === qn).label,
value: defaultQn === null ? opts[0].qn : defaultQn,
width: 480
});
}// Widen the select control itself (Inputs.select's width option sizes the
// wrapper; this ensures the inner <select> fills it so long labels show).
html`<style>
form.oi-inputs-select select, .di-picker select { min-width: 460px; }
</style>`selRows = {
const rows = diRows.filter(r => r.qn === selectedQn)
.map(r => ({
wn: +r.wn,
ind: IND[r.idn],
di: +r.di,
base: +r.n_base,
up: +r.n_up, same: +r.n_same, down: +r.n_down,
solid: +r.n_base >= BASE_FLOOR
}))
.filter(r => r.ind); // only the three page industries
return rows.sort((a,b) => a.wn - b.wn);
}
selLabel = {
const r = labels.find(l => l.qn === selectedQn);
if (!r) return "Diffusion index";
const sub = r.subheader && r.subheader.trim() ? " — " + r.subheader : "";
return `${r.theme}: ${r.header}${sub}`;
}
// wave range present for this question
waveExtent = d3.extent(selRows, r => r.wn)diChart = {
const present = INDUSTRIES.filter(i => selRows.some(r => r.ind === i));
const solid = selRows.filter(r => r.solid);
const lowc = selRows.filter(r => !r.solid);
// last (latest-wave) point per industry, for end-of-line value labels
const endPoints = present.map(ind => {
const rows = selRows.filter(r => r.ind === ind);
return rows.reduce((a, b) => (b.wn > a.wn ? b : a), rows[0]);
}).filter(Boolean);
// adaptive y-domain: tighten to the data so the chart fills the vertical
// space, but ALWAYS include 50 (the no-change reference) so the
// above/below reading stays honest. Pad by ~8% of the range.
const vals = selRows.map(r => r.di).filter(v => Number.isFinite(v));
let lo = Math.min(50, d3.min(vals)), hi = Math.max(50, d3.max(vals));
const pad = Math.max(2, (hi - lo) * 0.08);
lo = Math.max(0, lo - pad);
hi = Math.min(100, hi + pad);
return Plot.plot({
marginLeft: 44, marginRight: 52, marginTop: 16, marginBottom: 34,
height: 420, width: 900,
x: {label: "Wave \u2192", tickFormat: d => d, grid: false,
domain: waveExtent},
y: {label: "Diffusion index", domain: [lo, hi], grid: true},
color: {domain: present, range: present.map(i => COL[i]), legend: true},
marks: [
// the 50 reference line (no change)
Plot.ruleY([50], {stroke: "#999", strokeDasharray: "4 3", strokeWidth: 1}),
// solid lines per industry through confident points
Plot.line(solid, {x: "wn", y: "di", stroke: "ind", strokeWidth: 2,
curve: "monotone-x", z: "ind"}),
// confident points
Plot.dot(solid, {x: "wn", y: "di", fill: "ind", r: 2.5}),
// low-confidence points: hollow, no connecting line
Plot.dot(lowc, {x: "wn", y: "di", stroke: "ind", fill: "white",
r: 2.5, strokeWidth: 1}),
// end-of-line value labels: the latest index per industry, to the right
Plot.text(endPoints, {x: "wn", y: "di", fill: "ind",
text: d => d.di.toFixed(1),
dx: 10, textAnchor: "start",
fontSize: 11, fontWeight: 600}),
// hover
Plot.tip(selRows, Plot.pointer({x: "wn", y: "di",
title: r => `${r.ind}\nWave ${r.wn}\nIndex: ${r.di.toFixed(1)}` +
`\nUp ${r.up.toFixed(0)} / Same ${r.same.toFixed(0)} / Down ${r.down.toFixed(0)}` +
`\nBase: ${r.base.toFixed(0)}${r.solid ? "" : " (low confidence)"}`}))
]
});
}htl.html`<h3>${selLabel}</h3>
<div class="di-explain">Latest wave: <strong>${latestWave}</strong>.
<strong>50 = no change</strong>; above 50 indicates net increase, below 50 net
decrease. The index is <em>(% up) + ½ × (% same)</em>, computed among firms
giving a directional answer. Hollow points sit on a small directional base
(< ${BASE_FLOOR}) and are shown for completeness but are low-confidence.</div>
${diChart}
<div class="bics-src">Source:
<a href="https://www.ons.gov.uk/economy/economicoutputandproductivity/output/datasets/businessinsightsandimpactontheukeconomy" target="_blank">ONS | Business Insights and Conditions Survey</a></div>`