From 79282317b13e09b84f961819db2153b30e6430a6 Mon Sep 17 00:00:00 2001 From: gutmet Date: Sun, 19 Apr 2020 10:20:00 +0200 Subject: [PATCH] fix ticks on x axis --- hdiet.go | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/hdiet.go b/hdiet.go index d14737f..d7261aa 100644 --- a/hdiet.go +++ b/hdiet.go @@ -6,6 +6,7 @@ import ( "fmt" "git.gutmet.org/go-chart.git" "git.gutmet.org/go-chart.git/drawing" + util "git.gutmet.org/go-chart.git/util" "git.gutmet.org/goutil.git" "math" "os" @@ -157,18 +158,18 @@ func show(args showFlags) error { fromTo := dateInterval{from, to} s1 := timeSeries("weight", chart.ColorCyan) - yRange1 := addPointsGetRange(&s1, points, fromTo, func(p point) float64 { return p.weight }) + xRange, yRange1 := addPointsGetRange(&s1, points, fromTo, func(p point) float64 { return p.weight }) s2 := timeSeries("trend", drawing.Color{R: 255, A: 255}) - yRange2 := addPointsGetRange(&s2, points, fromTo, func(p point) float64 { return p.trend }) + _, yRange2 := addPointsGetRange(&s2, points, xRange, func(p point) float64 { return p.trend }) s3 := timeSeries("diet", drawing.Color{R: 255, G: 211, A: 255}) yRange3 := yRange2 if dietpoints != nil { - yRange3 = addPointsGetRange(&s3, dietpoints, fromTo, func(p point) float64 { return p.weight }) + _, yRange3 = addPointsGetRange(&s3, dietpoints, xRange, func(p point) float64 { return p.weight }) } YRange := yRange{math.Min(math.Min(yRange1.min, yRange2.min), yRange3.min), math.Max(math.Max(yRange1.max, yRange2.max), yRange3.max)} YRange.min = math.Floor(YRange.min) - 1.0 YRange.max = math.Ceil(YRange.max) + 1.0 - graph := graph(s1, s2, s3, YRange) + graph := graph(xticks(xRange), s1, s2, s3, YRange) err = writeGraphToFile(graph) if err != nil { return err @@ -249,11 +250,35 @@ func main() { /*****************************/ -func addPointsGetRange(s *chart.TimeSeries, points []point, di dateInterval, project func(point) float64) yRange { +func xticks(d dateInterval) []chart.Tick { + ticks := []chart.Tick{} + start := d.min + end := d.max + stepDays := int(end.Sub(start).Hours() / 24 / 10) + if stepDays < 1 { + stepDays = 1 + } + for i := 0; start.AddDate(0, 0, i).Before(end.AddDate(0, 0, stepDays)); i += stepDays { + val := start.AddDate(0, 0, i) + label := val.Format(dateLayout) + ticks = append(ticks, chart.Tick{util.Time.ToFloat64(val), label}) + } + return ticks +} + +func addPointsGetRange(s *chart.TimeSeries, points []point, di dateInterval, project func(point) float64) (dateInterval, yRange) { + XRange := dateInterval{time.Now().AddDate(10000, 0, 0), time.Now().AddDate(-10000, 0, 0)} YRange := yRange{math.MaxFloat64, -math.MaxFloat64} for _, p := range points { if (p.date.After(di.min) || p.date.Equal(di.min)) && (p.date.Before(di.max) || p.date.Equal(di.max)) && !math.IsNaN(project(p)) { - s.XValues = append(s.XValues, p.date) + xVal := p.date + s.XValues = append(s.XValues, xVal) + if xVal.Before(XRange.min) { + XRange.min = xVal + } + if xVal.After(XRange.max) { + XRange.max = xVal + } yVal := project(p) s.YValues = append(s.YValues, yVal) if yVal < YRange.min { @@ -264,14 +289,14 @@ func addPointsGetRange(s *chart.TimeSeries, points []point, di dateInterval, pro } } } - return YRange + return XRange, YRange } func timeSeries(name string, color drawing.Color) chart.TimeSeries { return chart.TimeSeries{Name: name, Style: chart.Style{Show: true, StrokeColor: color}} } -func graph(s1 chart.TimeSeries, s2 chart.TimeSeries, s3 chart.TimeSeries, YRange yRange) chart.Chart { +func graph(xticks []chart.Tick, s1 chart.TimeSeries, s2 chart.TimeSeries, s3 chart.TimeSeries, YRange yRange) chart.Chart { graph := chart.Chart{ Width: width, Height: height, @@ -280,6 +305,7 @@ func graph(s1 chart.TimeSeries, s2 chart.TimeSeries, s3 chart.TimeSeries, YRange NameStyle: chart.StyleShow(), Style: chart.StyleShow(), ValueFormatter: chart.TimeDateValueFormatter, + Ticks: xticks, }, YAxis: chart.YAxis{ Name: "kg",