开发者

How to use googlecharts gem?

开发者 https://www.devze.com 2023-03-08 13:48 出处:网络
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 d开发者_运维技巧o

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 d开发者_运维技巧o you display it?

Thanks


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"/>.


The documentation on the link provided isn't very informative in regards to use cases, but I'm almost positive that you need to make that call in a view. Something like:

<%= Gchart.line(:data => [0, 40, 10, 70, 20]) %>

will output the html required to create the chart.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号