¶ ↑
active_graphgithub.com/kazjote/active_graph/tree/master
¶ ↑
DESCRIPTION:Gem which makes filling data for Gruff graphs based on ActiveRecord objects much easier.
¶ ↑
FEATURES/PROBLEMS:Good points:
-
Ease of use
-
Flexible
-
Created in model but enable to customize presentation in controller
Bad points:
-
Slow - requires many database calls (it is intended to create cacheable graphs)
-
Do not allow to customize options after model is loaded (TODO)
¶ ↑
SYNOPSIS:The simplies case (mosts are defaults - you probably won’t use it):
class User < ActiveRecord::Base active_graph :posts, :series => {:submited_posts => {:value => :count_have_posts_between, :caption => "posts"}} def self.count_have_posts_between(min, max) count(:all, :conditions => "posts_count > #{min} AND posts_count <= #{max}") end end
Now, featured one. It will create a line graph, which
* starts at User.start_time, * end one week later * shows data with 1 day step * defines x_axis with formated labels * presents 2 data series class User < ActiveRecord::Base active_graph :new_posts, :type => :line, :start => Proc.new {@@start_time}, :end => Proc.new { @@start_time + 1.week }, :step => 1.day, :x_axis => {:interval => 3, :method => Proc.new {|time| time.strftime("%d-%m") }}, :width => 100, :series => {:submited_posts => {:value => :count_have_posts_between, :caption => "posts"}, :submited_posts2 => {:value => :count_have_posts2_between, :caption => "posts2"}} def self.count_have_posts_between(min, max); end def self.count_have_posts2_between(min, max); end end
Statistic on user age?
active_graph :age, :type => :bar, :start => 0, :end => 100, :step => 10, :x_axis => {:interval => 1, :method => Proc.new {|age| "#{age}-#{age + 10}" }}, :series => { :age => {:value => :count_aged_between, :caption => "Age" } }
Now, create an action in your controller:
class UsersController < ApplicationController def new_posts_graph graph = User.new_posts_graph send_data graph.to_blob, :type => "image/png", :disposition => "inline", :filename => "new_users_graph.png" end end
graph is a normal Gruff object, so you can customize presentation before rendering.
¶ ↑
REQUIREMENTS:-
gem ‘activesupport’ >= 2.0.2
-
gem ‘metaid’ >= 1.0
-
gem ‘gruff’ >= 0.3.1
It can run without Rails (testcase does not use rails at all).
¶ ↑
INSTALL:-
sudo gem install active_graph
In your envrionment.rb:
config.gem "metaid" config.gem "gruff" config.gem "active_graph"
If you have older rails you can load it with initializer or directly in your model:
require 'gruff' require 'active_graph' class User < ActiveRecord::Base extend ActiveGraph::ActiveRecordExtension ... end
¶ ↑
LICENSE:(The MIT License)
Copyright © 2008 FIXME full name
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.