Forum: Ruby on Rails Howto to use Gruff (charts) from within Rails?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E1e5b5e4f0faa4f0dd7d5a325938e48f?d=identicon&s=25 Vitruviano 61 (vitruviano61)
on 2005-11-17 20:36
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.
Df0a460118ef876e45b13b8c1a471454?d=identicon&s=25 Marston A. (marstoni)
on 2006-05-09 16:34
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.
Baf018e2cc4616e4776d323215c7136c?d=identicon&s=25 Alex MacCaw (Guest)
on 2006-05-09 16:47
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
Df0a460118ef876e45b13b8c1a471454?d=identicon&s=25 Marston A. (marstoni)
on 2006-05-09 21:44
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
Df0a460118ef876e45b13b8c1a471454?d=identicon&s=25 Marston A. (marstoni)
on 2006-05-10 00:41
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
This topic is locked and can not be replied to.