Skip to content

Commit

Permalink
Merge pull request #27 from quackduck/master
Browse files Browse the repository at this point in the history
Add option to set custom precision
  • Loading branch information
guptarohit committed Mar 27, 2021
2 parents a48bbc4 + 7253ba5 commit d553cef
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
func Plot(series []float64, options ...Option) string {
var logMaximum float64
config := configure(config{
Offset: 3,
Offset: 3,
Precision: 2,
}, options)

if config.Width > 0 {
Expand Down Expand Up @@ -59,7 +60,7 @@ func Plot(series []float64, options ...Option) string {
plot[i] = line
}

precision := 2
precision := config.Precision
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)
Expand All @@ -69,9 +70,9 @@ func Plot(series []float64, options ...Option) string {
// negative log
if math.Mod(logMaximum, 1) != 0 {
// non-zero digits after decimal
precision += int(math.Abs(logMaximum))
precision += uint(math.Abs(logMaximum))
} else {
precision += int(math.Abs(logMaximum) - 1.0)
precision += uint(math.Abs(logMaximum) - 1.0)
}
} else if logMaximum > 2 {
precision = 0
Expand Down
9 changes: 9 additions & 0 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ func TestPlot(t *testing.T) {
0.30 ┤ ╭╴
0.20 ┤╭╯
0.10 ┼╯`},
{
[]float64{-0.000018527, -0.021, -.00123, .00000021312, -.0434321234, -.032413241234, .0000234234},
[]Option{Height(5), Width(45), Precision(5)},
` 0.000023 ┼─╮ ╭────────╮ ╭
-0.008467 ┤ ╰──╮ ╭──╯ ╰─╮ ╭─╯
-0.016958 ┤ ╰─────╯ ╰╮ ╭─╯
-0.025449 ┤ ╰─╮ ╭─╯
-0.033940 ┤ ╰╮ ╭────╯
-0.042430 ┼ ╰───╯`},
}

for i := range cases {
Expand Down
4 changes: 4 additions & 0 deletions cmd/asciigraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
height uint
width uint
offset uint = 3
precision uint = 2
caption string
enableRealTime bool
realTimeDataBuffer int
Expand All @@ -33,6 +34,7 @@ func main() {
flag.UintVar(&height, "h", height, "`height` in text rows, 0 for auto-scaling")
flag.UintVar(&width, "w", width, "`width` in columns, 0 for auto-scaling")
flag.UintVar(&offset, "o", offset, "`offset` in columns, for the label")
flag.UintVar(&precision, "p", precision, "`precision` of data point labels along the y-axis")
flag.StringVar(&caption, "c", caption, "`caption` for the graph")
flag.BoolVar(&enableRealTime, "r", enableRealTime, "enables `realtime` graph for data stream")
flag.IntVar(&realTimeDataBuffer, "b", realTimeDataBuffer, "data points `buffer` when realtime graph enabled, default equal to `width`")
Expand Down Expand Up @@ -70,6 +72,7 @@ func main() {
asciigraph.Height(int(height)),
asciigraph.Width(int(width)),
asciigraph.Offset(int(offset)),
asciigraph.Precision(precision),
asciigraph.Caption(caption))
asciigraph.Clear()
fmt.Println(plot)
Expand All @@ -90,6 +93,7 @@ func main() {
asciigraph.Height(int(height)),
asciigraph.Width(int(width)),
asciigraph.Offset(int(offset)),
asciigraph.Precision(precision),
asciigraph.Caption(caption))

fmt.Println(plot)
Expand Down
6 changes: 6 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type config struct {
Width, Height int
Offset int
Caption string
Precision uint
}

// An optionFunc applies an option.
Expand Down Expand Up @@ -59,6 +60,11 @@ func Offset(o int) Option {
return optionFunc(func(c *config) { c.Offset = o })
}

// Precision sets the graphs precision.
func Precision(p uint) Option {
return optionFunc(func(c *config) { c.Precision = p })
}

// Caption sets the graphs caption.
func Caption(caption string) Option {
return optionFunc(func(c *config) {
Expand Down

0 comments on commit d553cef

Please sign in to comment.