I''d like to be able to add a few charts into my application using the gruff gem, but because I''m a noob, it''s taking me a long time to figure out how to do it. Grateful to anyone who can offer me a clue, or furnish a simple example. Thanks. -- Posted via http://www.ruby-forum.com/.
Marston A.
2006-May-09 14:34 UTC
[Rails] Re: Howto to use Gruff (charts) from within Rails?
I would also love some info on this. I spent hours recompiling graphing libraries and finally got gruff to work. I want to know what is the easiest way to implement the graphs from within my views? Anyone out there doing this easily? vitruviano 61 wrote:> I''d like to be able to add a few charts into my application using the > gruff gem, but because I''m a noob, it''s taking me a long time to figure > out how to do it. > > Grateful to anyone who can offer me a clue, or furnish a simple example. > > Thanks.-- Posted via http://www.ruby-forum.com/.
Alex MacCaw
2006-May-09 14:47 UTC
[Rails] Re: Howto to use Gruff (charts) from within Rails?
vitruviano 61 wrote:> I''d like to be able to add a few charts into my application using the > gruff gem, but because I''m a noob, it''s taking me a long time to figure > out how to do it. > > Grateful to anyone who can offer me a clue, or furnish a simple example. > > Thanks.Here''s some code to get you started :) In your view just make an image with an src pointing to the action. def graph(days=6) if params[:days] days = params[:days].to_i end data = [] data2 = [] days.downto(0) do |n| data << Stat.count([''created_at BETWEEN ? AND ?'', (n + 1).days.ago, n.days.ago]) data2 << Stat.count_by_sql([''SELECT COUNT(DISTINCT remote_address) FROM stats WHERE created_at BETWEEN ? AND ?'', (n + 1).days.ago, n.days.ago]) end total = Stat.count unique = Stat.count_by_sql([''SELECT COUNT(DISTINCT remote_address) FROM stats'']) g = Gruff::Line.new(480) g.title = "Stats (total: #{total}, unique: #{unique})" # g.theme_37signals g.data("Hits", data) g.data("Unique Hits", data2) g.labels = {0 => "#{days} days ago", days => Time.now.strftime(''%d.%m.%Y'')} send_data(g.to_blob, :disposition => ''inline'', :type => ''image/png'', :filename => "hits.png") end def browsers safari = Stat.count([''browser = ?'', ''Safari'']) firefox = Stat.count([''browser = ?'', ''Firefox'']) ie = Stat.count([''browser = ?'', ''IE'']) netscape = Stat.count([''browser = ?'', ''Netscape'']) g = Gruff::Pie.new(480) g.title = "Browsers" g.data("Safari", [safari]) g.data("Firefox", [firefox]) g.data("IE", [ie]) g.data("Netscape", [netscape]) send_data(g.to_blob, :disposition => ''inline'', :type => ''image/png'', :filename => "browsers.png") end -- Posted via http://www.ruby-forum.com/.
Marston A.
2006-May-09 19:45 UTC
[Rails] Re: Howto to use Gruff (charts) from within Rails?
Alex, Thanks! So I can call it from my view for example like this: <%= image_tag("/controller/graph", :class => "whatever") %> ? I''ll give that a shot.> def graph(days=6) > if params[:days] > days = params[:days].to_i > end > data = [] > data2 = [] > days.downto(0) do |n| > data << Stat.count([''created_at BETWEEN ? AND ?'', (n + 1).days.ago, > n.days.ago]) > data2 << Stat.count_by_sql([''SELECT COUNT(DISTINCT remote_address) FROM > stats WHERE created_at BETWEEN ? AND ?'', (n + 1).days.ago, n.days.ago]) > end > > total = Stat.count > unique = Stat.count_by_sql([''SELECT COUNT(DISTINCT remote_address) FROM > stats'']) > > g = Gruff::Line.new(480) > g.title = "Stats (total: #{total}, unique: #{unique})" > # g.theme_37signals > > g.data("Hits", data) > g.data("Unique Hits", data2) > > g.labels = {0 => "#{days} days ago", days => > Time.now.strftime(''%d.%m.%Y'')} > send_data(g.to_blob, > :disposition => ''inline'', > :type => ''image/png'', > :filename => "hits.png") > end-- Posted via http://www.ruby-forum.com/.
Marston A.
2006-May-09 22:41 UTC
[Rails] Re: Howto to use Gruff (charts) from within Rails?
Hrm, The graph shows when I have <%= image_tag("/graph/test") %> in my test.rhml view, except the rest of my view layout doesn''t display, just simply the graph image. And when I do view source its just a large long serialized blog of image data. In my controller my test method looks like this: def test g = Gruff::Line.new(750) g.title = "Scores for Bart" g.font = File.expand_path(''artwork/fonts/Vera.ttf'', RAILS_ROOT) g.labels = { 0 => ''Mon'', 2 => ''Wed'', 4 => ''Fri'', 6 => ''Sun'' } # Modify this to represent your actual data models g.data("Watermelon", [9, 10, 20, 44, 65, 89]) @graph = send_data(g.to_blob, :disposition => ''inline'', :type => ''image/png'', :filename => "bart_scores.png") end Am I missing something? Alex MacCaw wrote:> Here''s some code to get you started :) > In your view just make an image with an src pointing to the action. > > def graph(days=6) > if params[:days] > days = params[:days].to_i > end > data = [] > data2 = [] > days.downto(0) do |n| > data << Stat.count([''created_at BETWEEN ? AND ?'', (n + 1).days.ago, > n.days.ago]) > data2 << Stat.count_by_sql([''SELECT COUNT(DISTINCT remote_address) FROM > stats WHERE created_at BETWEEN ? AND ?'', (n + 1).days.ago, n.days.ago]) > end > > total = Stat.count > unique = Stat.count_by_sql([''SELECT COUNT(DISTINCT remote_address) FROM > stats'']) > > g = Gruff::Line.new(480) > g.title = "Stats (total: #{total}, unique: #{unique})" > # g.theme_37signals > > g.data("Hits", data) > g.data("Unique Hits", data2) > > g.labels = {0 => "#{days} days ago", days => > Time.now.strftime(''%d.%m.%Y'')} > send_data(g.to_blob, > :disposition => ''inline'', > :type => ''image/png'', > :filename => "hits.png") > end-- Posted via http://www.ruby-forum.com/.