From 231ff42904c42c330209fa755fca34446b62cfcb Mon Sep 17 00:00:00 2001 From: Alexey Kirpichnikov Date: Thu, 23 May 2019 15:59:57 +0500 Subject: [PATCH] Explicitly prevent empty ranges --- tick.go | 4 ++++ tick_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tick.go b/tick.go index d5d5a7b..be94dd3 100644 --- a/tick.go +++ b/tick.go @@ -133,6 +133,10 @@ func GeneratePrettyContinuousTicks(r Renderer, ra Range, isVertical bool, style rangeMin, rangeMax := ra.GetMin(), ra.GetMax() + if rangeMin >= rangeMax || ra.GetDomain() == 0 { + return []Tick{} + } + renderedLabelExample := vf(rangeMin) style.GetTextOptions().WriteToRenderer(r) renderedLabelSizePx := r.MeasureText(renderedLabelExample) diff --git a/tick_test.go b/tick_test.go index 446a098..b88ee0a 100644 --- a/tick_test.go +++ b/tick_test.go @@ -95,6 +95,37 @@ func TestGenerateContinuousPrettyTicks(t *testing.T) { {Label: "60.00", Value: 60}}) } +func TestGenerateContinuousPrettyTicksForEmptyRange(t *testing.T) { + assert := assert.New(t) + + f, err := GetDefaultFont() + assert.Nil(err) + + r, err := PNG(1024, 1024) + assert.Nil(err) + r.SetFont(f) + + ra := &ContinuousRange{ + Min: 1.0, + Max: 1.0, + Domain: 256, + } + + vf := FloatValueFormatter + + ticks := GeneratePrettyContinuousTicks(r, ra, false, Style{}, vf) + assert.Empty(ticks) + + ra = &ContinuousRange{ + Min: 1.0, + Max: 2.0, + Domain: 0, + } + + ticks = GeneratePrettyContinuousTicks(r, ra, false, Style{}, vf) + assert.Empty(ticks) +} + func TestGeneratePrettyTicksForVerySmallRange(t *testing.T) { assert := assert.New(t)