Skip to content

Commit

Permalink
Fix wrong values on y-axis
Browse files Browse the repository at this point in the history
- also fixes the issue of incorrect position of box-character & next character after axis in many cases.
  • Loading branch information
guptarohit committed Jun 29, 2018
1 parent 4463b43 commit 458d747
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
6 changes: 3 additions & 3 deletions asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func Plot(series []float64, options ...Option) string {
}

ratio := float64(config.Height) / interval
min2 := math.Floor(minimum * ratio)
max2 := math.Ceil(maximum * ratio)
min2 := round(minimum * ratio)
max2 := round(maximum * ratio)

intmin2 := int(min2)
intmax2 := int(max2)
Expand Down Expand Up @@ -86,7 +86,7 @@ func Plot(series []float64, options ...Option) string {
}
}

y0 := int(series[0]*ratio - min2)
y0 := int(round(series[0]*ratio) - min2)
var y1 int

plot[rows-y0][config.Offset-1] = "┼" // first value
Expand Down
84 changes: 39 additions & 45 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,29 @@ func TestPlot(t *testing.T) {
{
[]float64{.2, .1, .2, 2, -.9, .7, .91, .3, .7, .4, .5},
[]Option{Caption("Plot using asciigraph.")},
` 2.00 ┤
1.03 ┼ ╭╮ ╭╮
0.07 ┼──╯│╭╯╰───
` 2.00 ┤ ╭╮ ╭╮
0.55 ┼──╯│╭╯╰───
-0.90 ┤ ╰╯
Plot using asciigraph.`},
{
[]float64{2, 1, 1, 2, -2, 5, 7, 11, 3, 7, 1},
[]Option{Height(4), Offset(3)},
` 11.00 ┤
8.40 ┼ ╭╮
5.80 ┤ ╭─╯│╭╮
3.20 ┤╮ ╭╮│ ╰╯│
0.60 ┼╰─╯││ ╰
` 11.00 ┤ ╭╮
7.75 ┼ ╭─╯│╭╮
4.50 ┼╮ ╭╮│ ╰╯│
1.25 ┤╰─╯││ ╰
-2.00 ┤ ╰╯ `},
{
[]float64{.453, .141, .951, .251, .223, .581, .771, .191, .393, .617, .478},
nil,
` 0.95 ┤
0.86 ┤ ╭╮
0.77 ┤ ││ ╭╮
0.68 ┤ ││ ││
0.59 ┤ ││ ╭╯│ ╭╮
0.50 ┤ ││ │ │ │╰
0.41 ┼╮││ │ │╭╯
0.32 ┤│││ │ ││
0.23 ┤││╰─╯ ╰╯
` 0.95 ┤ ╭╮
0.85 ┤ ││ ╭╮
0.75 ┤ ││ ││
0.65 ┤ ││ ╭╯│ ╭╮
0.55 ┤ ││ │ │ │╰
0.44 ┼╮││ │ │╭╯
0.34 ┤│││ │ ││
0.24 ┤││╰─╯ ╰╯
0.14 ┤╰╯ `},

{
Expand All @@ -92,27 +89,25 @@ func TestPlot(t *testing.T) {
{
[]float64{192, 431, 112, 449, -122, 375, 782, 123, 911, 1711, 172},
[]Option{Height(10)},
` 1711 ┤
1544 ┼ ╭╮
1378 ┤ ││
1211 ┤ ││
1044 ┤ ││
878 ┤ ╭╯│
711 ┤ ╭╮│ │
545 ┤ │││ │
378 ┤╭╮╭╮╭╯││ │
211 ┼╯╰╯││ ╰╯ ╰
45 ┤ ││
` 1711 ┤ ╭╮
1528 ┼ ││
1344 ┤ ││
1161 ┤ ││
978 ┤ ╭╯│
794 ┤ ╭╮│ │
611 ┤ │││ │
428 ┤╭╮╭╮╭╯││ │
245 ┼╯╰╯││ ╰╯ ╰
61 ┤ ││
-122 ┤ ╰╯ `},
{
[]float64{0.3189989805, 0.149949026, 0.30142492354, 0.195129182935, 0.3142492354, 0.1674974513, 0.3142492354, 0.1474974513, 0.3047974513},
[]Option{Width(30), Height(5), Caption("Plot with custom height & width.")},
` 0.32 ┤
0.29 ┼╮ ╭─╮ ╭╮ ╭
0.27 ┤╰╮ ╭─╮ ╭╯ │ ╭╯│ │
0.24 ┤ │ ╭╯ ╰╮ ╭╯ ╰╮ ╭╯ ╰╮ ╭╯
0.22 ┤ ╰╮ ╭╯ ╰╮│ ╰╮╭╯ ╰╮ ╭╯
0.19 ┤ ╰╮│ ╰╯ ╰╯ │╭╯
` 0.32 ┼╮ ╭─╮ ╭╮ ╭
0.29 ┤╰╮ ╭─╮ ╭╯ │ ╭╯│ │
0.26 ┤ │ ╭╯ ╰╮ ╭╯ ╰╮ ╭╯ ╰╮ ╭╯
0.23 ┤ ╰╮ ╭╯ ╰╮│ ╰╮╭╯ ╰╮ ╭╯
0.20 ┤ ╰╮│ ╰╯ ╰╯ │╭╯
0.16 ┤ ╰╯ ╰╯
Plot with custom height & width.`},
{
Expand All @@ -123,17 +118,16 @@ func TestPlot(t *testing.T) {
},
[]Option{Offset(10), Height(10), Caption("I'm a doctor, not an engineer.")},
` 10.00 ┤ ╭╮
8.82 ┤ ╭╮ ││
7.64 ┤ ││ ╭╮ ││
6.45 ┼ ││ ││ ││
5.27 ┤ ││ ││ ││
4.09 ┤ ││ ││ ││
2.91 ┤ ││ ╭╮ ││ ╭╮ ││ ╭╮
1.73 ┤ ╭╮ ││ ╭╯╰╮ ╭╮ ││ ╭╯╰╮ ╭╮ ││ ╭╯╰╮
0.55 ┼───╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───
-0.64 ┤ ││ ││ ││
-1.82 ┤ ╰╯ ╰╯ ╰╯
-3.00 ┤
8.70 ┤ ╭╮ ││
7.40 ┼ ││ ╭╮ ││
6.10 ┤ ││ ││ ││
4.80 ┤ ││ ││ ││
3.50 ┤ ││ ││ ││
2.20 ┤ ││ ╭╮ ││ ╭╮ ││ ╭╮
0.90 ┤ ╭╮ ││ ╭╯╰╮ ╭╮ ││ ╭╯╰╮ ╭╮ ││ ╭╯╰╮
-0.40 ┼───╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───
-1.70 ┤ ││ ││ ││
-3.00 ┤ ╰╯ ╰╯ ╰╯
I'm a doctor, not an engineer.`},
}

Expand Down

0 comments on commit 458d747

Please sign in to comment.