Module: Saulabs::Reportable::ReportTagHelper

Defined in:
lib/saulabs/reportable/report_tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#flot_report_tag(data, options = {}, flot_options = {}) ⇒ String

Renders a sparkline with the given data using the jquery flot plugin.

Examples:

Rendering a sparkline tag for report data


<%= flot_report_tag(User.registrations_report) %>

Parameters:

  • data (Array<Array<DateTime, Float>>)

    an array of report data as returned by Saulabs::Reportable::Report#run

  • options (Hash) (defaults to: {})

    options for width, height, the dom id and the format

  • flot_options (Hash) (defaults to: {})

    options that are passed directly to Raphael as JSON

Options Hash (options):

  • :width (Fixnum) — default: 300

    the width of the generated graph

  • :height (Fixnum) — default: 34

    the height of the generated graph

  • :dom_id (Array<Symbol>) — default: "reportable_#{Time.now.to_i}"

    the dom id of the generated div

Returns:

  • (String)

    an div tag and the javascript code showing a sparkline for the passed data



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/saulabs/reportable/report_tag_helper.rb', line 139

def flot_report_tag(data, options = {}, flot_options = {})
  @__flot_report_tag_count ||= -1
  @__flot_report_tag_count += 1
  default_dom_id = "#{data.model_class_name.downcase}_#{data.report_name}#{@__flot_report_tag_count > 0 ? @__flot_report_tag_count : ''}"
  options.reverse_merge!(Config.flot_options.slice(:width, :height, :format))
  options.reverse_merge!(:dom_id => default_dom_id)
  flot_options.reverse_merge!(Config.flot_options.except(:width, :height, :format))
  %Q{<div id="#{options[:dom_id]}" style="width:#{options[:width]}px;height:#{options[:height]}px;"></div>
  <script type="text\/javascript" charset="utf-8">
  $(function() {
    var set = #{data.to_a.map{|d| d[1] }.to_json},
    data = [];
    for (var i = 0; i < set.length; i++) {
      data.push([i, set[i]]);
    }
    $.plot($('##{options[:dom_id]}'), [data], #{flot_options.to_json});
  });
  </script>}
end

#google_report_tag(data, options = {}) ⇒ String

Renders a sparkline with the given data using the google drawing api.

Examples:

Rendering a sparkline tag for report data


<%= google_report_tag(User.registrations_report, :width => 200, :height => 100, :color => '000') %>

Parameters:

  • data (Array<Array<DateTime, Float>>)

    an array of report data as returned by Saulabs::Reportable::Report#run

  • options (Hash) (defaults to: {})

    options for the sparkline

Options Hash (options):

  • :width (Fixnum) — default: 300

    the width of the generated image

  • :height (Fixnum) — default: 34

    the height of the generated image

  • :line_color (String) — default: '0077cc'

    the line color of the generated image

  • :fill_color (String) — default: 'e6f2fa'

    the fill color of the generated image

  • :labels (Array<Symbol>) — default: []

    the axes to render lables for (Array of :x, :y, :r, :t; this is x axis, y axis, right, top)

  • :alt (String) — default: ''

    the alt attribute for the generated image

  • :title (String) — default: ''

    the title attribute for the generated image

Returns:

  • (String)

    an image tag showing a sparkline for the passed data



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/saulabs/reportable/report_tag_helper.rb', line 38

def google_report_tag(data, options = {})
  options.reverse_merge!(Config.google_options)
  data = data.to_a.collect { |d| d[1] }
  labels = ''
  unless options[:labels].empty?
    chxr = {}
    options[:labels].each_with_index do |l, i|
      chxr[l] = "#{i}," + ([:x, :t].include?(l) ? "0,#{data.length}" : "#{[data.min, 0].min},#{data.max}")
    end
    labels = "&chxt=#{options[:labels].map(&:to_s).join(',')}&chxr=#{options[:labels].collect{|l| chxr[l]}.join('|')}"
  end
  title = ''
  unless options[:title].blank?
    title = "&chtt=#{options[:title]}"
  end
  image_tag(
    "http://chart.apis.google.com/chart?cht=ls&chs=#{options[:width]}x#{options[:height]}&chd=t:#{data.join(',')}&chco=#{options[:line_color]}&chm=B,#{options[:fill_color]},0,0,0&chls=1,0,0&chds=#{data.min},#{data.max}#{labels}#{title}",
    :alt   => options[:alt],
    :title => options[:title]
  )
end

#raphael_report_tag(data, options = {}, raphael_options = {}) ⇒ String

Renders a sparkline with the given data using Raphael.

Examples:

Rendering a sparkline tag for report data


<%= raphael_report_tag(User.registrations_report, { :width => 200, :height => 100, :format => 'div(100).to_i' }, { :vertical_label_unit => 'registrations' }) %>

Parameters:

  • data (Array<Array<DateTime, Float>>)

    an array of report data as returned by Saulabs::Reportable::Report#run

  • options (Hash) (defaults to: {})

    options for width, height, the dom id and the format

  • raphael_options (Hash) (defaults to: {})

    options that are passed directly to Raphael as JSON

Options Hash (options):

  • :width (Fixnum) — default: 300

    the width of the generated graph

  • :height (Fixnum) — default: 34

    the height of the generated graph

  • :dom_id (Array<Symbol>) — default: "reportable_#{Time.now.to_i}"

    the dom id of the generated div

Returns:

  • (String)

    an div tag and the javascript code showing a sparkline for the passed data



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/saulabs/reportable/report_tag_helper.rb', line 84

def raphael_report_tag(data, options = {}, raphael_options = {})
  @__raphael_report_tag_count ||= -1
  @__raphael_report_tag_count += 1
  default_dom_id = "#{data.model_class_name.downcase}_#{data.report_name}#{@__raphael_report_tag_count > 0 ? @__raphael_report_tag_count : ''}"
  options.reverse_merge!(Config.raphael_options.slice(:width, :height, :format))
  options.reverse_merge!(:dom_id => default_dom_id)
  raphael_options.reverse_merge!(Config.raphael_options.except(:width, :height, :format))
  %Q{<div id="#{options[:dom_id]}" style="width:#{options[:width]}px;height:#{options[:height]}px;"></div>
  <script type="text\/javascript" charset="utf-8">
    var graph = Raphael('#{options[:dom_id]}');
    graph.g.linechart(
      -10, 4, #{options[:width]}, #{options[:height]},
      #{(0..data.to_a.size).to_a.to_json},
      #{data.to_a.map { |d| d[1].send(:eval, options[:format]) }.to_json},
      #{raphael_options.to_json}
    ).hover(function() {
      this.disc = graph.g.disc(this.x, this.y, 3).attr({fill: "#{options[:hover_fill_color]}", stroke: '#{options[:hover_line_color]}' }).insertBefore(this);
      this.flag = graph.g.flag(this.x, this.y, this.value || "0", 0).insertBefore(this);
      if (this.x + this.flag.getBBox().width > this.paper.width) {
        this.flag.rotate(-180);
        this.flag.translate(-this.flag.getBBox().width, 0);
        this.flag.items[1].rotate(180);
        this.flag.items[1].translate(-5, 0);
      }
    }, function() {
      this.disc.remove();
      this.flag.remove();
    });
  </script>}
end