Sha256: 345350697cb42dd0df27ea4a80bba0e4676a0ce8a06b3e99a00c0fc2fb5ed600

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

# @private
module Gruff::BarValueLabel
  using String::GruffCommify

  # @private
  class Base
    attr_reader :coordinate, :value

    def initialize(coordinate, value)
      @coordinate = coordinate
      @value = value
    end
  end

  # @private
  class Bar < Base
    def prepare_rendering(format, _bar_width = 0)
      left_x, left_y, right_x, _right_y = @coordinate
      if format.is_a?(Proc)
        val = format.call(@value)
      else
        val = sprintf(format || '%.2f', @value).commify
      end

      y = @value >= 0 ? left_y - 30 : left_y + 12
      yield left_x + (right_x - left_x) / 2, y, val
    end
  end

  # @private
  class SideBar < Base
    def prepare_rendering(format, bar_width = 0)
      left_x, _left_y, right_x, right_y = @coordinate
      if format.is_a?(Proc)
        val = format.call(@value)
      else
        val = sprintf(format || '%.2f', @value).commify
      end

      x = @value >= 0 ? right_x + 40 : left_x - 40
      yield x, right_y - bar_width / 2, val
    end
  end

  # @private
  class StackedBar
    def initialize
      @bars = []
    end

    def add(bar, index)
      bars = @bars[index] || []
      bars << bar
      @bars[index] = bars
    end

    def prepare_rendering(format, bar_width = 0, &block)
      @bars.each do |bars|
        value = bars.sum(&:value)
        bar = bars.last
        bar_value_label = bar.class.new(bar.coordinate, value)
        bar_value_label.prepare_rendering(format, bar_width, &block)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gruff-0.14.0 lib/gruff/helper/bar_value_label.rb
gruff-0.14.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.13.0 lib/gruff/helper/bar_value_label.rb
gruff-0.13.0-java lib/gruff/helper/bar_value_label.rb