Skip to content

Commit

Permalink
Merge 6a62c3b into b8c6bd8
Browse files Browse the repository at this point in the history
  • Loading branch information
xordspar0 committed Jul 3, 2018
2 parents b8c6bd8 + 6a62c3b commit 2034244
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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)

Expand Down Expand Up @@ -74,7 +79,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))

Expand Down
4 changes: 4 additions & 0 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func TestPlot(t *testing.T) {
opts []Option
expected string
}{
{
[]float64{1, 1, 1, 1, 1},
nil,
` 1.00 ┼──── `},
{
[]float64{2, 1, 1, 2, -2, 5, 7, 11, 3, 7, 1},
nil,
Expand Down

0 comments on commit 2034244

Please sign in to comment.