googlecharts icon indicating copy to clipboard operation
googlecharts copied to clipboard

Bar chart doesn't seem to cope gracefully with empty dataset

Open mockdeep opened this issue 9 years ago • 6 comments

When I have an empty data set, I get an error from google charts:

 > Gchart.bar(:data => [[],[],[]])
NoMethodError: undefined method `-' for nil:NilClass
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:594:in `block in encode_scaled_dataset'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:592:in `each'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:592:in `encode_scaled_dataset'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:624:in `simple_encoding'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:328:in `set_data'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:659:in `block in query_builder'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:656:in `map'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:656:in `query_builder'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:652:in `url_builder'
    from /home/fletch/.rvm/gems/ruby-2.1.2/gems/googlecharts-1.6.8/lib/gchart.rb:299:in `url'
    from (eval):12:in `bar'
    from (irb):3

mockdeep avatar Oct 13 '14 00:10 mockdeep

@mockdeep shouldn't you be checking if data set is empty before using Gchart?

kathgironpe avatar Oct 13 '14 06:10 kathgironpe

@katgironpe It doesn't seem like the role of the library to make that decision for users. If I pass an empty data set to the Google Charts API, it returns a blank graph, which is fine for my purposes. At the very least, a meaningful error message would be helpful.

mockdeep avatar Oct 13 '14 13:10 mockdeep

So your expectation is that it returns an empty graph? Or an error?

Chuongv avatar Oct 14 '14 06:10 Chuongv

@mockdeep it's a rare case but agree about proper error handling.

kathgironpe avatar Oct 14 '14 12:10 kathgironpe

I think the ideal situation for me would be for it just to return an empty graph. It's fairly common in the dev environment and in tests to have empty graphs, so it would be nice just to render it transparently instead of having to handle the error case.

mockdeep avatar Oct 14 '14 14:10 mockdeep

请问使用Google chart要引入哪些文件,去哪里下载?

Seven0711 avatar Mar 31 '15 08:03 Seven0711