Cleans up the charts
This commit is contained in:
parent
7b9954c624
commit
160b773641
95
charts.go
95
charts.go
@ -10,6 +10,16 @@ import (
|
|||||||
chart "github.com/wcharczuk/go-chart"
|
chart "github.com/wcharczuk/go-chart"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var weekDaysOrder = []time.Weekday{
|
||||||
|
time.Monday,
|
||||||
|
time.Tuesday,
|
||||||
|
time.Wednesday,
|
||||||
|
time.Thursday,
|
||||||
|
time.Friday,
|
||||||
|
time.Saturday,
|
||||||
|
time.Sunday,
|
||||||
|
}
|
||||||
|
|
||||||
type renderableChart interface {
|
type renderableChart interface {
|
||||||
Render(chart.RendererProvider, io.Writer) error
|
Render(chart.RendererProvider, io.Writer) error
|
||||||
}
|
}
|
||||||
@ -39,7 +49,7 @@ func (a *App) renderChart(w http.ResponseWriter, c renderableChart) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeHistoryChart(title string, points []ServerPoint) chart.Chart {
|
func makeHistoryChart(title string, showLegend bool, points []ServerPoint) chart.Chart {
|
||||||
// TODO BUG: one day is missing randomly (usually the 3rd day in the range) in the chart
|
// TODO BUG: one day is missing randomly (usually the 3rd day in the range) in the chart
|
||||||
var xVals []time.Time
|
var xVals []time.Time
|
||||||
var yVals []float64
|
var yVals []float64
|
||||||
@ -53,7 +63,7 @@ func makeHistoryChart(title string, points []ServerPoint) chart.Chart {
|
|||||||
XValues: xVals,
|
XValues: xVals,
|
||||||
YValues: yVals,
|
YValues: yVals,
|
||||||
}
|
}
|
||||||
li := &chart.LinearRegressionSeries{
|
lr := &chart.LinearRegressionSeries{
|
||||||
Name: "Linear regression",
|
Name: "Linear regression",
|
||||||
InnerSeries: series,
|
InnerSeries: series,
|
||||||
}
|
}
|
||||||
@ -63,34 +73,35 @@ func makeHistoryChart(title string, points []ServerPoint) chart.Chart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c := chart.Chart{
|
c := chart.Chart{
|
||||||
Title: title,
|
|
||||||
TitleStyle: chart.Style{
|
|
||||||
Show: true,
|
|
||||||
},
|
|
||||||
Background: chart.Style{
|
Background: chart.Style{
|
||||||
Padding: chart.Box{
|
Padding: chart.Box{
|
||||||
Left: 120,
|
Top: 40,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
XAxis: chart.XAxis{
|
XAxis: chart.XAxis{
|
||||||
Style: chart.Style{
|
Style: chart.StyleShow(),
|
||||||
Show: true,
|
ValueFormatter: func(v interface{}) string {
|
||||||
|
t := int64(v.(float64))
|
||||||
|
return time.Unix(0, t).Format("Jan 02 15:04")
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
YAxis: chart.YAxis{
|
YAxis: chart.YAxis{
|
||||||
Style: chart.Style{
|
Style: chart.StyleShow(),
|
||||||
Show: true,
|
ValueFormatter: func(v interface{}) string {
|
||||||
|
return fmt.Sprintf("%.0f", v)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Series: []chart.Series{
|
Series: []chart.Series{
|
||||||
series,
|
series,
|
||||||
li,
|
lr,
|
||||||
sma,
|
sma,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if showLegend {
|
||||||
// Add a legend
|
// Add a legend
|
||||||
c.Elements = []chart.Renderable{
|
c.Elements = []chart.Renderable{
|
||||||
chart.LegendLeft(&c),
|
chart.LegendThin(&c),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
@ -113,53 +124,27 @@ func makeDayAverageChart(title string, points []ServerPoint) chart.BarChart {
|
|||||||
avgDays[day] = float64(avg)
|
avgDays[day] = float64(avg)
|
||||||
}
|
}
|
||||||
|
|
||||||
prettyName := func(d time.Weekday) string {
|
var bars []chart.Value
|
||||||
return fmt.Sprintf("%s (%.0f)", d, avgDays[d])
|
for _, d := range weekDaysOrder {
|
||||||
|
bars = append(bars, chart.Value{
|
||||||
|
Label: fmt.Sprintf("%s (%.0f)", d, avgDays[d]),
|
||||||
|
Value: avgDays[d],
|
||||||
|
Style: chart.Style{
|
||||||
|
StrokeColor: chart.ColorBlue,
|
||||||
|
FillColor: chart.ColorBlue,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return chart.BarChart{
|
return chart.BarChart{
|
||||||
Title: title,
|
BarWidth: 50,
|
||||||
TitleStyle: chart.Style{
|
XAxis: chart.StyleShow(),
|
||||||
Show: true,
|
|
||||||
},
|
|
||||||
BarWidth: 60,
|
|
||||||
XAxis: chart.Style{
|
|
||||||
Show: true,
|
|
||||||
},
|
|
||||||
YAxis: chart.YAxis{
|
YAxis: chart.YAxis{
|
||||||
Style: chart.Style{
|
Style: chart.StyleShow(),
|
||||||
Show: true,
|
ValueFormatter: func(v interface{}) string {
|
||||||
},
|
return fmt.Sprintf("%.0f", v)
|
||||||
},
|
|
||||||
Bars: []chart.Value{
|
|
||||||
chart.Value{
|
|
||||||
Label: prettyName(time.Monday),
|
|
||||||
Value: avgDays[time.Monday],
|
|
||||||
},
|
|
||||||
chart.Value{
|
|
||||||
Label: prettyName(time.Tuesday),
|
|
||||||
Value: avgDays[time.Tuesday],
|
|
||||||
},
|
|
||||||
chart.Value{
|
|
||||||
Label: prettyName(time.Wednesday),
|
|
||||||
Value: avgDays[time.Wednesday],
|
|
||||||
},
|
|
||||||
chart.Value{
|
|
||||||
Label: prettyName(time.Thursday),
|
|
||||||
Value: avgDays[time.Thursday],
|
|
||||||
},
|
|
||||||
chart.Value{
|
|
||||||
Label: prettyName(time.Friday),
|
|
||||||
Value: avgDays[time.Friday],
|
|
||||||
},
|
|
||||||
chart.Value{
|
|
||||||
Label: prettyName(time.Saturday),
|
|
||||||
Value: avgDays[time.Saturday],
|
|
||||||
},
|
|
||||||
chart.Value{
|
|
||||||
Label: prettyName(time.Sunday),
|
|
||||||
Value: avgDays[time.Sunday],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Bars: bars,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,13 +71,13 @@ func (a *App) pageDailyChart(w http.ResponseWriter, r *http.Request, vars handle
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c := makeHistoryChart("Daily history", points)
|
c := makeHistoryChart("Daily history", true, points)
|
||||||
return a.renderChart(w, c)
|
return a.renderChart(w, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) pageWeeklyChart(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
func (a *App) pageWeeklyChart(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
points, err := a.store.GetSingleServerHistory(id, 7)
|
points, err := a.store.GetSingleServerHistory(id, 6)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ func (a *App) pageWeeklyChart(w http.ResponseWriter, r *http.Request, vars handl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c := makeHistoryChart("Weekly history", points)
|
c := makeHistoryChart("Weekly history", false, points)
|
||||||
return a.renderChart(w, c)
|
return a.renderChart(w, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user