lib/chartkick/helper.rb in chartkick-1.3.2 vs lib/chartkick/helper.rb in chartkick-1.4.0
- old
+ new
@@ -1,11 +1,10 @@
require "json"
require "erb"
module Chartkick
module Helper
-
def line_chart(data_source, options = {})
chartkick_chart "LineChart", data_source, options
end
def pie_chart(data_source, options = {})
@@ -22,29 +21,33 @@
def area_chart(data_source, options = {})
chartkick_chart "AreaChart", data_source, options
end
+ def scatter_chart(data_source, options = {})
+ chartkick_chart "ScatterChart", data_source, options
+ end
+
def geo_chart(data_source, options = {})
chartkick_chart "GeoChart", data_source, options
end
def timeline(data_source, options = {})
chartkick_chart "Timeline", data_source, options
end
private
- def chartkick_chart(klass, data_source, options, &block)
+ def chartkick_chart(klass, data_source, options)
@chartkick_chart_id ||= 0
options = chartkick_deep_merge(Chartkick.options, options)
element_id = options.delete(:id) || "chart-#{@chartkick_chart_id += 1}"
height = options.delete(:height) || "300px"
# content_for: nil must override default
- content_for = options.has_key?(:content_for) ? options.delete(:content_for) : Chartkick.content_for
+ content_for = options.key?(:content_for) ? options.delete(:content_for) : Chartkick.content_for
- html = (options.delete(:html) || %[<div id="%{id}" style="height: %{height}; text-align: center; color: #999; line-height: %{height}; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;">Loading...</div>]) % {id: ERB::Util.html_escape(element_id), height: ERB::Util.html_escape(height)}
+ html = (options.delete(:html) || %(<div id="%{id}" style="height: %{height}; text-align: center; color: #999; line-height: %{height}; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;">Loading...</div>)) % {id: ERB::Util.html_escape(element_id), height: ERB::Util.html_escape(height)}
js = <<JS
<script type="text/javascript">
new Chartkick.#{klass}(#{element_id.to_json}, #{data_source.respond_to?(:chart_json) ? data_source.chart_json : data_source.to_json}, #{options.to_json});
</script>
@@ -59,14 +62,13 @@
end
# https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
def chartkick_deep_merge(hash_a, hash_b)
hash_a = hash_a.dup
- hash_b.each_pair do |k,v|
+ hash_b.each_pair do |k, v|
tv = hash_a[k]
hash_a[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
end
hash_a
end
-
end
end