Skip to content

Commit

Permalink
Enable fill_by
Browse files Browse the repository at this point in the history
  • Loading branch information
domitry committed Dec 13, 2015
1 parent 71d93fe commit ac45f04
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/nyaplot/charts/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@ class Bar < ChartBase
## data: {ylabel: [1,2,3], xlabel: [:a, :b. :c]} or equal dataframe
def create(**opts)
data = opts[:data]
accessor = nil

unless opts[:fill_by].nil? && opts[:colors].nil?
cs = opts[:colors]
s = ad Layers::Scale.new({domain: data[opts[:x]], range: cs, type: "discrete"})
accessor = ad Layers::Accessor.new({scale: s, label: opts[:fill_by]})
end

data[opts[:x]].each_with_index do |str, i|
data2 = {x1: [-0.8], x2: [0.8], y2: [0], y1: [data[opts[:y]][i]]}
data_l = ad Layers::Data.new({data: data2})
data2 = data.reduce({}){|memo, pair| memo[pair[0]] = [pair[1][i]]; memo}
.merge({x1: [-0.8], x2: [0.8], y2: [0], y1: [data[opts[:y]][i]]})

pos = create_ordinal_position(opts[:xscale], opts[:yscale], str)
data_l = ad Layers::Data.new({data: data2})

args = {
data: data_l,
x1: :x1,
x2: :x2,
y1: :y1,
y2: :y2,
position: ad(create_ordinal_position(opts[:xscale], opts[:yscale], str))
position: create_ordinal_position(opts[:xscale], opts[:yscale], str),
stroke_color: 'none'
}

args[:color]= accessor unless accessor.nil?

ag Layers::Rect.new(args)
end

Expand Down

0 comments on commit ac45f04

Please sign in to comment.