Class: TTY::Pie::DataItem

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/pie/data_item.rb

Overview

Encapsulates a single data item

Constant Summary collapse

LABEL_FORMAT =
"%<label>s %<name>s %<percent>.2f%%"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, percent, color, fill) ⇒ DataItem

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creat a DataItem



26
27
28
29
30
31
32
33
# File 'lib/tty/pie/data_item.rb', line 26

def initialize(name, value, percent, color, fill)
  @name = name
  @value = value
  @color = color
  @percent = percent
  @fill = fill
  @pastel = Pastel.new(enabled: !!color)
end

Instance Attribute Details

#angleObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The item start angle



38
39
40
# File 'lib/tty/pie/data_item.rb', line 38

def angle
  percent * FULL_CIRCLE_DEGREES / 100.to_f
end

#colorObject

Returns the value of attribute color.



19
20
21
# File 'lib/tty/pie/data_item.rb', line 19

def color
  @color
end

#fillObject

Returns the value of attribute fill.



21
22
23
# File 'lib/tty/pie/data_item.rb', line 21

def fill
  @fill
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/tty/pie/data_item.rb', line 11

def name
  @name
end

#percentObject

Returns the value of attribute percent.



15
16
17
# File 'lib/tty/pie/data_item.rb', line 15

def percent
  @percent
end

#valueObject

Returns the value of attribute value.



13
14
15
# File 'lib/tty/pie/data_item.rb', line 13

def value
  @value
end

Instance Method Details

#number_to_currency(value, precision: 2, delimiter: ",") ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert a number to a currency

Parameters:

  • value (Numeric)
  • precision (Integer) (defaults to: 2)
  • delimiter (String) (defaults to: ",")

Returns:

  • (String)


71
72
73
74
75
76
77
# File 'lib/tty/pie/data_item.rb', line 71

def number_to_currency(value, precision: 2, delimiter: ",")
  whole, part = value.to_s.split(".")
  unless part.nil?
    part = format("%.#{precision}f", part.to_f / 10**part.size)[1..-1]
  end
  "#{whole.gsub(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{delimiter}")}#{part}"
end

#to_label(legend) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert a data item into a legend label

Parameters:

  • legend (Hash)

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tty/pie/data_item.rb', line 49

def to_label(legend)
  pattern   = legend && legend[:format] || LABEL_FORMAT
  precision = legend && legend[:precision] || 2
  delimiter = legend && legend[:delimiter] || ","

  label = color ? @pastel.decorate(fill, color) : fill
  currency = number_to_currency(value, precision: precision,
                                       delimiter: delimiter)

  format(pattern, label: label, name: name, value: value,
                  percent: percent, currency: currency)
end