summaryrefslogtreecommitdiff
path: root/internal/render/svg.go
blob: dcb9af5796864b6f050b93c773181d2ddeb47d24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package render

import (
	"encoding/xml"
	"fmt"
	"math"
	"strings"
	"time"

	"github.com/lukaszkasprzak/prognosis/internal/config"
	"github.com/lukaszkasprzak/prognosis/internal/i18n"
	"github.com/lukaszkasprzak/prognosis/internal/openmeteo"
)

// SVGFields are the hourly fields a meteogram needs, whatever columns the table
// happens to show. Requested in addition to the table's own fields.
var SVGFields = []string{
	"temperature_2m", "apparent_temperature", "precipitation",
	"precipitation_probability", "relative_humidity_2m", "wind_speed_10m",
}

// Layout constants, in user units (which are CSS pixels at 1:1).
const (
	svgMarginLeft   = 52
	svgMarginRight  = 44
	svgMarginTop    = 64
	svgMarginBottom = 34
	svgPanelGap     = 24
	svgHourWidth    = 26 // per hour, before clamping
	svgMinWidth     = 640
	svgMaxWidth     = 1800
)

// panel is one stacked chart sharing the figure's time axis.
type panel struct {
	title  string
	height int
	draw   func(b *strings.Builder, p panelBox)
}

// panelBox is a panel's rectangle on the canvas.
type panelBox struct {
	x, y, w, h int
}

// SVG renders the forecast as a standalone meteogram.
//
// Written directly rather than through a plotting library or gnuplot: SVG is
// markup, so this keeps prognosis dependency-free and works identically on a
// machine that has no plotting tools at all -- the phone, for instance.
func SVG(v View, cfg config.Config) string {
	rows := v.Rows
	if len(rows) == 0 {
		return ""
	}
	cat := i18n.For(cfg.DisplayLang)

	width := len(rows) * svgHourWidth
	if width < svgMinWidth {
		width = svgMinWidth
	}
	if width > svgMaxWidth {
		width = svgMaxWidth
	}
	plotW := width - svgMarginLeft - svgMarginRight

	series := func(field string) []float64 {
		out := make([]float64, len(rows))
		for i, r := range rows {
			out[i], _ = r.Val(field)
		}
		return out
	}
	temp := series("temperature_2m")
	feels := series("apparent_temperature")
	rain := series("precipitation")
	prob := series("precipitation_probability")
	hum := series("relative_humidity_2m")

	panels := []panel{
		{title: cat.Header("temp") + " °", height: 170, draw: func(b *strings.Builder, p panelBox) {
			plo, phi := paddedRange(append(append([]float64{}, temp...), feels...))
			lo, hi, lines := niceTicks(plo, phi, 6)
			svgGrid(b, p, lo, hi, lines, "%.0f")
			svgLine(b, p, feels, lo, hi, "#c98", 1.5, true)
			svgLine(b, p, temp, lo, hi, "#c33", 2.2, false)
			svgLegend(b, p, cat.Header("temp"), cat.Header("feels"))
		}},
		{title: cat.Header("mm") + " (" + cat.Header("rain") + " %)", height: 104, draw: func(b *strings.Builder, p panelBox) {
			hiRain := maxOf(rain)
			if hiRain < 1 {
				hiRain = 1 // an empty panel still needs a sane scale
			}
			svgGrid(b, p, 0, hiRain, 3, "%.1f")
			// Two units share this panel, so the probability gets its own axis
			// on the right. Without it the dashed line reads against millimetres.
			svgRightAxis(b, p, 0, 100, 3, "%.0f%%", "#69b")
			svgBars(b, p, rain, 0, hiRain, "#39c")
			svgLine(b, p, prob, 0, 100, "#69b", 1.2, true)
		}},
		{title: cat.Header("humidity") + " %", height: 104, draw: func(b *strings.Builder, p panelBox) {
			svgGrid(b, p, 0, 100, 3, "%.0f")
			svgLine(b, p, hum, 0, 100, "#4a7", 1.8, false)
		}},
	}

	height := svgMarginTop + svgMarginBottom
	for i, p := range panels {
		height += p.height
		if i > 0 {
			height += svgPanelGap
		}
	}

	var b strings.Builder
	fmt.Fprintf(&b, `<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" `+
		`viewBox="0 0 %d %d" font-family="DejaVu Sans, Helvetica, sans-serif" font-size="11">`+"\n",
		width, height, width, height)
	fmt.Fprintf(&b, `<rect width="%d" height="%d" fill="#ffffff"/>`+"\n", width, height)

	title := fmt.Sprintf("%s   %s", v.Label, cat.Date(rows[0].When))
	fmt.Fprintf(&b, `<text x="%d" y="26" font-size="15" fill="#222">%s</text>`+"\n",
		svgMarginLeft, escape(title))
	if sun, ok := v.Sun[rows[0].When.Format("2006-01-02")]; ok {
		fmt.Fprintf(&b, `<text x="%d" y="43" fill="#777">%s %s  %s %s</text>`+"\n",
			svgMarginLeft, escape(sun[0]), escape(cat.Word("up")),
			escape(sun[1]), escape(cat.Word("down")))
	}

	y := svgMarginTop
	for i, p := range panels {
		box := panelBox{x: svgMarginLeft, y: y, w: plotW, h: p.height}
		svgNightBands(&b, box, rows, v.Sun)
		svgTimeGrid(&b, box, rows)
		p.draw(&b, box)
		fmt.Fprintf(&b, `<rect x="%d" y="%d" width="%d" height="%d" fill="none" stroke="#bbb"/>`+"\n",
			box.x, box.y, box.w, box.h)
		fmt.Fprintf(&b, `<text x="%d" y="%d" fill="#555">%s</text>`+"\n",
			box.x+4, box.y-4, escape(p.title))
		if i == len(panels)-1 {
			svgTimeAxis(&b, box, rows)
		}
		y += p.height + svgPanelGap
	}

	b.WriteString("</svg>\n")
	return b.String()
}

// svgNightBands shades the hours between sunset and sunrise, which is what makes
// a meteogram readable at a glance.
func svgNightBands(b *strings.Builder, p panelBox, rows []openmeteo.Row, sun map[string][2]string) {
	start := -1
	for i, r := range rows {
		night := isNight(r.When, sun)
		if night && start < 0 {
			start = i
		}
		if (!night || i == len(rows)-1) && start >= 0 {
			end := i
			if night {
				end = i + 1
			}
			x0 := xAt(p, start, len(rows))
			x1 := xAt(p, end, len(rows))
			fmt.Fprintf(b, `<rect x="%.1f" y="%d" width="%.1f" height="%d" fill="#eef1f6"/>`+"\n",
				x0, p.y, math.Max(x1-x0, 1), p.h)
			start = -1
		}
	}
}

// isNight reports whether an hour falls outside that date's sunrise..sunset.
// Without sun data it reports false: no shading beats wrong shading.
func isNight(t time.Time, sun map[string][2]string) bool {
	s, ok := sun[t.Format("2006-01-02")]
	if !ok {
		return false
	}
	rise, err1 := time.Parse("15:04", s[0])
	set, err2 := time.Parse("15:04", s[1])
	if err1 != nil || err2 != nil {
		return false
	}
	mins := t.Hour()*60 + t.Minute()
	return mins < rise.Hour()*60+rise.Minute() || mins >= set.Hour()*60+set.Minute()
}

// svgTimeGrid draws a faint line per labelled hour and a stronger one at each
// midnight, so a value can be traced back to a time without counting squares.
func svgTimeGrid(b *strings.Builder, p panelBox, rows []openmeteo.Row) {
	every := hourStep(len(rows))
	for i, r := range rows {
		midnight := r.When.Hour() == 0
		if !midnight && i%every != 0 {
			continue
		}
		x := xAt(p, i, len(rows)-1)
		stroke, w := "#eeeeee", 1.0
		if midnight {
			stroke, w = "#9aa3ad", 1.4
		}
		fmt.Fprintf(b, `<line x1="%.1f" y1="%d" x2="%.1f" y2="%d" stroke="%s" stroke-width="%.1f"/>`+"\n",
			x, p.y, x, p.y+p.h, stroke, w)
	}
}

// hourStep is how often the time axis is labelled, kept in one place so the
// grid and the labels cannot disagree.
func hourStep(n int) int { return 1 + n/16 }

func svgGrid(b *strings.Builder, p panelBox, lo, hi float64, lines int, format string) {
	for i := 0; i < lines; i++ {
		frac := float64(i) / float64(lines-1)
		y := float64(p.y+p.h) - frac*float64(p.h)
		value := lo + (hi-lo)*frac
		fmt.Fprintf(b, `<line x1="%d" y1="%.1f" x2="%d" y2="%.1f" stroke="#e4e4e4"/>`+"\n",
			p.x, y, p.x+p.w, y)
		fmt.Fprintf(b, `<text x="%d" y="%.1f" text-anchor="end" fill="#888">%s</text>`+"\n",
			p.x-6, y+3.5, escape(fmt.Sprintf(format, value)))
	}
}

func svgLine(b *strings.Builder, p panelBox, vals []float64, lo, hi float64, colour string, w float64, dashed bool) {
	if len(vals) == 0 {
		return
	}
	var pts []string
	for i, v := range vals {
		pts = append(pts, fmt.Sprintf("%.1f,%.1f", xAt(p, i, len(vals)-1), yAt(p, v, lo, hi)))
	}
	dash := ""
	if dashed {
		dash = ` stroke-dasharray="4 3"`
	}
	fmt.Fprintf(b, `<polyline points="%s" fill="none" stroke="%s" stroke-width="%.1f" `+
		`stroke-linejoin="round"%s/>`+"\n", strings.Join(pts, " "), colour, w, dash)
}

func svgBars(b *strings.Builder, p panelBox, vals []float64, lo, hi float64, colour string) {
	if len(vals) < 2 {
		return
	}
	bw := float64(p.w) / float64(len(vals)) * 0.7
	for i, v := range vals {
		if v <= 0 {
			continue
		}
		y := yAt(p, v, lo, hi)
		x := xAt(p, i, len(vals)-1) - bw/2
		fmt.Fprintf(b, `<rect x="%.1f" y="%.1f" width="%.1f" height="%.1f" fill="%s" opacity="0.75"/>`+"\n",
			x, y, bw, float64(p.y+p.h)-y, colour)
	}
}

// svgTimeAxis labels the hours, with dates in bold at midnight.
//
// A date label is wider than an hour, so any hour that would land under one is
// dropped: otherwise "23" and "28.08" render on top of each other as "2328.08".
func svgTimeAxis(b *strings.Builder, p panelBox, rows []openmeteo.Row) {
	const collision = 22.0 // user units either side of a date label

	var dateX []float64
	for i, r := range rows {
		if r.When.Hour() == 0 {
			dateX = append(dateX, xAt(p, i, len(rows)-1))
		}
	}
	near := func(x float64) bool {
		for _, dx := range dateX {
			if math.Abs(x-dx) < collision {
				return true
			}
		}
		return false
	}

	every := hourStep(len(rows))
	for i, r := range rows {
		x := xAt(p, i, len(rows)-1)
		switch {
		case r.When.Hour() == 0:
			fmt.Fprintf(b, `<text x="%.1f" y="%d" text-anchor="middle" font-weight="bold" fill="#222">%s</text>`+"\n",
				x, p.y+p.h+16, escape(r.When.Format("02.01")))
		case i%every == 0 && !near(x):
			fmt.Fprintf(b, `<text x="%.1f" y="%d" text-anchor="middle" fill="#666">%s</text>`+"\n",
				x, p.y+p.h+16, escape(r.When.Format("15")))
		}
	}
}

// svgLegend names the two temperature series, since a dashed line is not
// self-explanatory.
func svgLegend(b *strings.Builder, p panelBox, solid, dashed string) {
	x := float64(p.x+p.w) - 150
	y := float64(p.y) + 14
	fmt.Fprintf(b, `<rect x="%.1f" y="%.1f" width="146" height="18" fill="#ffffff" opacity="0.82"/>`+"\n",
		x-6, y-12)
	fmt.Fprintf(b, `<line x1="%.1f" y1="%.1f" x2="%.1f" y2="%.1f" stroke="#c33" stroke-width="2.2"/>`+"\n",
		x, y-4, x+18, y-4)
	fmt.Fprintf(b, `<text x="%.1f" y="%.1f" fill="#555">%s</text>`+"\n", x+23, y, escape(solid))
	x2 := x + 23 + float64(len(solid))*6 + 12
	fmt.Fprintf(b, `<line x1="%.1f" y1="%.1f" x2="%.1f" y2="%.1f" stroke="#c98" stroke-width="1.5" stroke-dasharray="4 3"/>`+"\n",
		x2, y-4, x2+18, y-4)
	fmt.Fprintf(b, `<text x="%.1f" y="%.1f" fill="#555">%s</text>`+"\n", x2+23, y, escape(dashed))
}

func xAt(p panelBox, i, n int) float64 {
	if n <= 0 {
		return float64(p.x)
	}
	return float64(p.x) + float64(i)/float64(n)*float64(p.w)
}

func yAt(p panelBox, v, lo, hi float64) float64 {
	if hi-lo < 1e-9 {
		return float64(p.y + p.h/2)
	}
	frac := (v - lo) / (hi - lo)
	frac = math.Max(0, math.Min(1, frac))
	return float64(p.y+p.h) - frac*float64(p.h)
}

// svgRightAxis labels the right-hand edge, for a panel carrying a second unit.
func svgRightAxis(b *strings.Builder, p panelBox, lo, hi float64, lines int, format, colour string) {
	for i := 0; i < lines; i++ {
		frac := float64(i) / float64(lines-1)
		y := float64(p.y+p.h) - frac*float64(p.h)
		fmt.Fprintf(b, `<text x="%d" y="%.1f" fill="%s">%s</text>`+"\n",
			p.x+p.w+6, y+3.5, colour, escape(fmt.Sprintf(format, lo+(hi-lo)*frac)))
	}
}

// niceTicks snaps a range to round numbers and returns how many gridlines that
// implies, so every label lands on a multiple of the step. Snapping the ends
// alone is not enough: with a fixed number of lines the values between them
// still come out as 12.5 and 27.5.
func niceTicks(lo, hi float64, maxLines int) (float64, float64, int) {
	span := hi - lo
	if span <= 0 {
		return lo, lo + 1, 2
	}
	step := math.Pow(10, math.Floor(math.Log10(span/float64(maxLines-1))))
	for _, m := range []float64{1, 2, 2.5, 5, 10} {
		if span/(step*m) <= float64(maxLines-1) {
			step *= m
			break
		}
	}
	lo = math.Floor(lo/step) * step
	hi = math.Ceil(hi/step) * step
	return lo, hi, int(math.Round((hi-lo)/step)) + 1
}

// paddedRange leaves a margin above and below so a curve never touches the frame.
func paddedRange(vals []float64) (float64, float64) {
	if len(vals) == 0 {
		return 0, 1
	}
	lo, hi := vals[0], vals[0]
	for _, v := range vals {
		lo, hi = math.Min(lo, v), math.Max(hi, v)
	}
	pad := math.Max((hi-lo)*0.15, 0.5)
	return lo - pad, hi + pad
}

func maxOf(vals []float64) float64 {
	m := 0.0
	for _, v := range vals {
		m = math.Max(m, v)
	}
	return m
}

// escape makes text safe for XML. A place name is user input and can contain &.
func escape(s string) string {
	var b strings.Builder
	xml.EscapeText(&b, []byte(s))
	return b.String()
}