From a736560c718e22135e8bead979080117b6dcb551 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 2 Jun 2019 20:14:32 -0400 Subject: [PATCH] fix runchart X axis margin edge case --- component/runchart/grid.go | 84 ++++++++++++++++++++------------------ config/options.go | 2 +- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/component/runchart/grid.go b/component/runchart/grid.go index a019df5..f758806 100644 --- a/component/runchart/grid.go +++ b/component/runchart/grid.go @@ -30,11 +30,36 @@ func (c *RunChart) newChartGrid() ChartGrid { valueExtrema: getLocalExtrema(c.lines, timeRange), linesCount: linesCount, maxTimeWidth: c.Inner.Max.X, - minTimeWidth: c.getMaxValueLength(), + minTimeWidth: 0, } } func (c *RunChart) renderAxes(buffer *ui.Buffer) { + + // draw y axis labels + if c.grid.valueExtrema.max != c.grid.valueExtrema.min { + labelsCount := (c.Inner.Dy() - xAxisLabelsHeight - 1) / (yAxisLabelsIndent + yAxisLabelsHeight) + valuePerY := (c.grid.valueExtrema.max - c.grid.valueExtrema.min) / float64(c.Inner.Dy()-xAxisLabelsHeight-3) + for i := 0; i < int(labelsCount); i++ { + val := c.grid.valueExtrema.max - (valuePerY * float64(i) * (yAxisLabelsIndent + yAxisLabelsHeight)) + fmt := util.FormatValue(val, c.scale) + if len(fmt) > c.grid.minTimeWidth { + c.grid.minTimeWidth = len(fmt) + } + buffer.SetString( + fmt, + ui.NewStyle(c.palette.BaseColor), + image.Pt(c.Inner.Min.X, 1+c.Inner.Min.Y+i*(yAxisLabelsIndent+yAxisLabelsHeight))) + } + } else { + fmt := util.FormatValue(c.grid.valueExtrema.max, c.scale) + c.grid.minTimeWidth = len(fmt) + buffer.SetString( + fmt, + ui.NewStyle(c.palette.BaseColor), + image.Pt(c.Inner.Min.X, c.Inner.Min.Y+c.Inner.Dy()/2)) + } + // draw origin cell buffer.SetCell( ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(c.palette.BaseColor)), @@ -71,24 +96,6 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { ui.NewStyle(c.palette.BaseColor), image.Pt(c.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), c.Inner.Max.Y-1)) } - - // draw y axis labels - if c.grid.valueExtrema.max != c.grid.valueExtrema.min { - labelsCount := (c.Inner.Dy() - xAxisLabelsHeight - 1) / (yAxisLabelsIndent + yAxisLabelsHeight) - valuePerY := (c.grid.valueExtrema.max - c.grid.valueExtrema.min) / float64(c.Inner.Dy()-xAxisLabelsHeight-3) - for i := 0; i < int(labelsCount); i++ { - value := c.grid.valueExtrema.max - (valuePerY * float64(i) * (yAxisLabelsIndent + yAxisLabelsHeight)) - buffer.SetString( - util.FormatValue(value, c.scale), - ui.NewStyle(c.palette.BaseColor), - image.Pt(c.Inner.Min.X, 1+c.Inner.Min.Y+i*(yAxisLabelsIndent+yAxisLabelsHeight))) - } - } else { - buffer.SetString( - util.FormatValue(c.grid.valueExtrema.max, c.scale), - ui.NewStyle(c.palette.BaseColor), - image.Pt(c.Inner.Min.X, c.Inner.Min.Y+c.Inner.Dy()/2)) - } } func (c *RunChart) getTimeRange(linesCount int) TimeRange { @@ -139,23 +146,22 @@ func (r *TimeRange) isInRange(time time.Time) bool { return time.After(r.min) && time.Before(r.max) } -// TODO add boundaries for values in range -func (c *RunChart) getMaxValueLength() int { - - maxValueLength := -1 - - for _, line := range c.lines { - for _, point := range line.points { - l := len(util.FormatValue(point.value, c.scale)) - if l > maxValueLength { - maxValueLength = l - } - } - } - - if maxValueLength < 0 { - return DefaultValueLength - } - - return maxValueLength -} +//func (c *RunChart) getMaxValueLength() int { +// +// maxValueLength := -1 +// +// for _, line := range c.lines { +// for _, point := range line.points { +// l := len(util.FormatValue(point.value, c.scale)) +// if l > maxValueLength { +// maxValueLength = l +// } +// } +// } +// +// if maxValueLength < 0 { +// return DefaultValueLength +// } +// +// return maxValueLength +//} diff --git a/config/options.go b/config/options.go index 14cd61b..1f2213b 100644 --- a/config/options.go +++ b/config/options.go @@ -1,7 +1,7 @@ package config type Options struct { - ConfigFile *string `short:"c" long:"config" required:"true" description:"Path to YAML config file"` + ConfigFile *string `short:"c" long:"config" description:"Path to YAML config file"` LicenseKey *string `short:"l" long:"license" description:"License key. Visit www.sampler.dev for details"` Environment []string `short:"e" long:"env" description:"Specify name=value variable to use in script placeholder as $name. This flag takes precedence over the same name variables, specified in config yml"` Version bool `short:"v" long:"version" description:"Print version"`