Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use googlecharts gem?

I am trying to use the googlecharts (http://googlecharts.rubyforge.org/) gem. Where do you put the code to generate a chart (like Gchart.line(:data => [0, 40, 10, 70, 20]) )? How do you display it?

Thanks

like image 667
B Seven Avatar asked Dec 09 '22 08:12

B Seven


1 Answers

Calling Gchart.line() simply returns a string that is the URL for the corresponding Google Chart image. E.g. Gchart.line(:data => [0, 40, 10, 70, 20]) returns "http://chart.apis.google.com/chart?chd=s:AjI9R&cht=lc&chs=300x200&chxr=0,0,70".

So, to display a chart on your page, you will need to create an image tag with a source of this generated URL. You can call Gchart directly from the view or set up the variable in your controller.

For example:

Controller @line_chart = Gchart.line(:data => [0, 40, 10, 70, 20])

View <%= image_tag(@line_chart) %>

This will generate an image tag like so: <img src="http://chart.apis.google.com/chart?chd=s:AjI9R&cht=lc&chs=300x200&chxr=0,0,70"/>.

like image 71
matkins Avatar answered Dec 29 '22 13:12

matkins