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, ``, w, h, html.EscapeString(title)) var mx float64 for _, v := range vals { if float64(v) > mx { mx = float64(v) } } top := niceMax(mx) pw, ph := w-ml-mr, h-mt-mb for i := 0; i <= 4; i++ { y := mt + ph - ph*float64(i)/4 fmt.Fprintf(&sb, ``, ml, y, w-mr, y) fmt.Fprintf(&sb, `%s`, ml-4, y+3, fi(top*float64(i)/4)) } n := len(vals) if n > 1 { xs := func(i int) float64 { return ml + pw*float64(i)/float64(n-1) } ys := func(v uint32) float64 { return mt + ph - ph*float64(v)/top } var line strings.Builder for i, v := range vals { if i == 0 { fmt.Fprintf(&line, "M%.1f %.1f", xs(i), ys(v)) } else { fmt.Fprintf(&line, "L%.1f %.1f", xs(i), ys(v)) } } fmt.Fprintf(&sb, ``, line.String(), xs(n-1), mt+ph, xs(0), mt+ph) fmt.Fprintf(&sb, ``, line.String()) if step > 0 { for i := 0; i < 5; i++ { idx := (n - 1) * i / 4 ts := start + int64(idx)*step lbl := time.Unix(ts, 0).UTC().Format("02/01") if step < 86400 && int64(n)*step <= 3*86400 { lbl = time.Unix(ts, 0).UTC().Format("02/01 15h") } anchor := "middle" if i == 0 { anchor = "start" } else if i == 4 { anchor = "end" } fmt.Fprintf(&sb, `%s`, xs(idx), h-6, anchor, lbl) } } } else if n == 1 { fmt.Fprintf(&sb, ``, ml+pw/2, mt+ph-ph*float64(vals[0])/top) } sb.WriteString(``) 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, ``, w, h, html.EscapeString(title)) bw := w - lw - rw for i, it := range items { y := rh * float64(i) l := it.N / mx * bw if it.N > 0 && l < 1.5 { l = 1.5 } fmt.Fprintf(&sb, `%s`, lw-6, y+13, html.EscapeString(clip(it.Label, 20))) fmt.Fprintf(&sb, ``, lw, y+3.5, l, color) fmt.Fprintf(&sb, `%s`, lw+l+4, y+13, fi(it.N)) } sb.WriteString(``) 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, ``, html.EscapeString(title)) const r = 44.0 circ := 2 * math.Pi * r sb.WriteString(``) if total > 0 { off := 0.0 for i, it := range items { if it.N <= 0 { continue } l := it.N / total * circ fmt.Fprintf(&sb, ``, reportPalette[i%len(reportPalette)], l, circ-l, -off) off += l } } sb.WriteString(``) 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() }