allow 'zero y-range delta' (#72)

This commit is contained in:
Edwin 2018-04-16 03:35:39 +08:00 committed by Will Charczuk
parent 7c3982fe3d
commit 2dc8482db3
4 changed files with 8 additions and 22 deletions

View File

@ -317,9 +317,6 @@ func (c Chart) checkRanges(xr, yr, yra Range) error {
if math.IsNaN(yDelta) { if math.IsNaN(yDelta) {
return errors.New("nan y-range delta") return errors.New("nan y-range delta")
} }
if yDelta == 0 {
return errors.New("zero y-range delta")
}
if c.hasSecondarySeries() { if c.hasSecondarySeries() {
yraDelta := yra.GetDelta() yraDelta := yra.GetDelta()
@ -329,9 +326,6 @@ func (c Chart) checkRanges(xr, yr, yra Range) error {
if math.IsNaN(yraDelta) { if math.IsNaN(yraDelta) {
return errors.New("nan secondary y-range delta") return errors.New("nan secondary y-range delta")
} }
if yraDelta == 0 {
return errors.New("zero secondary y-range delta")
}
} }
return nil return nil

View File

@ -443,22 +443,6 @@ func TestChartCheckRanges(t *testing.T) {
assert.Nil(c.checkRanges(xr, yr, yra)) assert.Nil(c.checkRanges(xr, yr, yra))
} }
func TestChartCheckRangesFailure(t *testing.T) {
assert := assert.New(t)
c := Chart{
Series: []Series{
ContinuousSeries{
XValues: []float64{1.0, 2.0},
YValues: []float64{3.14, 3.14},
},
},
}
xr, yr, yra := c.getRanges()
assert.NotNil(c.checkRanges(xr, yr, yra))
}
func TestChartCheckRangesWithRanges(t *testing.T) { func TestChartCheckRangesWithRanges(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View File

@ -93,12 +93,18 @@ func (m mathUtil) GetRoundToForDelta(delta float64) float64 {
// RoundUp rounds up to a given roundTo value. // RoundUp rounds up to a given roundTo value.
func (m mathUtil) RoundUp(value, roundTo float64) float64 { func (m mathUtil) RoundUp(value, roundTo float64) float64 {
if roundTo < 0.000000000000001 {
return value
}
d1 := math.Ceil(value / roundTo) d1 := math.Ceil(value / roundTo)
return d1 * roundTo return d1 * roundTo
} }
// RoundDown rounds down to a given roundTo value. // RoundDown rounds down to a given roundTo value.
func (m mathUtil) RoundDown(value, roundTo float64) float64 { func (m mathUtil) RoundDown(value, roundTo float64) float64 {
if roundTo < 0.000000000000001 {
return value
}
d1 := math.Floor(value / roundTo) d1 := math.Floor(value / roundTo)
return d1 * roundTo return d1 * roundTo
} }

View File

@ -78,6 +78,7 @@ func TestRoundUp(t *testing.T) {
assert.Equal(0.5, Math.RoundUp(0.49, 0.1)) assert.Equal(0.5, Math.RoundUp(0.49, 0.1))
assert.Equal(1.0, Math.RoundUp(0.51, 1.0)) assert.Equal(1.0, Math.RoundUp(0.51, 1.0))
assert.Equal(0.4999, Math.RoundUp(0.49988, 0.0001)) assert.Equal(0.4999, Math.RoundUp(0.49988, 0.0001))
assert.Equal(0.123, Math.RoundUp(0.123, 0))
} }
func TestRoundDown(t *testing.T) { func TestRoundDown(t *testing.T) {
@ -85,6 +86,7 @@ func TestRoundDown(t *testing.T) {
assert.Equal(0.5, Math.RoundDown(0.51, 0.1)) assert.Equal(0.5, Math.RoundDown(0.51, 0.1))
assert.Equal(1.0, Math.RoundDown(1.01, 1.0)) assert.Equal(1.0, Math.RoundDown(1.01, 1.0))
assert.Equal(0.5001, Math.RoundDown(0.50011, 0.0001)) assert.Equal(0.5001, Math.RoundDown(0.50011, 0.0001))
assert.Equal(0.123, Math.RoundDown(0.123, 0))
} }
func TestPercentDifference(t *testing.T) { func TestPercentDifference(t *testing.T) {