Sha256: 60df4e65e5574077f02dd0a8ec29649795b12fc1c6c44c3055ac03060399263a

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'json'

module MetricsGraphicsRails
  module ViewHelpers
    # TODO: generalize for extra options
    def metrics_graphic_for(data, options = {})
      json_data   = data.to_json
      target      = options.fetch(:target)
      x_accessor  = options.fetch(:x_accessor)  { :date }
      y_accessor  = options.fetch(:y_accessor)  { :value }
      title       = options.fetch(:title)
      description = options.fetch(:description) { '' }
      width       = options.fetch(:width)       { 600 }
      height      = options.fetch(:height)      { 250 }
      time_format = options.fetch(:time_format) { '%Y-%m-%d' }
      is_multiple = data.first.is_a?(Array)
      extra_options = options[:extra_options] || {}
      extra_options_to_options = extra_options.map{ |k,v| "#{k}: #{v}," }.join('')

      javascript_tag <<-SCRIPT
        var data = #{json_data};

        #{convert_data_js(data, x_accessor, time_format, is_multiple)}

        MG.data_graphic({
          title: "#{title}",
          description: "#{description}",
          data: data,
          width: #{width},
          height: #{height},
          target: '#{target}',
          #{extra_options_to_options}
          x_accessor: '#{x_accessor}',
          y_accessor: '#{y_accessor}'
        });
      SCRIPT
    end

    private

    def convert_data_js(data, x_accessor, time_format, is_multiple)
      if is_multiple
        <<-CONVERT
          for (var i = 0; i < data.length; i++) {
            data[i] = MG.convert.date(data[i], '#{x_accessor}', '#{time_format}');
          }
        CONVERT
      else
        "MG.convert.date(data, '#{x_accessor}', '#{time_format}');"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metrics-graphics-rails-2.6.0 lib/metrics-graphics-rails/view_helpers.rb
metrics-graphics-rails-2.4.0.3 lib/metrics-graphics-rails/view_helpers.rb