Skip to content

Commit

Permalink
handle NaN values in series
Browse files Browse the repository at this point in the history
fixes #21
  • Loading branch information
guptarohit committed Sep 13, 2020
1 parent 67bb1a4 commit 2407d69
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
25 changes: 23 additions & 2 deletions asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,29 @@ func Plot(series []float64, options ...Option) string {
plot[rows-y0][config.Offset-1] = "┼" // first value

for x := 0; x < len(series)-1; x++ { // plot the line
y0 = int(round(series[x+0]*ratio) - float64(intmin2))
y1 = int(round(series[x+1]*ratio) - float64(intmin2))

d0 := series[x]
d1 := series[x+1]

if math.IsNaN(d0) && math.IsNaN(d1) {
continue
}

if math.IsNaN(d1) && !math.IsNaN(d0) {
y0 = int(round(d0*ratio) - float64(intmin2))
plot[rows-y0][x+config.Offset] = "╴"
continue
}

if math.IsNaN(d0) && !math.IsNaN(d1) {
y1 = int(round(d1*ratio) - float64(intmin2))
plot[rows-y1][x+config.Offset] = "╶"
continue
}

y0 = int(round(d0*ratio) - float64(intmin2))
y1 = int(round(d1*ratio) - float64(intmin2))

if y0 == y1 {
plot[rows-y0][x+config.Offset] = "─"
} else {
Expand Down
26 changes: 26 additions & 0 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package asciigraph

import (
"fmt"
"math"
"testing"
)

Expand Down Expand Up @@ -187,6 +188,31 @@ func TestPlot(t *testing.T) {
38.46 ┤ ││ │││
37.46 ┤ ╰╯ │││
36.45 ┤ ╰╯╰ `},
{
[]float64{1, 1, math.NaN(), 1, 1},
nil,
` 1.00 ┼─╴╶─ `},
{
[]float64{0, 0, 1, 1, math.NaN(), math.NaN(), 3, 3, 4},
nil,
` 4.00 ┼ ╭
3.00 ┤ ╶─╯
2.00 ┤
1.00 ┤ ╭─╴
0.00 ┼─╯ `},
{
[]float64{.1, .2, .3, math.NaN(), .5, .6, .7, math.NaN(), math.NaN(), .9, 1},
nil,
` 1.00 ┤ ╭
0.90 ┤ ╶╯
0.80 ┤
0.70 ┤ ╭╴
0.60 ┤ ╭╯
0.50 ┤ ╶╯
0.40 ┤
0.30 ┤ ╭╴
0.20 ┤╭╯
0.10 ┼╯ `},
}

for i := range cases {
Expand Down

0 comments on commit 2407d69

Please sign in to comment.