Class: TTY::Pie::DataItem
- Inherits:
-
Object
- Object
- TTY::Pie::DataItem
- 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
-
#angle ⇒ Object
private
The item start angle.
-
#color ⇒ Object
Returns the value of attribute color.
-
#fill ⇒ Object
Returns the value of attribute fill.
-
#name ⇒ Object
Returns the value of attribute name.
-
#percent ⇒ Object
Returns the value of attribute percent.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name, value, percent, color, fill) ⇒ DataItem
constructor
private
Creat a DataItem.
-
#number_to_currency(value, precision: 2, delimiter: ",") ⇒ String
private
Convert a number to a currency.
-
#to_label(legend) ⇒ String
private
Convert a data item into a legend label.
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
#angle ⇒ Object
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 |
#color ⇒ Object
Returns the value of attribute color.
19 20 21 |
# File 'lib/tty/pie/data_item.rb', line 19 def color @color end |
#fill ⇒ Object
Returns the value of attribute fill.
21 22 23 |
# File 'lib/tty/pie/data_item.rb', line 21 def fill @fill end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/tty/pie/data_item.rb', line 11 def name @name end |
#percent ⇒ Object
Returns the value of attribute percent.
15 16 17 |
# File 'lib/tty/pie/data_item.rb', line 15 def percent @percent end |
#value ⇒ Object
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
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
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 |