topics = transpose(bicsTopics)
data = transpose(bicsData)
respondents = transpose(bicsRespondents)
trend = transpose(bicsTrend)
questions = transpose(bicsQuestions)
waves = bicsWaves.sort((a,b) => b - a)
// lookup: "wn|qn" -> question text
questionFor = {
const m = new Map();
for (const r of questions) m.set(r.wn + "|" + r.qn, r.q);
return m;
}BICS Survey
viewof wave = Inputs.select(waves, {
label: "Wave",
format: w => "Wave " + w + (bicsPeriods[String(w)] ? " · " + bicsPeriods[String(w)] : ""),
value: waves[0]
})INDUSTRIES = ["Manufacturing", "Construction", "All Businesses"]
COL = ({Manufacturing: "#30a4dc", Construction: "#3a6f8f", "All Businesses": "#a9a613"})
dataIndex = {
const m = new Map();
for (const r of data) {
const k = r.wn + "|" + r.qn;
if (!m.has(k)) m.set(k, []);
m.get(k).push(r);
}
return m;
}highlightHtml = bicsHighlights[String(wave)] || ""
edition = bicsEditions[String(wave)] || {}
period = bicsPeriods[String(wave)] || ""
// Shared date formatter: dd mmm yyyy (zero-padded day, 3-letter month), built from
// the integer edition columns so it is locale-proof and never diverges between the
// highlights header and the chart question bars. Returns "" when no edition row.
monthAbbr = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
function fmtEdition(ed) {
if (!ed || ed.dy == null || ed.mth == null || ed.yr == null) return "";
const mi = +ed.mth;
if (!(mi >= 1 && mi <= 12)) return "";
return `${String(ed.dy).padStart(2, "0")} ${monthAbbr[mi - 1]} ${ed.yr}`;
}
editionDate = fmtEdition(edition)
// htl escapes interpolated strings, so inject a parsed DOM node instead.
function rawHtml(s) {
const t = document.createElement("template");
t.innerHTML = s;
return t.content.cloneNode(true);
}htl.html`<div class="bics-card bics-highlights">
<div class="hl-eyebrow">Highlights${editionDate ? htl.html` · <span style="text-transform:none;">${editionDate}</span>` : ""}</div>
${rawHtml(highlightHtml)}
</div>`function chart(rows) {
const present = INDUSTRIES.filter(i => rows.some(r => r.ind === i));
const ref = present.includes("All Businesses") ? "All Businesses" : present[0];
const order = [...new Set(rows.filter(r => r.ind === ref)
.sort((a,b) => a.v - b.v).map(r => r.x))];
const cats = order.length ? order : [...new Set(rows.map(r => r.x))];
// Adaptive left margin: size to the longest category label so labels never
// clip, but stay tight for short-label charts. Labels are kept concise at the
// seed stage (bd.bics_fill_qs_labels), so wrapping is unnecessary.
const longest = cats.reduce((m, s) => Math.max(m, String(s).length), 0);
const marginLeft = Math.min(280, Math.max(110, longest * 6 + 14));
return Plot.plot({
marginLeft, marginRight: 16, marginTop: 6, marginBottom: 26,
height: Math.max(120, cats.length * present.length * 15 + present.length * 30),
width: 860,
x: {label: "Proportion of firms (%) \u2192", grid: true,
domain: [0, Math.max(5, d3.max(rows, r => r.v) * 1.12)]},
y: {domain: cats, label: null, tickSize: 0},
fy: {domain: present, label: null, axis: null},
color: {domain: present, range: present.map(i => COL[i])},
marks: [
Plot.barX(rows, {x: "v", y: "x", fy: "ind", fill: "ind",
sort: null, insetTop: 1, insetBottom: 1}),
Plot.text(rows, {x: "v", y: "x", fy: "ind", text: d => d.v.toFixed(1),
textAnchor: "start", dx: 4, fontSize: 10, fill: "#444"}),
Plot.ruleX([0])
]
});
}
function legendNode() {
return htl.html`<div class="bics-legend">${
INDUSTRIES.map(i => htl.html`<span><i style="background:${COL[i]}"></i>${i}</span>`)
}</div>`;
}body = {
const wTopics = topics.filter(t => t.wv == wave).sort((a,b) => a.pos - b.pos);
if (!wTopics.length)
return htl.html`<div class="bics-empty">No topic structure defined for this wave.</div>`;
const themes = [...new Set(wTopics.map(t => t.theme))];
const sections = themes.map(th => {
const headers = [...new Set(wTopics.filter(t => t.theme === th).map(t => t.header))];
const cards = headers.map(hd => {
const rows = wTopics.filter(t => t.theme === th && t.header === hd);
const single = rows.length === 1 && !rows[0].subheader;
// chart + its survey question caption, tied to this specific qn
const chartWithQ = (qn) => {
const d = dataIndex.get(wave + "|" + qn) || [];
const q = questionFor.get(wave + "|" + qn);
if (!d.length) return htl.html`<div class="bics-empty">No data for this question.</div>`;
return htl.html`${
q ? htl.html`<div class="bics-question">${q}</div>` : ""}${legendNode()}${chart(d)}`;
};
let content;
if (single) {
content = chartWithQ(rows[0].qn);
} else {
const tabs = rows.map((r,i) => htl.html`<button class="bics-tab ${i===0?'active':''}"
onclick=${function(){
const card = this.closest('.bics-card');
card.querySelectorAll('.bics-tab').forEach(x=>x.classList.remove('active'));
this.classList.add('active');
card.querySelectorAll('.bics-panel').forEach((p,j)=>p.classList.toggle('active', j===i));
}}>${r.subheader || ('Q'+r.qn)}</button>`);
const panels = rows.map((r,i) =>
htl.html`<div class="bics-panel ${i===0?'active':''}">${chartWithQ(r.qn)}</div>`);
content = htl.html`<div class="bics-tabs">${tabs}</div>${panels}`;
}
return htl.html`<div class="bics-card">
<h3>${hd}</h3>
${content}
<div class="bics-src">Wave ${wave}${period ? " · " + period : ""}${editionDate ? " · Published " + editionDate : ""} — Source:
<a href="https://www.ons.gov.uk/economy/economicoutputandproductivity/output/datasets/businessinsightsandimpactontheukeconomy" target="_blank">ONS | Business Insights and Conditions Survey</a></div>
</div>`;
});
return htl.html`<h2 class="bics-theme">${th}</h2>${cards}`;
});
return htl.html`<div>${sections}</div>`;
}respondentsSection = {
const wResp = respondents.filter(r => r.wn == wave);
if (!wResp.length)
return htl.html`<div class="bics-empty">Respondent data not available for this wave.</div>`;
const pull = (grp, industry, size) => {
const r = wResp.find(x => x.data_grp===grp && x.industry===industry && x.size_range===size);
return r ? r.count : null;
};
const sample = pull("sample","UK","Total");
const response = pull("response","UK","Total");
const rate = sample ? (response/sample*100).toFixed(1) : "\u2014";
const tWide = d3.rollup(trend, v => v[0].count, d => d.wn, d => d.grp);
const tRows = [...tWide.keys()].sort((a,b)=>a-b).flatMap(wn => {
const g = tWide.get(wn);
return ["sample","response"].filter(k=>g.has(k))
.map(k => ({wn, grp: k==="sample"?"Sample":"Responses", count: g.get(k)}));
});
const trendChart = Plot.plot({
height: 230, width: 860, marginLeft: 60, marginRight: 16,
x: {label: "Wave", tickFormat: "d"},
y: {label: "Count", grid: true, tickFormat: "~s"},
color: {domain:["Sample","Responses"], range:["#3a6f8f","#649e35"], legend:true},
marks: [
Plot.line(tRows, {x:"wn", y:"count", stroke:"grp",
strokeDasharray: d => d.grp==="Responses" ? "4,3" : null}),
Plot.ruleY([0])
]
});
const exclude = new Set(["All Industries","UK","Northern Ireland","Scotland","Wales",
"England","South West","South East","London","East of England","West Midlands",
"East Midlands","Yorkshire & the Humber","North West","North East"]);
const indMap = d3.group(wResp.filter(r => r.size_range==="Total" && !exclude.has(r.industry)),
r => r.industry);
const indRows = [...indMap].map(([industry, rs]) => {
const s = rs.find(r=>r.data_grp==="sample")?.count || 0;
const r = rs.find(r=>r.data_grp==="response")?.count || 0;
return {industry, rate: s ? +(r/s*100).toFixed(1) : 0};
}).filter(d=>d.rate>0).sort((a,b)=>a.rate-b.rate);
const indChart = Plot.plot({
height: Math.max(160, indRows.length*22), width: 860, marginLeft: 200, marginRight: 40,
x: {label: "Response rate (%)", grid: true},
y: {domain: indRows.map(d=>d.industry), label: null, tickSize:0},
marks: [
Plot.barX(indRows, {x:"rate", y:"industry", fill:"#3a6f8f"}),
Plot.text(indRows, {x:"rate", y:"industry", text:d=>d.rate+"%",
textAnchor:"start", dx:4, fontSize:10, fill:"#444"}),
Plot.ruleX([0])
]
});
const regMap = d3.group(wResp.filter(r => r.grp==="region" && r.size_range==="Total"),
r => r.industry);
const regRows = [...regMap].map(([region, rs]) => {
const s = rs.find(r=>r.data_grp==="sample")?.count || 0;
const r = rs.find(r=>r.data_grp==="response")?.count || 0;
return {region, rate: s ? +(r/s*100).toFixed(1) : 0};
}).filter(d=>d.rate>0).sort((a,b)=>a.rate-b.rate);
const regChart = Plot.plot({
height: Math.max(160, regRows.length*22), width: 860, marginLeft: 200, marginRight: 40,
x: {label: "Response rate (%)", grid: true},
y: {domain: regRows.map(d=>d.region), label: null, tickSize:0},
marks: [
Plot.barX(regRows, {x:"rate", y:"region", fill:"#30a4dc"}),
Plot.text(regRows, {x:"rate", y:"region", text:d=>d.rate+"%",
textAnchor:"start", dx:4, fontSize:10, fill:"#444"}),
Plot.ruleX([0])
]
});
return htl.html`
<h2 class="bics-theme">Survey Respondents</h2>
<div class="bics-card">
<div class="bics-metrics">
<span><b>Wave</b> ${wave}</span>
<span><b>Sample</b> ${sample ? sample.toLocaleString() : "\u2014"}</span>
<span><b>Responses</b> ${response ? response.toLocaleString() : "\u2014"}</span>
<span><b>Response rate</b> ${rate}%</span>
</div>
<h3>Sample & Response Trend</h3>
${trendChart}
<h3 style="margin-top:18px">Response Rate by Industry</h3>
${indChart}
<h3 style="margin-top:18px">Response Rate by Region</h3>
${regChart}
<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>
</div>`;
}