viewof schwelle = Inputs.range([25, 39], {
  value: 30,
  step: 0.5,
  label: "Schwellenwert Hitzetage (°C)"
})
function maxHitzeperiode(data, threshold) {
  const byYear = d3.group(data, d => d.Jahr);
  return Array.from(byYear, ([Jahr, days]) => {
    let maxRun = 0, currentRun = 0;
    for (const d of days) {
      if (d.tlmax >= threshold) {
        currentRun++;
        maxRun = Math.max(maxRun, currentRun);
      } else {
        currentRun = 0;
      }
    }
    return { Jahr, Hitzeperiode: maxRun };
  });
}

perioden = maxHitzeperiode(transpose(hw_data), schwelle)

Plot.plot({
  marks: [
    Plot.barY(perioden, { x: "Jahr", y: "Hitzeperiode", fill: "firebrick" })
  ],
  x: { tickFormat: "d", label: "Jahr", ticks: [...d3.range(1950, 2026, 10), 2026] },
  y: { label: "Längste Hitzeperiode (Tage)", tickFormat: d => Number.isInteger(d) ? d : "" },
  width: 700
})