package server
import (
"fmt"
"html"
"math"
"strings"
"time"
)
// Palette of the printed report (light theme, Ciberfauna identity).
var reportPalette = []string{"#f97316", "#087f68", "#f0b35b", "#45d6b0", "#c2410c", "#4b6b61", "#9aa9a2"}
type chartItem struct {
Label string
N float64
}
func clip(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n-1]) + "…"
}
// niceMax rounds a maximum up to 1, 2, 5 x 10^k so the axis reads well.
func niceMax(v float64) float64 {
if v <= 0 {
return 1
}
p := math.Pow(10, math.Floor(math.Log10(v)))
for _, m := range []float64{1, 2, 5, 10} {
if v <= m*p {
return m * p
}
}
return 10 * p
}
// svgArea draws a filled time series. All text is escaped; numbers are formatted here.
func svgArea(vals []uint32, start, step int64, fi func(float64) string, title string) string {
const w, h, ml, mr, mt, mb = 680.0, 150.0, 40.0, 8.0, 8.0, 20.0
var sb strings.Builder
fmt.Fprintf(&sb, ``)
return sb.String()
}
// svgBars draws horizontal bars with the label on the left and the value on the right.
func svgBars(items []chartItem, color string, fi func(float64) string, title string) string {
const w, lw, rw, rh = 300.0, 112.0, 40.0, 19.0
if len(items) == 0 {
return ""
}
var mx float64
for _, it := range items {
if it.N > mx {
mx = it.N
}
}
if mx <= 0 {
mx = 1
}
h := rh*float64(len(items)) + 2
var sb strings.Builder
fmt.Fprintf(&sb, ``)
return sb.String()
}
// svgDonut draws a ring; the legend is HTML next to it (see donutLegend).
func svgDonut(items []chartItem, title string) string {
var total float64
for _, it := range items {
total += it.N
}
var sb strings.Builder
fmt.Fprintf(&sb, ``)
return sb.String()
}
// svgMotif is the cover decoration: a stylised waveform of the urutau's call,
// five whistled notes that descend in pitch and fade, the way the bird sings
// at night. It does not depend on the data, so it never turns into a flat comb.
func svgMotif(_ []uint32) string {
var sb strings.Builder
sb.WriteString(``)
return sb.String()
}