google_otg

Author

Eric Silverberg (www.ericsilverberg.com)

Copyright

Copyright © 2009 Eric Silverberg

License

MIT (Go Beavers!)

Git

github.com/esilverberg/google_otg/tree/master

This plugin adds helpers to include Google’s pretty over time line graph in your rails application. You will recognize this line graph from Google Analytics.

You can also download images via integration with the Google chart API as well as download CSV

See a live example at www.picostatus.com/reports/1J6v2eQ

Requirements

You must be able to generate arrays of objects that respond to “count” and “created_at”. The X-axis is presumed to be dates. You can control time step of the x-axis.

Example Usage

In your controller:

@hits_last_week = Hits.find_by_sql(["
       SELECT DAYOFYEAR(TIMESTAMPADD(SECOND, ?, created_at)) as d, 
           DATE(TIMESTAMPADD(SECOND, ?, created_at)) as created_at, 
           count(*) as count
       FROM hits
       WHERE widget_id = ?

       AND created_at >= TIMESTAMPADD(SECOND, -1 * ?, DATE(?))
       AND created_at <= TIMESTAMPADD(SECOND, -1 * ?, DATE(?)) + INTERVAL 1 DAY

       GROUP BY d
       ORDER BY created_at  
   ", utc_offset_in_sec, utc_offset_in_sec, widget.id, utc_offset_in_sec, lower_bound, utc_offset_in_sec, upper_bound])

In your view:

<%= over_time_graph(@hits_last_week) %>

or

<%= over_time_graph(@hits_last_week, 
      :x_label_format => "%A, %B %d", 
      :max_x_label_count => 3,
      :time_zone => @time_zone,
      :range => @range,
      :src => "/google/OverTimeGraph.swf") %>

over_time_graph

Some of the options available:

:label

The label of quantity being measured

:range

The time step, in minutes

:x_label_format

The time format for the x label

:src

An optional local source to serve this widget (otherwise will serve from Google)

google_line_graph

Example:

google_line_graph(
    [@impressions, @conversions],
    :x_label_format => "%a, %b %d", 
    :time_zone => @time_zone, 
    :range => 1440,
    :title => @company_name,
    :legend => ['Impressions','Conversions'])

Some of the options available:

:title

The title of this graph

:legend

The graph legend

:title_color

The title color

:title_size

Title font size

:grid_lines

Grid lines on the graph

data_to_csv

Example:

data_to_csv([@impressions, @conversions], 
        :legend => ['Impressions','Conversions'],
        :x_label_format => "%m/%d/%Y", 
        :time_zone => ActiveSupport::TimeZone['Hawaii'],
        :range => {:lower_bound => 1.day.ago, 
                   :upper_bound => 1.day.fromnow})

Some of the options available:

:legend

Column headers

:time_zone

Time zone of data

:range

Lower & upper bound

:x_label_format

Format string for x-axis data

Copyright © 2009 esilverberg. See LICENSE for details.