Sha256: ce7d684df6fb6ec996833f1e6917c82b2c25613b9be1eea0beb8adb1a6674f2d

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

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

  # @private
  def self.metrics(value, format, proc_text_metrics)
    val = begin
      if format.is_a?(Proc)
        format.call(value)
      else
        sprintf(format || '%.2f', value).commify
      end
    end

    metrics = proc_text_metrics.call(val)
    [val, metrics]
  end

  # @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, proc_text_metrics)
      left_x, left_y, _right_x, _right_y = @coordinate
      val, metrics = Gruff::BarValueLabel.metrics(@value, format, proc_text_metrics)

      y = @value >= 0 ? left_y - metrics.height - 5 : left_y + 5
      yield left_x, y, val, metrics.width, metrics.height
    end
  end

  # @private
  class SideBar < Base
    def prepare_rendering(format, proc_text_metrics)
      left_x, left_y, right_x, _right_y = @coordinate
      val, metrics = Gruff::BarValueLabel.metrics(@value, format, proc_text_metrics)

      x = @value >= 0 ? right_x + 10 : left_x - metrics.width - 10
      yield x, left_y, val, metrics.width, metrics.height
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
gruff-0.24.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.24.0 lib/gruff/helper/bar_value_label.rb
gruff-0.23.0 lib/gruff/helper/bar_value_label.rb
gruff-0.23.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.22.0 lib/gruff/helper/bar_value_label.rb
gruff-0.22.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.21.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.21.0 lib/gruff/helper/bar_value_label.rb
gruff-0.20.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.20.0 lib/gruff/helper/bar_value_label.rb
gruff-0.19.0 lib/gruff/helper/bar_value_label.rb
gruff-0.19.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.18.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.18.0 lib/gruff/helper/bar_value_label.rb
gruff-0.17.0-java lib/gruff/helper/bar_value_label.rb
gruff-0.17.0 lib/gruff/helper/bar_value_label.rb