Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ automatisch mit [git-cliff](https://git-cliff.org) aus den Commit-Messages gener

- **icons**: Hochgeladene SVG-Symbole nehmen die Textfarbe an
- **calendar**: Schichtzeiten immer mit Minuten
- **weather**: Temperaturdiagramm so scharf wie der übrige Text

## [0.9.1] - 2026-11-14

Expand Down
83 changes: 82 additions & 1 deletion web/src/features/modules/weather/Widget.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { screen, waitFor, within } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { json, mockApi, problem } from '../../../test/mockApi.ts'
Expand Down Expand Up @@ -80,7 +82,7 @@ describe('weather widget', () => {
await screen.findByText('17°')
const bars = container.querySelectorAll('.mirror-weather__rain-bars rect')
expect(bars).toHaveLength(1)
expect(within(container.querySelector('.mirror-weather__rain-bars') as HTMLElement).getByText('40%')).toBeInTheDocument()
expect(within(container.querySelector('.mirror-weather__chart-labels') as HTMLElement).getByText('40%')).toBeInTheDocument()
})

describe('temperature chart', () => {
Expand Down Expand Up @@ -136,6 +138,85 @@ describe('weather widget', () => {
expect(labelLefts).toHaveLength(9)
pointXs.forEach((x, i) => expect(labelLefts[i]).toBeCloseTo((x / 640) * 100, 3))
})

it('draws all chart text as HTML: no SVG text, one value label per point at the point x', async () => {
stubObserver(640)
const data = weatherData()
setup({ configured: true, stale: false, data })
const { container } = renderWithProviders('/')
await screen.findByText('17°')
const svg = container.querySelector('svg.mirror-weather__chart') as SVGSVGElement
await waitFor(() => expect(svg.getAttribute('viewBox')).toBe('0 0 640 265'))
expect(svg.querySelectorAll('text')).toHaveLength(0)
const overlay = container.querySelector('.mirror-weather__chart-labels') as HTMLElement
expect(overlay.tagName).toBe('DIV')
const values = [...overlay.querySelectorAll<HTMLElement>('.mirror-weather__chart-value')]
expect(values.map((el) => el.textContent)).toEqual(data.hourly.map((h) => `${h.temperatureC}°`))
const pointXs = [...svg.querySelectorAll(':scope > g:not(.mirror-weather__rain-bars) > circle')].map((c) => Number(c.getAttribute('cx')))
values.forEach((el, i) => expect(parseFloat(el.style.left)).toBeCloseTo((pointXs[i] / 640) * 100, 3))
})

it('shows the three scale labels and the rain percentage as HTML in the overlay', async () => {
setup({ configured: true, stale: false, data: weatherData() })
const { container } = renderWithProviders('/')
await screen.findByText('17°')
const overlay = container.querySelector('.mirror-weather__chart-labels') as HTMLElement
const scale = [...overlay.querySelectorAll('.mirror-weather__chart-scale')].map((el) => el.textContent)
expect(scale).toHaveLength(3)
scale.forEach((text) => expect(text).toMatch(/^-?\d+°$/))
expect([...overlay.querySelectorAll('.mirror-weather__chart-rain')].map((el) => el.textContent)).toEqual(['40%'])
})

it('right-aligns the scale labels in the left gutter, ending 12 px before the first point', async () => {
stubObserver(640)
setup({ configured: true, stale: false, data: weatherData() })
const { container } = renderWithProviders('/')
await screen.findByText('17°')
const svg = container.querySelector('svg.mirror-weather__chart') as SVGSVGElement
await waitFor(() => expect(svg.getAttribute('viewBox')).toBe('0 0 640 265'))
const firstX = Number(svg.querySelector(':scope > g:not(.mirror-weather__rain-bars) > circle')?.getAttribute('cx'))
const labels = [...container.querySelectorAll<HTMLElement>('.mirror-weather__chart-scale')]
expect(labels).toHaveLength(3)
// The CSS rule translates by -100%, so left is the right edge of the label.
labels.forEach((el) => expect(parseFloat(el.style.left)).toBeCloseTo(((firstX - 12) / 640) * 100, 3))
// Vertical anchor: top is the grid line's y, and the CSS lifts the label so its bottom edge sits 4 px above it.
const lineYs = [...svg.querySelectorAll(':scope > line:not(.mirror-weather__chart-now)')].map((l) => Number(l.getAttribute('y1')))
expect(lineYs).toHaveLength(3)
expect(labels.map((el) => parseFloat(el.style.top)).sort((a, b) => a - b)).toEqual([...lineYs].sort((a, b) => a - b))
const css = readFileSync(resolve(process.cwd(), 'src/features/modules/weather/weather.css'), 'utf8')
const rule = /\.mirror-weather__chart-scale\s*\{[^}]*\}/.exec(css)?.[0] ?? ''
expect(rule).toContain('transform: translate(-100%, calc(-100% - 4px))')
})

it('draws a gradient area that closes on the bottom plot line, below the line, in both clipped halves', async () => {
setup({ configured: true, stale: false, data: weatherData() })
const { container } = renderWithProviders('/')
await screen.findByText('17°')
const areas = [...container.querySelectorAll('path.mirror-weather__chart-area')]
expect(areas).toHaveLength(2)
const bottom = container.querySelector('svg.mirror-weather__chart > line')?.getAttribute('y1')
for (const area of areas) {
const d = area.getAttribute('d') as string
expect(d).toMatch(new RegExp(`L [\\d.]+,${bottom} L [\\d.]+,${bottom} Z$`))
const id = /^url\(#(.+)\)$/.exec(area.getAttribute('fill') ?? '')?.[1]
expect(id).toBeTruthy()
expect(container.querySelector(`linearGradient[id="${id}"]`)).not.toBeNull()
expect(area.nextElementSibling?.classList.contains('mirror-weather__chart-temp')).toBe(true)
}
})

it('gives the points a black outline so the line passes behind them cleanly', async () => {
setup({ configured: true, stale: false, data: weatherData() })
const { container } = renderWithProviders('/')
await screen.findByText('17°')
const points = container.querySelectorAll('circle.mirror-weather__chart-point')
expect(points).toHaveLength(9)
expect(points[0].getAttribute('r')).toBe('4.5')
points.forEach((p) => {
expect(p.getAttribute('stroke')).toBe('#000')
expect(p.getAttribute('stroke-width')).toBe('2')
})
})
})

it('shows the alert line', async () => {
Expand Down
132 changes: 87 additions & 45 deletions web/src/features/modules/weather/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,57 +165,99 @@ function WeatherChart({ entries, now, width }: { entries: Hourly[]; now: number;
const lastTime = new Date(entries[entries.length - 1].time).getTime()
const currentProgress = Math.max(0, Math.min(1, (now - firstTime) / (lastTime - firstTime || 1)))
const currentX = left + plotWidth * currentProgress
const first = temperatures[0]
const last = temperatures[temperatures.length - 1]
// Closed shape below the curve down to the bottom plot line; the gradient fades it out towards that line.
const areaPath = `${path} L ${last.x},${yBottom} L ${first.x},${yBottom} Z`
const scaleLabels = [
{ value: high, y: yTop },
{ value: mid, y: 130 },
{ value: low, y: yBottom },
]
const pct = (x: number) => `${(x / width) * 100}%`
// Text is HTML on top of the SVG: Chromium draws SVG text without the hinting and weight of HTML text.
return (
<svg
className="mirror-weather__chart"
viewBox={`0 0 ${width} ${chartHeight}`}
width={width}
height={chartHeight}
aria-label="Temperatur- und Niederschlagsverlauf"
>
<defs>
<clipPath id={`${clipId}-past`}>
<rect x="-20" y="0" width={currentX + 20} height={chartHeight} />
</clipPath>
<clipPath id={`${clipId}-future`}>
<rect x={currentX} y="0" width={width - currentX + 20} height={chartHeight} />
</clipPath>
</defs>
<line x1={left} y1={yBottom} x2={right} y2={yBottom} />
<line x1={left} y1="130" x2={right} y2="130" />
<line x1={left} y1={yTop} x2={right} y2={yTop} />
<text className="mirror-weather__chart-scale" x="0" y="217">{low}°</text>
<text className="mirror-weather__chart-scale" x="0" y="137">{mid}°</text>
<text className="mirror-weather__chart-scale" x="0" y="57">{high}°</text>
<g className="mirror-weather__rain-bars">
<div className="mirror-weather__chart-box" style={{ height: chartHeight }}>
<svg
className="mirror-weather__chart"
viewBox={`0 0 ${width} ${chartHeight}`}
width={width}
height={chartHeight}
aria-label="Temperatur- und Niederschlagsverlauf"
>
<defs>
<clipPath id={`${clipId}-past`}>
<rect x="-20" y="0" width={currentX + 20} height={chartHeight} />
</clipPath>
<clipPath id={`${clipId}-future`}>
<rect x={currentX} y="0" width={width - currentX + 20} height={chartHeight} />
</clipPath>
<linearGradient id={`${clipId}-area`} gradientUnits="userSpaceOnUse" x1="0" y1={yTop} x2="0" y2={yBottom}>
<stop offset="0" stopColor="#fff" stopOpacity="0.16" />
<stop offset="1" stopColor="#fff" stopOpacity="0" />
</linearGradient>
</defs>
<line x1={left} y1={yBottom} x2={right} y2={yBottom} />
<line x1={left} y1="130" x2={right} y2="130" />
<line x1={left} y1={yTop} x2={right} y2={yTop} />
<g className="mirror-weather__rain-bars">
{entries.map((entry, index) => {
const height = Math.max(0, entry.precipitationProbability * 0.28)
return height > 0 ? (
<g className={isPast(entry.time, now) ? 'mirror-weather__chart-past' : undefined} key={entry.time}>
<rect x={xAt(index) - 5} y={244 - height} width="10" height={height} rx="2">
<title>{entry.precipitationProbability}% Regenwahrscheinlichkeit</title>
</rect>
</g>
) : null
})}
</g>
{/* The same curve twice, clipped at "now": the past part is dimmed and there is no join to kink. */}
<g className="mirror-weather__chart-past" clipPath={`url(#${clipId}-past)`}>
<path className="mirror-weather__chart-area" d={areaPath} fill={`url(#${clipId}-area)`} />
<path className="mirror-weather__chart-temp" d={path} />
</g>
<g clipPath={`url(#${clipId}-future)`}>
<path className="mirror-weather__chart-area" d={areaPath} fill={`url(#${clipId}-area)`} />
<path className="mirror-weather__chart-temp" d={path} />
</g>
<line className="mirror-weather__chart-now" x1={currentX} y1="32" x2={currentX} y2="244" />
<circle className="mirror-weather__chart-now-marker" cx={currentX} cy="244" r="3" />
{temperatures.map((value, index) => (
<g className={isPast(entries[index].time, now) ? 'mirror-weather__chart-past' : undefined} key={entries[index].time}>
<circle className="mirror-weather__chart-point" cx={value.x} cy={value.y} r="4.5" stroke="#000" strokeWidth="2" />
</g>
))}
</svg>
<div className="mirror-weather__chart-labels" aria-hidden="true">
{scaleLabels.map((label) => (
<span className="mirror-weather__chart-scale" key={label.y} style={{ left: pct(first.x - 12), top: label.y }}>
{label.value}°
</span>
))}
{entries.map((entry, index) => {
const height = Math.max(0, entry.precipitationProbability * 0.28)
return height > 0 ? (
<g className={isPast(entry.time, now) ? 'mirror-weather__chart-past' : undefined} key={entry.time}>
<rect x={xAt(index) - 5} y={244 - height} width="10" height={height} rx="2">
<title>{entry.precipitationProbability}% Regenwahrscheinlichkeit</title>
</rect>
<text x={xAt(index)} y={240 - height}>{entry.precipitationProbability}%</text>
</g>
<span
className={`mirror-weather__chart-rain${isPast(entry.time, now) ? ' mirror-weather__chart-past' : ''}`}
key={`rain-${entry.time}`}
style={{ left: pct(xAt(index)), top: 240 - height }}
>
{entry.precipitationProbability}%
</span>
) : null
})}
</g>
{/* The same curve twice, clipped at "now": the past part is dimmed and there is no join to kink. */}
<g className="mirror-weather__chart-past" clipPath={`url(#${clipId}-past)`}>
<path className="mirror-weather__chart-temp" d={path} />
</g>
<g clipPath={`url(#${clipId}-future)`}>
<path className="mirror-weather__chart-temp" d={path} />
</g>
<line className="mirror-weather__chart-now" x1={currentX} y1="32" x2={currentX} y2="244" />
<circle className="mirror-weather__chart-now-marker" cx={currentX} cy="244" r="3" />
{temperatures.map((value, index) => (
<g className={isPast(entries[index].time, now) ? 'mirror-weather__chart-past' : undefined} key={entries[index].time}>
<circle cx={value.x} cy={value.y} r="4" />
<text className="mirror-weather__chart-value" x={value.x} y={value.y - 11}>{entries[index].temperatureC}°</text>
</g>
))}
</svg>
{temperatures.map((value, index) => (
<span
className={`mirror-weather__chart-value${isPast(entries[index].time, now) ? ' mirror-weather__chart-past' : ''}`}
key={`value-${entries[index].time}`}
style={{ left: pct(value.x), top: value.y - 9 }}
>
{entries[index].temperatureC}°
</span>
))}
</div>
</div>
)
}

Expand Down
14 changes: 9 additions & 5 deletions web/src/features/modules/weather/weather.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
.mirror-weather__hourly { margin-top: 3.5rem; text-align: left; width: 100%; }
.mirror-weather__forecast h2 { color: #fff; font-size: clamp(1.35rem,1.7vw,1.8rem); font-weight: 500; letter-spacing: .08rem; margin: 0 0 1.15rem; text-transform: uppercase; }

.mirror-weather__chart { display: block; height: 265px; margin-top: .9rem; overflow: visible; width: 100%; }
.mirror-weather__chart-box { margin-top: .9rem; position: relative; width: 100%; }
.mirror-weather__chart { display: block; height: 265px; overflow: visible; shape-rendering: geometricPrecision; width: 100%; }
.mirror-weather__chart line { stroke: rgba(255,255,255,.15); stroke-dasharray: 3 6; stroke-width: 1; }
.mirror-weather__chart-now { stroke: rgba(255,255,255,.8) !important; stroke-dasharray: 3 5 !important; stroke-width: 1.5 !important; }
.mirror-weather__chart-temp { fill: none; stroke: #fff; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2.5; }
.mirror-weather__chart-temp { fill: none; shape-rendering: geometricPrecision; stroke: #fff; stroke-linecap: round; stroke-linejoin: round; stroke-width: 3; }
.mirror-weather__chart-area { stroke: none; }
.mirror-weather__chart circle { fill: #fff; }
.mirror-weather__chart-scale { fill: var(--mirror-muted); font-size: 27px; text-anchor: start; }
.mirror-weather__chart-value { fill: #fff; font-size: 27px; text-anchor: middle; }
.mirror-weather__chart-labels { inset: 0; pointer-events: none; position: absolute; }
.mirror-weather__chart-labels > span { font-size: 27px; line-height: 1; position: absolute; white-space: nowrap; }
.mirror-weather__chart-labels .mirror-weather__chart-scale { color: var(--mirror-muted); font-weight: 500; transform: translate(-100%, calc(-100% - 4px)); }
.mirror-weather__chart-labels .mirror-weather__chart-value { color: #fff; font-weight: 600; transform: translate(-50%, -100%); }
.mirror-weather__chart-labels .mirror-weather__chart-rain { color: rgba(255,255,255,.8); font-size: 22px; font-weight: 500; transform: translate(-50%, -100%); }
.mirror-weather__rain-bars rect { fill: rgba(255,255,255,.42); }
.mirror-weather__rain-bars text { fill: rgba(255,255,255,.8); font-size: 24px; text-anchor: middle; }
.mirror-weather__chart-past { opacity: .86; }
.mirror-weather__chart-now-marker { fill: #fff; }

Expand Down
Loading