Class: TimeFlot

Inherits:
Flot
  • Object
show all
Defined in:
lib/time_flot.rb

Overview

TimeFlot

The TimeFlot class provides for a graph of values over time. See Flot for more details.

Usage:

TimeFlot.new('graph') do |f|
  f.bars
  f.grid :hoverable => true
  f.selection :mode => "xy"
  f.filter {|collection| collection.select {|j| j. < Date.parse("7/8/2007") }}
  f.series_for("Stress", @journals, :x => :entry_date, :y => :stress_rating)
  f.series_for("Hours of Sleep", @journals, :x => :entry_date, :y => :hours_of_sleep)
  f.series_for("Restful Night?", @journals, :x => :entry_date, :y => lambda {|record| record.restful_night ? 5 : 0 }, :options => {:points => {:show => true}, :bars => {:show => false}})
end

Constant Summary collapse

JS_TIME_MULTIPLIER =
1000
BAR_WIDTH =
1.day * JS_TIME_MULTIPLIER

Instance Method Summary collapse

Constructor Details

#initialize(time_axis = :xaxis, &block) ⇒ TimeFlot

Create a new TimeFlot object with a default time_axis of :xaxis

TimeFlot.new do |tf|
  tf.bars  # default width is equal to 1 day
  tf.series_for("Temperature", @temps, :x => :created_on, :y => :temperature)
end


35
36
37
38
39
# File 'lib/time_flot.rb', line 35

def initialize(time_axis = :xaxis, &block)
  @options ||= {}
  time_axis(time_axis)
  super(nil, {}, &block)
end

Instance Method Details

#bars(opts = {:show => true, :barWidth => BAR_WIDTH, :align => "center"}) ⇒ Object

Sets the default width to one day… different set of defaults from Flot#bar



43
44
45
# File 'lib/time_flot.rb', line 43

def bars(opts = {:show => true, :barWidth => BAR_WIDTH, :align => "center"})
  @options[:bars] = opts
end

#series(label, d, opts = {}) ⇒ Object



47
48
49
# File 'lib/time_flot.rb', line 47

def series(label, d, opts = {})
  super label, d.map {|data| is_time_axis?(:yaxis) ? [data[0], TimeFlot.js_time_from(data[1]), data[2], data[3]] : [TimeFlot.js_time_from(data[0]), data[1], data[2], data[3]]}, opts
end