Sha256: e9d6e885b3c82e20fb209633a757c53a2887d2bb496ebe062a25789571240d37

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module RubyvisCharts
  class BarTimelineChart < AbstractTimelineChart
    module DefaultArguments
      BARS_PADDING = Padding.new(right: 0.5, left: 0.5)
      BARS_COLORS = ['#4d79da'].freeze
    end

    attr_reader :bars_padding, :bars_colors

    def initialize(
      bars_padding: DefaultArguments::BARS_PADDING,
      bars_colors: DefaultArguments::BARS_COLORS,
      **other
    )
      super(**other)

      @bars_padding = bars_padding
      @bars_colors = bars_colors

      initialize_bars!
    end

    private

    def initialize_bars!
      chart = self

      bar_left_indent = -> { self.index * chart.send(:bars_widths).range_band + chart.bars_padding.left }
      fill_style_colors = -> { chart.send(:bars_colors_iterator, self.index, self.height, chart.bars_colors) }

      @layer_timeline.add(Rubyvis::Bar)
        .data(values)
        .width(bars_widths.range_band - bars_padding.right - bars_padding.left)
        .height(bars_heights)
        .left(bar_left_indent)
        .bottom(0)
        .fillStyle(fill_style_colors)
    end

    def bars_widths
      @bars_widths ||= Rubyvis::Scale.ordinal(Rubyvis.range(values.length)).split_banded(0, timeline_width)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyvis_charts-0.1.6 lib/rubyvis_charts/bar_timeline_chart.rb