git.haldean.org sousvide / a5f5239
Remove obsolete chart generation Long live the client side! Will Haldean Brown 8 years ago
3 changed file(s) with 0 addition(s) and 93 deletion(s). Raw diff Collapse all Expand all
+0
-2
README less more
0 Dependencies:
1 github.com/ajstarks/svgo
+0
-88
chart.go less more
0 package main
1
2 import (
3 "github.com/ajstarks/svgo"
4 "math"
5 "net/http"
6 )
7
8 const (
9 ImgWidth = 600
10 ImgHeight = 400
11 )
12
13 func (s *SousVide) GenerateChart2(w http.ResponseWriter, r *http.Request) {
14 if len(s.History) == 0 {
15 w.WriteHeader(http.StatusNoContent)
16 return
17 }
18
19 N := len(s.History)
20
21 maxVal := float64(1)
22 for _, h := range s.History {
23 if h.Temp > maxVal {
24 maxVal = h.Temp
25 }
26 if h.Target > maxVal {
27 maxVal = h.Target
28 }
29 }
30 maxY := 10 * math.Ceil(maxVal/10)
31 pxPerUnitY := float64(ImgHeight) / maxY
32
33 maxX := float64(N - 1)
34 pxPerUnitX := float64(ImgWidth) / maxX
35
36 w.Header().Set("Content-type", "image/svg+xml")
37 svgs := svg.New(w)
38 svgs.Start(ImgWidth, ImgHeight)
39 svgs.Title("Temperature history (\u00B0C)")
40
41 // draw "metadata": heating, etc.
42 if N > 1 {
43 for i, h := range s.History {
44 if h.Heating {
45 x0 := int(float64(i) * pxPerUnitX)
46 svgs.Rect(x0, 0, int(math.Ceil(pxPerUnitX)), ImgHeight,
47 "fill:#F7F7F7")
48 }
49 }
50 }
51
52 // draw grid before data so it's under everything
53 even := true
54 step := int(5 * pxPerUnitY)
55 if step >= 1 {
56 for i := 0; i <= ImgHeight; i += step {
57 y := ImgHeight - i
58 if even {
59 svgs.Line(0, y, ImgWidth, y, "stroke:#DDD; stroke-width:1")
60 } else {
61 svgs.Line(0, y, ImgWidth, y, "stroke:#EEE; stroke-width:1")
62 }
63 even = !even
64 }
65 }
66
67 // draw data
68 if N > 1 {
69 xs := make([]int, N)
70 temps := make([]int, N)
71 targets := make([]int, N)
72 for i, h := range s.History {
73 xs[i] = int(float64(i) * pxPerUnitX)
74 temps[i] = ImgHeight - int(h.Temp*pxPerUnitY)
75 targets[i] = ImgHeight - int(h.Target*pxPerUnitY)
76 }
77 svgs.Polyline(xs, temps, "stroke:#FF0000; stroke-width:1; fill:none")
78 svgs.Polyline(xs, targets, "stroke:#0000FF; stroke-width:1; fill:none")
79 }
80
81 // draw axes last so they're on top of everything else
82 svgs.Line(0, ImgHeight, ImgWidth, ImgHeight, "stroke:#000000; stroke-width:3")
83 svgs.Line(0, 0, 0, ImgHeight, "stroke:#000000; stroke-width:3")
84 svgs.Line(ImgWidth, 0, ImgWidth, ImgHeight, "stroke:#000000; stroke-width:3")
85
86 svgs.End()
87 }
113113 http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
114114 s.DumpJson(w, r)
115115 })
116 http.HandleFunc("/plot", func(w http.ResponseWriter, r *http.Request) {
117 s.GenerateChart2(w, r)
118 })
119116 http.HandleFunc("/timer", AddTimerHandler)
120117 http.HandleFunc("/timers", GetTimersHandler)
121118 http.HandleFunc("/delete_timer", DeleteTimerHandler)