Class: CmdPlot::Bar
- Inherits:
-
Object
- Object
- CmdPlot::Bar
- Defined in:
- lib/cmd_plot/bar.rb
Instance Method Summary collapse
- #bar(height, labels) ⇒ Object
- #histogram(data, bins = 20) ⇒ Object
-
#initialize(width = 140, height = 40, symbol = '°') ⇒ Bar
constructor
A new instance of Bar.
Constructor Details
#initialize(width = 140, height = 40, symbol = '°') ⇒ Bar
Returns a new instance of Bar.
2 3 4 5 6 |
# File 'lib/cmd_plot/bar.rb', line 2 def initialize(width = 140, height = 40, symbol = '°') @width = width @height = height @symbol = symbol end |
Instance Method Details
#bar(height, labels) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cmd_plot/bar.rb', line 8 def (height, labels) # Calculate scaling of data to plot max_height = height.max.to_f max_label_length = labels.map { |l| l.length }.max max_val_length = height.map { |l| l.to_s.length }.max = (@width - max_label_length - max_val_length - 3) # Plot each bar for data in height.zip(labels) label_fill_space = ' ' * (max_label_length - data[1].length) value_fill_space = ' ' * (max_val_length - data[0].to_s.length) = @symbol * ((data[0] / max_height) * ).round puts "#{label_fill_space}#{data[1]}: #{value_fill_space}#{data[0]} #{}" end return nil end |
#histogram(data, bins = 20) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/cmd_plot/bar.rb', line 24 def histogram(data, bins = 20) # Turn data into bins with counts counts, bin_centers = BasicData.build_hist(data, bins) # Plot as bar chart (counts, bin_centers.map { |p| p.round(1).to_s }) end |