stuff
This commit is contained in:
1 parent
1275e5a821
commit
ff9860023b
3 files changed
+50
-6
No files matched your search
@@ -157,7 +157,7 @@ func (s *server) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
updatedLabel = updatedAt.Format("02.01.2006 15:04:05")
|
||||
}
|
||||
if err := page.Execute(w, map[string]any{
|
||||
"Watts": watts,
|
||||
"Watts": formatGermanNumber(watts),
|
||||
"UpdatedAt": updatedLabel,
|
||||
"HistoryChart": buildHistoryChart(history, s.chartMax),
|
||||
}); err != nil {
|
||||
@@ -165,6 +165,34 @@ func (s *server) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func formatGermanNumber(n int) string {
|
||||
s := strconv.Itoa(n)
|
||||
if len(s) <= 3 {
|
||||
return s
|
||||
}
|
||||
|
||||
neg := false
|
||||
if s[0] == '-' {
|
||||
neg = true
|
||||
s = s[1:]
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
if neg {
|
||||
b.WriteByte('-')
|
||||
}
|
||||
rem := len(s) % 3
|
||||
if rem == 0 {
|
||||
rem = 3
|
||||
}
|
||||
b.WriteString(s[:rem])
|
||||
for i := rem; i < len(s); i += 3 {
|
||||
b.WriteByte('.')
|
||||
b.WriteString(s[i : i+3])
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func buildHistoryChart(history []sample, maxWatts int) template.HTML {
|
||||
const (
|
||||
width = 640
|
||||
@@ -242,8 +270,8 @@ func buildHistoryChart(history []sample, maxWatts int) template.HTML {
|
||||
fmt.Fprintf(&b, `<path d="M %.1f %.1f V %.1f"/>`, x1, yTop, yBottom)
|
||||
b.WriteString(`</g>`)
|
||||
b.WriteString(`<g fill="#64748b" font-size="12" font-family="system-ui, sans-serif">`)
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">%d W</text>`, x0-10, int(yTop)+14, maxV)
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">%d W</text>`, x0-10, int(yMid)+14, midV)
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">%s W</text>`, x0-10, int(yTop)+14, formatGermanNumber(maxV))
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">%s W</text>`, x0-10, int(yMid)+14, formatGermanNumber(midV))
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">0 W</text>`, x0-10, int(yBottom)+14)
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="middle">%s</text>`, x0, int(height)-10, start.Format("15:04"))
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="middle">%s</text>`, x0+(plotW/2), int(height)-10, midT.Format("15:04"))
|
||||
|
||||
Reference in new issue
Block a user