Skip to content

Commit

Permalink
support mutliple series organized as a hash of series
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanbrown committed Oct 11, 2014
1 parent e7898ab commit 2830e96
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
45 changes: 22 additions & 23 deletions lib/chartnado/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(context, data_block, &render_block)

delegate :controller, to: :context

def render(*args, **options)
def render(*args, ** options)
json_options = {}
chartkick_options = options.dup

Expand Down Expand Up @@ -40,7 +40,7 @@ def render(*args, **options)
}

if options[:wrapper_proc]
context.instance_exec(*args, renderer, **options, &options[:wrapper_proc])
context.instance_exec(*args, renderer, ** options, &options[:wrapper_proc])
else
renderer.call
end
Expand All @@ -49,32 +49,33 @@ def render(*args, **options)
def chart_json(series, show_total: false, reverse_sort: false, percentage: false)
series = Chartnado::Series::Wrap[series]
series *= 100.0 if percentage
if series.hash?
if (key = series.keys.first) and key.is_a?(Array) and key.size == 2
if series.has_separate_named_series?
series = series.to_a
if series.first.second.respond_to?(:map)
totals = Hash.new(0.0)
new_series = series.group_by{|k, v| k[0] }.sort_by { |k| k.to_s }
new_series = series.sort_by { |item| item.first.to_s }
new_series = new_series.reverse if reverse_sort

new_series = new_series.map do |name, data|
{
name: name,
data: data.map do |k, v|
totals[k[1]] += v if show_total
[k[1], v]
totals[k] += v if show_total
[k, v]
end
}
end

if show_total
[{name: 'Total',
data: totals.map {|k,v| [k, 0] },
tooltip: totals.map {|k,v| [k, v] }
data: totals.map { |k, v| [k, 0] },
tooltip: totals.map { |k, v| [k, v] }
}] + new_series
else
new_series
end
else
new_series = series.sort_by { |key| key.to_s }
new_series = series.sort_by { |item| item.first.to_s }
new_series = new_series.reverse if reverse_sort

if show_total
Expand All @@ -83,32 +84,32 @@ def chart_json(series, show_total: false, reverse_sort: false, percentage: false
new_series
end
end
elsif series.array? && series.first.is_a?(Array)
if series.first.second.respond_to?(:map)
elsif series.hash?
if (key = series.keys.first) and key.is_a?(Array) and key.size == 2
totals = Hash.new(0.0)
new_series = series.sort_by { |item| item.first.to_s }
new_series = series.group_by { |k, v| k[0] }.sort_by { |k| k.to_s }
new_series = new_series.reverse if reverse_sort

new_series = new_series.map do |name, data|
{
name: name,
data: data.map do |k, v|
totals[k] += v if show_total
[k, v]
totals[k[1]] += v if show_total
[k[1], v]
end
}
end

if show_total
[{name: 'Total',
data: totals.map {|k,v| [k, 0] },
tooltip: totals.map {|k,v| [k, v] }
data: totals.map { |k, v| [k, 0] },
tooltip: totals.map { |k, v| [k, v] }
}] + new_series
else
new_series
end
else
new_series = series.sort_by { |item| item.first.to_s }
new_series = series.sort_by { |key| key.to_s }
new_series = new_series.reverse if reverse_sort

if show_total
Expand All @@ -117,12 +118,10 @@ def chart_json(series, show_total: false, reverse_sort: false, percentage: false
new_series
end
end
elsif series.respond_to?(:map)
series
else
if series.respond_to?(:map)
series
else
[['Total', series]]
end
[['Total', series]]
end
end
end
12 changes: 12 additions & 0 deletions spec/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ def chart_json(*series, **options)
{name: :b, data: [[1, 20]]}]
end
end
describe "for multiple series organized as a hash" do
it "can generate chartkick compatible series" do
expect(chart_json({:a => {1 => 10}, :b => {1 => 20}})).
to eq [{name: :a, data: [[1, 10]]}, {name: :b, data: [[1,20]]}]
end
it "can add totals" do
expect(chart_json({:a => {1 => 10}, :b => {1 => 20}}, show_total: true)).
to eq [{name: 'Total', data: [[1, 0]], tooltip: [[1, 30.0]]},
{name: :a, data: [[1, 10]]},
{name: :b, data: [[1, 20]]}]
end
end
describe "for data that is just a scalar" do
it "shows the scalar as the total" do
expect(chart_json(10)).
Expand Down

0 comments on commit 2830e96

Please sign in to comment.