Makes the barcharts sortable and indicate "current active bar" for the day/hour
This commit is contained in:
parent
946737d563
commit
6d642f6139
64
charts.go
64
charts.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
chart "github.com/wcharczuk/go-chart"
|
chart "github.com/wcharczuk/go-chart"
|
||||||
@ -106,7 +107,8 @@ func makeHistoryChart(points []ServerPoint, showLegend bool) chart.Chart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: The chart won't be renderable unless we've got at least two days/hours of history
|
// NOTE: The chart won't be renderable unless we've got at least two days/hours of history
|
||||||
func makeAverageChart(values map[int][]int, fnFormat func(int, float64) string) chart.BarChart {
|
func makeAverageChart(values map[int][]int, fnFormat func(int, float64) string, fnSort func([]int) []int) chart.BarChart {
|
||||||
|
var keys []int
|
||||||
avg := make(map[int]float64)
|
avg := make(map[int]float64)
|
||||||
for i, vl := range values {
|
for i, vl := range values {
|
||||||
sum := 0
|
sum := 0
|
||||||
@ -114,13 +116,14 @@ func makeAverageChart(values map[int][]int, fnFormat func(int, float64) string)
|
|||||||
sum += v
|
sum += v
|
||||||
}
|
}
|
||||||
avg[i] = float64(sum / len(vl))
|
avg[i] = float64(sum / len(vl))
|
||||||
|
keys = append(keys, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
var bars []chart.Value
|
var bars []chart.Value
|
||||||
for i := 0; i < len(avg); i++ {
|
for _, k := range fnSort(keys) {
|
||||||
bars = append(bars, chart.Value{
|
bars = append(bars, chart.Value{
|
||||||
Label: fnFormat(i, avg[i]),
|
Label: fnFormat(k, avg[k]),
|
||||||
Value: avg[i],
|
Value: avg[k],
|
||||||
Style: chart.Style{
|
Style: chart.Style{
|
||||||
StrokeColor: chart.ColorBlue,
|
StrokeColor: chart.ColorBlue,
|
||||||
FillColor: chart.ColorBlue,
|
FillColor: chart.ColorBlue,
|
||||||
@ -149,3 +152,56 @@ func makeAverageChart(values map[int][]int, fnFormat func(int, float64) string)
|
|||||||
Bars: bars,
|
Bars: bars,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shortcut/helper func for the calling handler
|
||||||
|
func avgDailyChart(points []ServerPoint) chart.BarChart {
|
||||||
|
days := make(map[int][]int)
|
||||||
|
for _, p := range points {
|
||||||
|
d := int(p.Time.Weekday())
|
||||||
|
days[d] = append(days[d], p.Players)
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
formatter := func(i int, f float64) string {
|
||||||
|
d := time.Weekday(i)
|
||||||
|
extra := ""
|
||||||
|
if d == now.Weekday() {
|
||||||
|
extra = "*"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s%s", d, extra)
|
||||||
|
}
|
||||||
|
sorter := func(keys []int) []int {
|
||||||
|
sort.Slice(keys, func(i, j int) bool {
|
||||||
|
return keys[i] < keys[j]
|
||||||
|
})
|
||||||
|
// Fucking wankers and their fucking sundays
|
||||||
|
if keys[0] == int(time.Sunday) {
|
||||||
|
keys = append(keys[1:], int(time.Sunday))
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
return makeAverageChart(days, formatter, sorter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shortcut/helper func for the calling handler
|
||||||
|
func avgHourlyChart(points []ServerPoint) chart.BarChart {
|
||||||
|
hours := make(map[int][]int)
|
||||||
|
for _, p := range points {
|
||||||
|
h := p.Time.Hour()
|
||||||
|
hours[h] = append(hours[h], p.Players)
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
formatter := func(i int, f float64) string {
|
||||||
|
extra := ""
|
||||||
|
if i == now.Hour() {
|
||||||
|
extra = "*"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%02d%s", i, extra)
|
||||||
|
}
|
||||||
|
sorter := func(keys []int) []int {
|
||||||
|
sort.Slice(keys, func(i, j int) bool {
|
||||||
|
return keys[i] < keys[j]
|
||||||
|
})
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
return makeAverageChart(hours, formatter, sorter)
|
||||||
|
}
|
||||||
|
|||||||
22
handlers.go
22
handlers.go
@ -3,7 +3,6 @@ package ss13_se
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *App) pageIndex(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
func (a *App) pageIndex(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
||||||
@ -105,16 +104,7 @@ func (a *App) pageAverageDailyChart(w http.ResponseWriter, r *http.Request, vars
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
days := make(map[int][]int)
|
c := avgDailyChart(points)
|
||||||
for _, p := range points {
|
|
||||||
d := int(p.Time.Weekday())
|
|
||||||
days[d] = append(days[d], p.Players)
|
|
||||||
}
|
|
||||||
formatter := func(i int, f float64) string {
|
|
||||||
d := time.Weekday(i)
|
|
||||||
return fmt.Sprintf("%s", d)
|
|
||||||
}
|
|
||||||
c := makeAverageChart(days, formatter)
|
|
||||||
return a.renderChart(w, c)
|
return a.renderChart(w, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,14 +121,6 @@ func (a *App) pageAverageHourlyChart(w http.ResponseWriter, r *http.Request, var
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hours := make(map[int][]int)
|
c := avgHourlyChart(points)
|
||||||
for _, p := range points {
|
|
||||||
h := p.Time.Hour()
|
|
||||||
hours[h] = append(hours[h], p.Players)
|
|
||||||
}
|
|
||||||
formatter := func(i int, f float64) string {
|
|
||||||
return fmt.Sprintf("%02d:00", i)
|
|
||||||
}
|
|
||||||
c := makeAverageChart(hours, formatter)
|
|
||||||
return a.renderChart(w, c)
|
return a.renderChart(w, c)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user