fix runchart X axis margin edge case

This commit is contained in:
sqshq 2019-06-02 20:14:32 -04:00
parent bc9d77a02d
commit a736560c71
2 changed files with 46 additions and 40 deletions

View File

@ -30,11 +30,36 @@ func (c *RunChart) newChartGrid() ChartGrid {
valueExtrema: getLocalExtrema(c.lines, timeRange), valueExtrema: getLocalExtrema(c.lines, timeRange),
linesCount: linesCount, linesCount: linesCount,
maxTimeWidth: c.Inner.Max.X, maxTimeWidth: c.Inner.Max.X,
minTimeWidth: c.getMaxValueLength(), minTimeWidth: 0,
} }
} }
func (c *RunChart) renderAxes(buffer *ui.Buffer) { 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 // draw origin cell
buffer.SetCell( buffer.SetCell(
ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(c.palette.BaseColor)), 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), ui.NewStyle(c.palette.BaseColor),
image.Pt(c.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), c.Inner.Max.Y-1)) 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 { 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) return time.After(r.min) && time.Before(r.max)
} }
// TODO add boundaries for values in range //func (c *RunChart) getMaxValueLength() int {
func (c *RunChart) getMaxValueLength() int { //
// maxValueLength := -1
maxValueLength := -1 //
// for _, line := range c.lines {
for _, line := range c.lines { // for _, point := range line.points {
for _, point := range line.points { // l := len(util.FormatValue(point.value, c.scale))
l := len(util.FormatValue(point.value, c.scale)) // if l > maxValueLength {
if l > maxValueLength { // maxValueLength = l
maxValueLength = l // }
} // }
} // }
} //
// if maxValueLength < 0 {
if maxValueLength < 0 { // return DefaultValueLength
return DefaultValueLength // }
} //
// return maxValueLength
return maxValueLength //}
}

View File

@ -1,7 +1,7 @@
package config package config
type Options struct { 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"` 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"` 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"` Version bool `short:"v" long:"version" description:"Print version"`