Skip to content

Commit

Permalink
Merge pull request #12 from carlosms/min-max
Browse files Browse the repository at this point in the history
Add LowerBound and UpperBound options
  • Loading branch information
guptarohit authored Jun 23, 2023
2 parents 07878f5 + 3a1efcd commit a95f7a0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Options:
set fps to control how frequently graph to be rendered when realtime graph enabled (default 24)
-h height
height in text rows, 0 for auto-scaling
-lb lower bound
lower bound set the minimum value for the vertical axis (ignored if series contains lower values) (default +Inf)
-lc label color
y-axis label color of the plot
-o offset
Expand All @@ -140,6 +142,8 @@ Options:
enables realtime graph for data stream
-sc series color
series color of the plot
-ub upper bound
upper bound set the maximum value for the vertical axis (ignored if series contains larger values) (default -Inf)
-w width
width in columns, 0 for auto-scaling
asciigraph expects data points from stdin. Invalid values are logged to stderr.
Expand Down
6 changes: 6 additions & 0 deletions asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func PlotMany(data [][]float64, options ...Option) string {
maximum = max
}
}
if config.LowerBound != nil && *config.LowerBound < minimum {
minimum = *config.LowerBound
}
if config.UpperBound != nil && *config.UpperBound > maximum {
maximum = *config.UpperBound
}
interval := math.Abs(maximum - minimum)

if config.Height <= 0 {
Expand Down
38 changes: 38 additions & 0 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,44 @@ func TestPlot(t *testing.T) {
38.46 ┤ ││ │││
37.46 ┤ ╰╯ │││
36.45 ┤ ╰╯╰`},
{
[]float64{2, 1, 1, 2, -2, 5, 7, 11, 3, 7, 4, 5, 6, 9, 4, 0, 6, 1, 5, 3, 6, 2},
[]Option{LowerBound(-3), UpperBound(13)},
` 13.00 ┤
12.00 ┤
11.00 ┤ ╭╮
10.00 ┤ ││
9.00 ┤ ││ ╭╮
8.00 ┤ ││ ││
7.00 ┤ ╭╯│╭╮ ││
6.00 ┤ │ │││ ╭╯│ ╭╮ ╭╮
5.00 ┤ ╭╯ │││╭╯ │ ││╭╮││
4.00 ┤ │ ││╰╯ ╰╮││││││
3.00 ┤ │ ╰╯ ││││╰╯│
2.00 ┼╮ ╭╮│ ││││ ╰
1.00 ┤╰─╯││ ││╰╯
0.00 ┤ ││ ╰╯
-1.00 ┤ ││
-2.00 ┤ ╰╯
-3.00 ┤`},
{
[]float64{2, 1, 1, 2, -2, 5, 7, 11, 3, 7, 4, 5, 6, 9, 4, 0, 6, 1, 5, 3, 6, 2},
[]Option{LowerBound(0), UpperBound(3)},
` 11.00 ┤ ╭╮
10.00 ┤ ││
9.00 ┤ ││ ╭╮
8.00 ┤ ││ ││
7.00 ┤ ╭╯│╭╮ ││
6.00 ┤ │ │││ ╭╯│ ╭╮ ╭╮
5.00 ┤ ╭╯ │││╭╯ │ ││╭╮││
4.00 ┤ │ ││╰╯ ╰╮││││││
3.00 ┤ │ ╰╯ ││││╰╯│
2.00 ┼╮ ╭╮│ ││││ ╰
1.00 ┤╰─╯││ ││╰╯
0.00 ┤ ││ ╰╯
-1.00 ┤ ││
-2.00 ┤ ╰╯`},

{
[]float64{1, 1, math.NaN(), 1, 1},
nil,
Expand Down
9 changes: 9 additions & 0 deletions cmd/asciigraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"log"
"math"
"os"
"strconv"
"time"
Expand All @@ -26,6 +27,8 @@ var (
captionColor asciigraph.AnsiColor
axisColor asciigraph.AnsiColor
labelColor asciigraph.AnsiColor
lowerBound = math.Inf(1)
upperBound = math.Inf(-1)
)

func main() {
Expand Down Expand Up @@ -79,6 +82,8 @@ func main() {
return nil
}
})
flag.Float64Var(&lowerBound, "lb", lowerBound, "`lower bound` set the minimum value for the vertical axis (ignored if series contains lower values)")
flag.Float64Var(&upperBound, "ub", upperBound, "`upper bound` set the maximum value for the vertical axis (ignored if series contains larger values)")

flag.Parse()

Expand Down Expand Up @@ -119,6 +124,8 @@ func main() {
asciigraph.CaptionColor(captionColor),
asciigraph.AxisColor(axisColor),
asciigraph.LabelColor(labelColor),
asciigraph.LowerBound(lowerBound),
asciigraph.UpperBound(upperBound),
)
asciigraph.Clear()
fmt.Println(plot)
Expand All @@ -145,6 +152,8 @@ func main() {
asciigraph.CaptionColor(captionColor),
asciigraph.AxisColor(axisColor),
asciigraph.LabelColor(labelColor),
asciigraph.LowerBound(lowerBound),
asciigraph.UpperBound(upperBound),
)

fmt.Println(plot)
Expand Down
29 changes: 21 additions & 8 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ type Option interface {

// config holds various graph options
type config struct {
Width, Height int
Offset int
Caption string
Precision uint
CaptionColor AnsiColor
AxisColor AnsiColor
LabelColor AnsiColor
SeriesColors []AnsiColor
Width, Height int
LowerBound, UpperBound *float64
Offset int
Caption string
Precision uint
CaptionColor AnsiColor
AxisColor AnsiColor
LabelColor AnsiColor
SeriesColors []AnsiColor
}

// An optionFunc applies an option.
Expand Down Expand Up @@ -59,6 +60,18 @@ func Height(h int) Option {
})
}

// LowerBound sets the graph's minimum value for the vertical axis. It will be ignored
// if the series contains a lower value.
func LowerBound(min float64) Option {
return optionFunc(func(c *config) { c.LowerBound = &min })
}

// UpperBound sets the graph's maximum value for the vertical axis. It will be ignored
// if the series contains a bigger value.
func UpperBound(max float64) Option {
return optionFunc(func(c *config) { c.UpperBound = &max })
}

// Offset sets the graphs offset.
func Offset(o int) Option {
return optionFunc(func(c *config) { c.Offset = o })
Expand Down

0 comments on commit a95f7a0

Please sign in to comment.