diff --git a/Gopkg.lock b/Gopkg.lock index 869a7ac9..d6f850df 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -58,12 +58,11 @@ revision = "9fd32a8b3d3d3f9d43c341bfe098430e07609480" [[projects]] - digest = "1:a3b08eb3ec49caec29db596d8f2aae79614413a8d2068d5082b92fa23b5b6d80" + digest = "1:8abef4b9ca2f94e008688685bf8fb7bb27e5c82b353a82ef9276a27b4a6d2020" name = "github.com/carlosms/asciigraph" packages = ["."] pruneopts = "NUT" - revision = "b8c6bd8cace5d89062a57acb2012b0be08fb9525" - version = "v0.4.1" + revision = "22e3e1b9e3cd5ab4f79619cd65987f426d2d3dc4" [[projects]] branch = "master" diff --git a/Gopkg.toml b/Gopkg.toml index 412e63c3..c7ba9ed6 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -41,6 +41,6 @@ branch = "master" name = "github.com/spkg/bom" -# [[constraint]] -# name = "github.com/docker/docker" -# version = "v17" +[[constraint]] + name = "github.com/carlosms/asciigraph" + revision = "22e3e1b9e3cd5ab4f79619cd65987f426d2d3dc4" diff --git a/vendor/github.com/carlosms/asciigraph/asciigraph.go b/vendor/github.com/carlosms/asciigraph/asciigraph.go index 63defb69..3dde5d36 100644 --- a/vendor/github.com/carlosms/asciigraph/asciigraph.go +++ b/vendor/github.com/carlosms/asciigraph/asciigraph.go @@ -9,6 +9,7 @@ import ( // Plot returns ascii graph for a series. func Plot(series []float64, options ...Option) string { + var logMaximum float64 config := configure(config{ Offset: 3, }, options) @@ -18,6 +19,13 @@ func Plot(series []float64, options ...Option) string { } minimum, maximum := minMaxFloat64Slice(series) + if config.Min != nil && *config.Min < minimum { + minimum = *config.Min + } + if config.Max != nil && *config.Max > maximum { + maximum = *config.Max + } + interval := math.Abs(maximum - minimum) if config.Height <= 0 { @@ -32,7 +40,12 @@ func Plot(series []float64, options ...Option) string { config.Offset = 3 } - ratio := float64(config.Height) / interval + var ratio float64 + if interval != 0 { + ratio = float64(config.Height) / interval + } else { + ratio = 1 + } min2 := round(minimum * ratio) max2 := round(maximum * ratio) @@ -54,7 +67,10 @@ func Plot(series []float64, options ...Option) string { } precision := 2 - logMaximum := math.Log10(math.Max(math.Abs(maximum), math.Abs(minimum))) //to find number of zeros after decimal + logMaximum = math.Log10(math.Max(math.Abs(maximum), math.Abs(minimum))) //to find number of zeros after decimal + if minimum == float64(0) && maximum == float64(0) { + logMaximum = float64(-1) + } if logMaximum < 0 { // negative log @@ -74,7 +90,14 @@ func Plot(series []float64, options ...Option) string { // axis and labels for y := intmin2; y < intmax2+1; y++ { - label := fmt.Sprintf("%*.*f", maxWidth+1, precision, maximum-(float64(y-intmin2)*interval/float64(rows))) + var magnitude float64 + if rows > 0 { + magnitude = maximum - (float64(y-intmin2) * interval / float64(rows)) + } else { + magnitude = float64(y) + } + + label := fmt.Sprintf("%*.*f", maxWidth+1, precision, magnitude) w := y - intmin2 h := int(math.Max(float64(config.Offset)-float64(len(label)), 0)) @@ -99,12 +122,9 @@ func Plot(series []float64, options ...Option) string { } else { if y0 > y1 { plot[rows-y1][x+config.Offset] = "╰" - } else { - plot[rows-y1][x+config.Offset] = "╭" - } - if y0 > y1 { plot[rows-y0][x+config.Offset] = "╮" } else { + plot[rows-y1][x+config.Offset] = "╭" plot[rows-y0][x+config.Offset] = "╯" } diff --git a/vendor/github.com/carlosms/asciigraph/options.go b/vendor/github.com/carlosms/asciigraph/options.go index 7ecf3794..04a42e4a 100644 --- a/vendor/github.com/carlosms/asciigraph/options.go +++ b/vendor/github.com/carlosms/asciigraph/options.go @@ -12,6 +12,7 @@ type Option interface { // config holds various graph options type config struct { Width, Height int + Min, Max *float64 Offset int Caption string } @@ -54,6 +55,18 @@ func Height(h int) Option { }) } +// Min sets the graph's minimum value for the vertical axis. It will be ignored +// if the series contains a lower value. +func Min(min float64) Option { + return optionFunc(func(c *config) { c.Min = &min }) +} + +// Max sets the graph's maximum value for the vertical axis. It will be ignored +// if the series contains a bigger value. +func Max(max float64) Option { + return optionFunc(func(c *config) { c.Max = &max }) +} + // Offset sets the graphs offset. func Offset(o int) Option { return optionFunc(func(c *config) { c.Offset = o })