Module: Graphene::TwoDGraphs

Included in:
OverX
Defined in:
lib/graphene/gruff.rb

Overview

Extends calculators with two-dimensional graphs, like line graphs.

Instance Method Summary collapse

Instance Method Details

#accumulator_bar_graph(path = nil, title = nil, &block) ⇒ Object Also known as: accumulator_bar_chart

Returns a Gruff::AccumulatorBar object with the stats set. This is different than most other graphs in that it may only have one row of data. For example, if you limit the browser to Firefox, it could show the relative gains in Firefox usage over time. You might start out with:

Optionally you may pass a file path and graph title. If you pass a file path, the graph will be written to file automatically. Otherwise, you would call “write(‘/path/to/graph.png’)” on the returned graph object.

If you pass a block, it will be called, giving you access to the Gruff::AccumulatorBar object before it is written to file (that is, if you also passed a file path). It will also give you access to a Proc for labeling the X axis.

Example:

logs = SomeLogParser.parse('/logs/*').select { |log| log.browser == 'Firefox' }
Graphene.subtotals(logs, :browser).over(:date).accumulator_bar_graph('/path/to/firefox-share.png', 'Firefox Share')


399
400
401
402
403
404
405
406
407
# File 'lib/graphene/gruff.rb', line 399

def accumulator_bar_graph(path=nil, title=nil, &block)
  Graphene.gruff do
    begin
      graph(Gruff::AccumulatorBar.new, path, title, &block)
    rescue Gruff::IncorrectNumberOfDatasetsException => e
      raise GrapheneException, "An Accumulator Bar Graph may only have one row of data - #{e.class.name}"
    end
  end
end

#dot_graph(path = nil, title = nil, &block) ⇒ Object Also known as: dot_chart

Returns a Gruff::Dot object with the stats set.

Optionally you may pass a file path and graph title. If you pass a file path, the graph will be written to file automatically. Otherwise, you would call “write(‘/path/to/graph.png’)” on the returned graph object.

If you pass a block, it will be called, giving you access to the Gruff::Dot object before it is written to file (that is, if you also passed a file path). It will also give you access to a Proc for labeling the X axis.

Example 1:

Graphene.percentages(logs, :browser).over(:date).dot_graph('/path/to/browser-share.png', 'Browser Share')

Example 2:

Graphene.subtotals(logs, :browser).over(:date).dot_graph('/path/to/browser-share.png') do |chart, labeler|
  chart.title = 'Browser Share'
  chart.font = '/path/to/font.ttf'
  chart.theme = pie.theme_37signals
end

Example 3:

Graphene.subtotals(logs, :browser).over(:date).dot_graph('/path/to/browser-share.png') do |chart, labeler|
  chart.title = 'Browser Share'

  # Both the 10 and the block are optional.
  #  - "10" means that only every 10'th label will be printed. Otherwise, each would be.
  #  - The block is passed each label (the return value of the "over attribute") and may return a formatted version.
  labeler.call(10) do |date|
    date.strftime('%m/%d/%Y')
  end
end

Example 4:

Graphene.percentages(logs, :platform, :browser).over(->(l) { l.date.strftime('%m/%Y') }).dot_graph('/path/to/os-browser-share.png', 'OS / Browser Share by Month')


375
376
377
378
379
# File 'lib/graphene/gruff.rb', line 375

def dot_graph(path=nil, title=nil, &block)
  Graphene.gruff do
    graph(Gruff::Dot.new, path, title, &block)
  end
end

#line_graph(path = nil, title = nil, &block) ⇒ Object Also known as: line_chart

Returns a Gruff::Line object with the stats set.

Optionally you may pass a file path and graph title. If you pass a file path, the graph will be written to file automatically. Otherwise, you would call “write(‘/path/to/graph.png’)” on the returned graph object.

If you pass a block, it will be called, giving you access to the Gruff::Line object before it is written to file (that is, if you also passed a file path). It will also give you access to a Proc for labeling the X axis.

Example 1:

Graphene.percentages(logs, :browser).over(:date).line_graph('/path/to/browser-share.png', 'Browser Share')

Example 2:

Graphene.subtotals(logs, :browser).over(:date).line_graph('/path/to/browser-share.png') do |chart, labeler|
  chart.title = 'Browser Share'
  chart.font = '/path/to/font.ttf'
  chart.theme = pie.theme_37signals
end

Example 3:

Graphene.subtotals(logs, :browser).over(:date).line_graph('/path/to/browser-share.png') do |chart, labeler|
  chart.title = 'Browser Share'

  # Both the 10 and the block are optional.
  #  - "10" means that only every 10'th label will be printed. Otherwise, each would be.
  #  - The block is passed each label (the return value of the "over attribute") and may return a formatted version.
  labeler.call(10) do |date|
    date.strftime('%m/%d/%Y')
  end
end

Example 4:

Graphene.percentages(logs, :platform, :browser).over(->(l) { l.date.strftime('%m/%Y') }).line_graph('/path/to/os-browser-share.png', 'OS / Browser Share by Month')


283
284
285
286
287
# File 'lib/graphene/gruff.rb', line 283

def line_graph(path=nil, title=nil, &block)
  Graphene.gruff do
    graph(Gruff::Line.new, path, title, &block)
  end
end

#net_graph(path = nil, title = nil, &block) ⇒ Object Also known as: net_chart

Returns a Gruff::Net object with the stats set.

Optionally you may pass a file path and graph title. If you pass a file path, the graph will be written to file automatically. Otherwise, you would call “write(‘/path/to/graph.png’)” on the returned graph object.

If you pass a block, it will be called, giving you access to the Gruff::Net object before it is written to file (that is, if you also passed a file path). It will also give you access to a Proc for labeling the X axis.

Example 1:

Graphene.percentages(logs, :browser).over(:date).net_graph('/path/to/browser-share.png', 'Browser Share')

Example 2:

Graphene.subtotals(logs, :browser).over(:date).net_graph('/path/to/browser-share.png') do |chart, labeler|
  chart.title = 'Browser Share'
  chart.font = '/path/to/font.ttf'
  chart.theme = pie.theme_37signals
end

Example 3:

Graphene.subtotals(logs, :browser).over(:date).net_graph('/path/to/browser-share.png') do |chart, labeler|
  chart.title = 'Browser Share'

  # Both the 10 and the block are optional.
  #  - "10" means that only every 10'th label will be printed. Otherwise, each would be.
  #  - The block is passed each label (the return value of the "over attribute") and may return a formatted version.
  labeler.call(10) do |date|
    date.strftime('%m/%d/%Y')
  end
end

Example 4:

Graphene.percentages(logs, :platform, :browser).over(->(l) { l.date.strftime('%m/%Y') }).net_graph('/path/to/os-browser-share.png', 'OS / Browser Share by Month')


329
330
331
332
333
# File 'lib/graphene/gruff.rb', line 329

def net_graph(path=nil, title=nil, &block)
  Graphene.gruff do
    graph(Gruff::Net.new, path, title, &block)
  end
end