Module: Spyro::ActionViewExtension::D3Helper

Included in:
ApplicationHelper
Defined in:
lib/spyro/helpers/action_view_extension.rb

Instance Method Summary collapse

Instance Method Details

#d3bar(data, opts = {}) ⇒ Object



451
452
453
454
455
456
457
458
# File 'lib/spyro/helpers/action_view_extension.rb', line 451

def d3bar data, opts = {}
  default_values = opts[:range].to_a.map { |i| {label: i, value: 0} } if opts[:range]

  data.map do |key, values|
    values = (values.map { |key, value| {label: key, value: value} } + (default_values || [])).uniq { |a| a[:label] }.sort { |a, b| a[:label] <=> b[:label] }
    {key: key, values: values}
  end.to_json
end

#d3line(data, opts = {}) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/spyro/helpers/action_view_extension.rb', line 460

def d3line data, opts = {}
  opts = {:field => :created_at, :interval => 3.hours}.merge opts
  data.map do |key, values|
    values = values.order(opts[:field]).pluck(opts[:field])
    break {} if values.empty?
    start, res, count = values.first, [[(values.first - opts[:interval]).to_i * 1000, 0]], 0
    values.each do |vote|
      if vote.between?(start, start + opts[:interval])
        count += 1
      else
        res.push [start.to_i * 1000, count]
        start, count = start + opts[:interval], 0
      end
    end
    res.push [start.to_i * 1000, count], [(start + opts[:interval]).to_i * 1000, 0]
    {key: key, values: res}
  end.to_json
end

#d3pie(data) ⇒ Object



447
448
449
# File 'lib/spyro/helpers/action_view_extension.rb', line 447

def d3pie data
  data.map { |label, value| {label: label, value: value} }.to_json
end