mattetti / googlecharts

Ruby Google Chart API
http://mattetti.github.com/googlecharts/
MIT License
699 stars 108 forks source link

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

Open mockdeep opened 10 years ago

mockdeep commented 10 years ago

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
kathgironpe commented 10 years ago

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

mockdeep commented 10 years ago

@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.

Chuongv commented 10 years ago

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

kathgironpe commented 10 years ago

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

mockdeep commented 10 years ago

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.

Seven0711 commented 9 years ago

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