git.haldean.org sousvide / 61789fc
Delete old chart generator. Will Haldean Brown 8 years ago
3 changed file(s) with 60 addition(s) and 149 deletion(s). Raw diff Collapse all Expand all
00 Dependencies:
1 code.google.com/p/graphics-go/graphics
2 code.google.com/p/draw2d/draw2d
3 github.com/haldean/chart
41 github.com/ajstarks/svgo
11
22 import (
33 "github.com/ajstarks/svgo"
4 "github.com/haldean/chart"
5 "github.com/haldean/chart/svgg"
6 "image/color"
74 "math"
85 "net/http"
96 )
107
118 const (
12 imgWidth = 600
13 imgHeight = 400
14 useSvg = true
9 ImgWidth = 600
10 ImgHeight = 400
1511 )
1612
17 func (s *SousVide) GenerateChart(w http.ResponseWriter, req *http.Request) {
13 func (s *SousVide) GenerateChart2(w http.ResponseWriter, r *http.Request) {
1814 if len(s.History) == 0 {
1915 w.WriteHeader(http.StatusNoContent)
2016 return
2117 }
22 c := chart.ScatterChart{}
2318
24 c.Key.Hide = true
25 c.XRange.TicSetting.Hide = true
26 c.YRange.MinMode.Fixed = true
27 c.YRange.MinMode.Value = 0
28 c.YRange.TicSetting.Grid = 1
29 c.YRange.TicSetting.HideLabels = true
19 N := len(s.History)
3020
31 c.XRange.Fixed(0, float64(len(s.History))+1, float64(len(s.History)/10))
21 maxVal := float64(0)
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
3232
33 temps := make([]chart.EPoint, 0, len(s.History))
34 targets := make([]chart.EPoint, 0, len(s.History))
35 errs := make([]chart.EPoint, 0, len(s.History))
36 var ep chart.EPoint
37 for i, h := range s.History {
38 ep = chart.EPoint{
39 X: float64(i),
40 Y: h.Temp,
41 DeltaX: math.NaN(),
42 DeltaY: math.NaN(),
43 }
44 temps = append(temps, ep)
45
46 ep = chart.EPoint{
47 X: float64(i),
48 Y: h.Target,
49 DeltaX: math.NaN(),
50 DeltaY: math.NaN(),
51 }
52 targets = append(targets, ep)
53
54 ep = chart.EPoint{
55 X: float64(i),
56 Y: math.Abs(h.Temp - h.Target),
57 DeltaX: math.NaN(),
58 DeltaY: math.NaN(),
59 }
60 errs = append(errs, ep)
61 }
62
63 c.AddData("Temperature", temps, chart.PlotStyleLines, chart.Style{
64 LineColor: color.NRGBA{0xFF, 0x00, 0x00, 0xFF}, LineWidth: 2,
65 })
66 c.AddData("Target", targets, chart.PlotStyleLines, chart.Style{
67 LineColor: color.NRGBA{0x00, 0x00, 0xFF, 0xFF}, LineWidth: 2,
68 })
69 c.AddData("Error", errs, chart.PlotStyleLines, chart.Style{
70 LineColor: color.NRGBA{0x00, 0x00, 0x00, 0x66}, LineWidth: 2,
71 })
33 maxX := float64(N - 1)
34 pxPerUnitX := float64(ImgWidth) / maxX
7235
7336 w.Header().Set("Content-type", "image/svg+xml")
7437 svgs := svg.New(w)
75 svgs.Start(imgWidth, imgHeight)
38 svgs.Start(ImgWidth, ImgHeight)
7639 svgs.Title("Temperature history (\u00B0C)")
77 canvas := svgg.New(
78 svgs, imgWidth, imgHeight, "monospace", 12,
79 color.RGBA{0xFF, 0xFF, 0xFF, 0xFF})
8040
81 canvas.Begin()
82 c.Plot(canvas)
83 canvas.End()
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 for i := 0; i <= ImgHeight; i += int(5 * pxPerUnitY) {
55 y := ImgHeight - i
56 if even {
57 svgs.Line(0, y, ImgWidth, y, "stroke:#DDD; stroke-width:1")
58 } else {
59 svgs.Line(0, y, ImgWidth, y, "stroke:#EEE; stroke-width:1")
60 }
61 even = !even
62 }
63
64 // draw data
65 if N > 1 {
66 xs := make([]int, N)
67 temps := make([]int, N)
68 targets := make([]int, N)
69 for i, h := range s.History {
70 xs[i] = int(float64(i) * pxPerUnitX)
71 temps[i] = ImgHeight - int(h.Temp * pxPerUnitY)
72 targets[i] = ImgHeight - int(h.Target * pxPerUnitY)
73 }
74 svgs.Polyline(xs, temps, "stroke:#FF0000; stroke-width:1; fill:none")
75 svgs.Polyline(xs, targets, "stroke:#0000FF; stroke-width:1; fill:none")
76 }
77
78 // draw axes last so they're on top of everything else
79 svgs.Line(0, ImgHeight, ImgWidth, ImgHeight, "stroke:#000000; stroke-width:3")
80 svgs.Line(0, 0, 0, ImgHeight, "stroke:#000000; stroke-width:3")
81 svgs.Line(ImgWidth, 0, ImgWidth, ImgHeight, "stroke:#000000; stroke-width:3")
82
8483 svgs.End()
8584 }
+0
-85
chart2.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(0)
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 for i := 0; i <= ImgHeight; i += int(5 * pxPerUnitY) {
55 y := ImgHeight - i
56 if even {
57 svgs.Line(0, y, ImgWidth, y, "stroke:#DDD; stroke-width:1")
58 } else {
59 svgs.Line(0, y, ImgWidth, y, "stroke:#EEE; stroke-width:1")
60 }
61 even = !even
62 }
63
64 // draw data
65 if N > 1 {
66 xs := make([]int, N)
67 temps := make([]int, N)
68 targets := make([]int, N)
69 for i, h := range s.History {
70 xs[i] = int(float64(i) * pxPerUnitX)
71 temps[i] = ImgHeight - int(h.Temp * pxPerUnitY)
72 targets[i] = ImgHeight - int(h.Target * pxPerUnitY)
73 }
74 svgs.Polyline(xs, temps, "stroke:#FF0000; stroke-width:1; fill:none")
75 svgs.Polyline(xs, targets, "stroke:#0000FF; stroke-width:1; fill:none")
76 }
77
78 // draw axes last so they're on top of everything else
79 svgs.Line(0, ImgHeight, ImgWidth, ImgHeight, "stroke:#000000; stroke-width:3")
80 svgs.Line(0, 0, 0, ImgHeight, "stroke:#000000; stroke-width:3")
81 svgs.Line(ImgWidth, 0, ImgWidth, ImgHeight, "stroke:#000000; stroke-width:3")
82
83 svgs.End()
84 }