Class: MiniHistogram::MiniUnicodePlot::Plot
- Inherits:
-
Object
- Object
- MiniHistogram::MiniUnicodePlot::Plot
- Includes:
- StyledPrinter
- Defined in:
- lib/mini_histogram/plot.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_WIDTH =
40- DEFAULT_BORDER =
:solid- DEFAULT_MARGIN =
3- DEFAULT_PADDING =
1- COLOR_CYCLE =
[ :green, :blue, :red, :magenta, :yellow, :cyan ].freeze
Constants included from StyledPrinter
StyledPrinter::COLOR_DECODE, StyledPrinter::COLOR_ENCODE, StyledPrinter::DISABLE_TEXT_STYLE, StyledPrinter::TEXT_COLORS
Instance Attribute Summary collapse
-
#border ⇒ Object
readonly
Returns the value of attribute border.
-
#colors_deco ⇒ Object
readonly
Returns the value of attribute colors_deco.
-
#colors_left ⇒ Object
readonly
Returns the value of attribute colors_left.
-
#colors_right ⇒ Object
readonly
Returns the value of attribute colors_right.
-
#decorations ⇒ Object
readonly
Returns the value of attribute decorations.
-
#labels_left ⇒ Object
readonly
Returns the value of attribute labels_left.
-
#labels_right ⇒ Object
readonly
Returns the value of attribute labels_right.
-
#margin ⇒ Object
readonly
Returns the value of attribute margin.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#xlabel ⇒ Object
readonly
Returns the value of attribute xlabel.
-
#ylabel ⇒ Object
readonly
Returns the value of attribute ylabel.
Instance Method Summary collapse
- #annotate!(loc, value, color: :normal) ⇒ Object
- #annotate_row!(loc, row_index, value, color: :normal) ⇒ Object
-
#initialize(title: nil, xlabel: nil, ylabel: nil, border: DEFAULT_BORDER, margin: DEFAULT_MARGIN, padding: DEFAULT_PADDING, labels: true) ⇒ Plot
constructor
A new instance of Plot.
- #next_color ⇒ Object
- #render(out) ⇒ Object
- #show_labels? ⇒ Boolean
- #title_given? ⇒ Boolean
- #to_s ⇒ Object
- #xlabel_given? ⇒ Boolean
- #ylabel_given? ⇒ Boolean
- #ylabel_length ⇒ Object
Methods included from StyledPrinter
#color?, #print_color, #print_styled
Constructor Details
#initialize(title: nil, xlabel: nil, ylabel: nil, border: DEFAULT_BORDER, margin: DEFAULT_MARGIN, padding: DEFAULT_PADDING, labels: true) ⇒ Plot
Returns a new instance of Plot.
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/mini_histogram/plot.rb', line 573 def initialize(title: nil, xlabel: nil, ylabel: nil, border: DEFAULT_BORDER, margin: DEFAULT_MARGIN, padding: DEFAULT_PADDING, labels: true) @title = title @xlabel = xlabel @ylabel = ylabel @border = border @margin = check_margin(margin) @padding = padding @labels_left = {} @colors_left = {} @labels_right = {} @colors_right = {} @decorations = {} @colors_deco = {} @show_labels = labels @auto_color = 0 end |
Instance Attribute Details
#border ⇒ Object (readonly)
Returns the value of attribute border.
599 600 601 |
# File 'lib/mini_histogram/plot.rb', line 599 def border @border end |
#colors_deco ⇒ Object (readonly)
Returns the value of attribute colors_deco.
607 608 609 |
# File 'lib/mini_histogram/plot.rb', line 607 def colors_deco @colors_deco end |
#colors_left ⇒ Object (readonly)
Returns the value of attribute colors_left.
603 604 605 |
# File 'lib/mini_histogram/plot.rb', line 603 def colors_left @colors_left end |
#colors_right ⇒ Object (readonly)
Returns the value of attribute colors_right.
605 606 607 |
# File 'lib/mini_histogram/plot.rb', line 605 def colors_right @colors_right end |
#decorations ⇒ Object (readonly)
Returns the value of attribute decorations.
606 607 608 |
# File 'lib/mini_histogram/plot.rb', line 606 def decorations @decorations end |
#labels_left ⇒ Object (readonly)
Returns the value of attribute labels_left.
602 603 604 |
# File 'lib/mini_histogram/plot.rb', line 602 def labels_left @labels_left end |
#labels_right ⇒ Object (readonly)
Returns the value of attribute labels_right.
604 605 606 |
# File 'lib/mini_histogram/plot.rb', line 604 def labels_right @labels_right end |
#margin ⇒ Object (readonly)
Returns the value of attribute margin.
600 601 602 |
# File 'lib/mini_histogram/plot.rb', line 600 def margin @margin end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
601 602 603 |
# File 'lib/mini_histogram/plot.rb', line 601 def padding @padding end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
596 597 598 |
# File 'lib/mini_histogram/plot.rb', line 596 def title @title end |
#xlabel ⇒ Object (readonly)
Returns the value of attribute xlabel.
597 598 599 |
# File 'lib/mini_histogram/plot.rb', line 597 def xlabel @xlabel end |
#ylabel ⇒ Object (readonly)
Returns the value of attribute ylabel.
598 599 600 |
# File 'lib/mini_histogram/plot.rb', line 598 def ylabel @ylabel end |
Instance Method Details
#annotate!(loc, value, color: :normal) ⇒ Object
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/mini_histogram/plot.rb', line 629 def annotate!(loc, value, color: :normal) case loc when :l (0 ... n_rows).each do |row| if @labels_left.fetch(row, "") == "" @labels_left[row] = value @colors_left[row] = color break end end when :r (0 ... n_rows).each do |row| if @labels_right.fetch(row, "") == "" @labels_right[row] = value @colors_right[row] = color break end end when :t, :b, :tl, :tr, :bl, :br @decorations[loc] = value @colors_deco[loc] = color else raise ArgumentError, "unknown location to annotate (#{loc.inspect} for :t, :b, :l, :r, :tl, :tr, :bl, or :br)" end end |
#annotate_row!(loc, row_index, value, color: :normal) ⇒ Object
656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/mini_histogram/plot.rb', line 656 def annotate_row!(loc, row_index, value, color: :normal) case loc when :l @labels_left[row_index] = value @colors_left[row_index] = color when :r @labels_right[row_index] = value @colors_right[row_index] = color else raise ArgumentError, "unknown location `#{loc}`, try :l or :r instead" end end |
#next_color ⇒ Object
682 683 684 685 686 |
# File 'lib/mini_histogram/plot.rb', line 682 def next_color COLOR_CYCLE[@auto_color] ensure @auto_color = (@auto_color + 1) % COLOR_CYCLE.length end |
#render(out) ⇒ Object
669 670 671 |
# File 'lib/mini_histogram/plot.rb', line 669 def render(out) Renderer.render(out, self) end |
#show_labels? ⇒ Boolean
625 626 627 |
# File 'lib/mini_histogram/plot.rb', line 625 def show_labels? @show_labels end |
#title_given? ⇒ Boolean
609 610 611 |
# File 'lib/mini_histogram/plot.rb', line 609 def title_given? title && title != "" end |
#to_s ⇒ Object
688 689 690 691 692 693 694 |
# File 'lib/mini_histogram/plot.rb', line 688 def to_s StringIO.open do |sio| render(sio) sio.close sio.string end end |
#xlabel_given? ⇒ Boolean
613 614 615 |
# File 'lib/mini_histogram/plot.rb', line 613 def xlabel_given? xlabel && xlabel != "" end |
#ylabel_given? ⇒ Boolean
617 618 619 |
# File 'lib/mini_histogram/plot.rb', line 617 def ylabel_given? ylabel && ylabel != "" end |
#ylabel_length ⇒ Object
621 622 623 |
# File 'lib/mini_histogram/plot.rb', line 621 def ylabel_length (ylabel && ylabel.length) || 0 end |