/api/trends has 1 pie of median

main
bel 2024-07-21 17:12:52 -06:00
parent b8c78fe55e
commit 6be00d6423
2 changed files with 22 additions and 6 deletions

View File

@ -143,18 +143,31 @@ func (router Router) APITrends(w http.ResponseWriter, r *http.Request) {
}
slices.Sort(months)
fmt.Fprintln(w, "<!DOCTYPE html>")
catToMonth := map[string][]int{}
for _, month := range months {
balances := monthsToDeltas[month].Balances().WithBPIs(bpis)
chart := view.NewChart("pie")
for category, balance := range balances {
chart.AddY(category, []int{int(balance[ledger.USD])})
catToMonth[category] = append(catToMonth[category], int(balance[ledger.USD]))
}
fmt.Fprintln(w, "<h2>", month, "</h2>")
}
chart := view.NewChart("pie")
ttl := 0
for cat, month := range catToMonth {
for i := 0; i < len(months)-len(month); i++ {
month = append(month, 0)
}
slices.Sort(month)
median := month[len(month)/2]
ttl += median
if median > 50 {
chart.AddY(cat, []int{median})
}
}
fmt.Fprintln(w, "<!DOCTYPE html><h2>Median Spending ($", ttl, ")</h2>")
if err := chart.Render(w); err != nil {
panic(err)
}
}
}
func (router Router) APIAmend(w http.ResponseWriter, r *http.Request) {

View File

@ -124,6 +124,9 @@ func (pie *Pie) AddX(v interface{}) {
func (pie *Pie) Render(w io.Writer) error {
pie.AddSeries("", pie.series)
pie.SetGlobalOptions(charts.WithLegendOpts(opts.Legend{
Show: false,
}))
return pie.Pie.Render(w)
}