Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BarChart error: invalid data range; cannot be zero #62

Open
panicake opened this issue Nov 29, 2017 · 6 comments
Open

BarChart error: invalid data range; cannot be zero #62

panicake opened this issue Nov 29, 2017 · 6 comments

Comments

@panicake
Copy link

When I get one value or a series of same value, and draw a BarChart, I will get an error 'invalid data range; cannot be zero'.
Finally, I found the reason.

yr = bc.getRanges()
	if yr.GetMax()-yr.GetMin() == 0 {
		return fmt.Errorf("invalid data range; cannot be zero")
	}

In the function getRanges,when one value or a series of same valuebc.Bars,min and max is same all the time. As a result, program will throw an error 'invalid data range; cannot be zero'.

min, max := math.MaxFloat64, -math.MaxFloat64
	for _, b := range bc.Bars {
		min = math.Min(b.Value, min)
		max = math.Max(b.Value, max)
	}
@panicake
Copy link
Author

I fixed this problem by rewriting code like this:

if yr.GetMax()-yr.GetMin() == 0 {
		// return fmt.Errorf("invalid data range; cannot be zero")
		v := yr.GetMax()
		if v > 0 {
			yr.SetMin(0)
		} else if  v < 0 {
			yr.SetMax(0)
		} else {
			yr.SetMax(0.5)
			yr.SetMin(0)
		}
	}

@panicake
Copy link
Author

in bar_chart.go

@FrontSide
Copy link

Hey @lovermaker. Thanks for this. I'd be interested in seeing that merged. Would you be able to open A PR?

@daill
Copy link

daill commented Apr 22, 2018

Faced this problem too and used the implementation of @lovermaker, works like a charm. I guess this should be merged soon.

@malaya7
Copy link

malaya7 commented Oct 16, 2020

This Also worked for me, can someone merge this ?

@Grehgous
Copy link

Grehgous commented May 3, 2021

For anyone unable to add a fork that has the fix proposed above, here is a workaround that can be added to your package that consumes go-chart.

func foo(graph chart.BarChart) {

	err := graph.Render(chart.PNG, buffer)

	// If there is a range error from go-chart, fix it and try it again.
	// Workaround for https://github.com/wcharczuk/go-chart/issues/62
	if err != nil && err.Error() == "invalid data range; cannot be zero" {
		fixRange(&graph)
		err = graph.Render(chart.PNG, buffer)
	}

	if err != nil {
		return nil, err
	}
}

// fixRange is a workaround for a go-chart issue where graph generation errors out if min and max yVals are equal.
// Workaround based off proposed fix from this ticket https://github.com/wcharczuk/go-chart/issues/62
func fixRange(graph *chart.BarChart) {

	var yRange chart.Range

	if graph.YAxis.Range != nil && !graph.YAxis.Range.IsZero() {
		yRange = graph.YAxis.Range
	} else {
		yRange = &chart.ContinuousRange{}
	}

	max := yRange.GetMax()

	// Min and max are different, no work needed
	if max-yRange.GetMin() != 0 {
		return
	}

	// Adjust range so min and max are different
	if max > 0 {
		yRange.SetMin(0)
	} else if max < 0 {
		yRange.SetMax(0)
	} else {
		yRange.SetMax(0.5)
		yRange.SetMin(0)
	}

	graph.YAxis.Range = yRange
}

Hopefully this ticket gets some attention. It has been a long standing issue and has a simple fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants