Sha256: 9cb7224d0214db663493ee334eb48b0b8f09a225f9d8a1bc1fd7a03214a8605a

Contents?: true

Size: 528 Bytes

Versions: 10

Compression:

Stored size: 528 Bytes

Contents

# for both multiple series and
# making sure hash order is preserved in JavaScript

class Array
  def chart_json
    map do |v|
      if v.is_a?(Hash) && v[:data].is_a?(Hash)
        v = v.dup
        v[:data] = v[:data].to_a
      end
      v
    end.to_json
  end
end

class Hash
  def chart_json
    if (key = keys.first) && key.is_a?(Array) && key.size == 2
      group_by { |k, _v| k[0] }.map do |name, data|
        {name: name, data: data.map { |k, v| [k[1], v] }}
      end
    else
      to_a
    end.to_json
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
chartkick-5.1.2 lib/chartkick/core_ext.rb
chartkick-5.1.1 lib/chartkick/core_ext.rb
chartkick-5.1.0 lib/chartkick/core_ext.rb
chartkick-5.0.6 lib/chartkick/core_ext.rb
chartkick-5.0.5 lib/chartkick/core_ext.rb
chartkick-5.0.4 lib/chartkick/core_ext.rb
chartkick-5.0.3 lib/chartkick/core_ext.rb
chartkick-5.0.2 lib/chartkick/core_ext.rb
chartkick-5.0.1 lib/chartkick/core_ext.rb
chartkick-5.0.0 lib/chartkick/core_ext.rb