Class: MiniHistogram::MiniUnicodePlot::Plot

Inherits:
Object
  • Object
show all
Includes:
StyledPrinter
Defined in:
lib/mini_histogram/plot.rb

Direct Known Subclasses

Barplot

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

Instance Method Summary collapse

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.



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/mini_histogram/plot.rb', line 576

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

#borderObject (readonly)

Returns the value of attribute border.



602
603
604
# File 'lib/mini_histogram/plot.rb', line 602

def border
  @border
end

#colors_decoObject (readonly)

Returns the value of attribute colors_deco.



610
611
612
# File 'lib/mini_histogram/plot.rb', line 610

def colors_deco
  @colors_deco
end

#colors_leftObject (readonly)

Returns the value of attribute colors_left.



606
607
608
# File 'lib/mini_histogram/plot.rb', line 606

def colors_left
  @colors_left
end

#colors_rightObject (readonly)

Returns the value of attribute colors_right.



608
609
610
# File 'lib/mini_histogram/plot.rb', line 608

def colors_right
  @colors_right
end

#decorationsObject (readonly)

Returns the value of attribute decorations.



609
610
611
# File 'lib/mini_histogram/plot.rb', line 609

def decorations
  @decorations
end

#labels_leftObject (readonly)

Returns the value of attribute labels_left.



605
606
607
# File 'lib/mini_histogram/plot.rb', line 605

def labels_left
  @labels_left
end

#labels_rightObject (readonly)

Returns the value of attribute labels_right.



607
608
609
# File 'lib/mini_histogram/plot.rb', line 607

def labels_right
  @labels_right
end

#marginObject (readonly)

Returns the value of attribute margin.



603
604
605
# File 'lib/mini_histogram/plot.rb', line 603

def margin
  @margin
end

#paddingObject (readonly)

Returns the value of attribute padding.



604
605
606
# File 'lib/mini_histogram/plot.rb', line 604

def padding
  @padding
end

#titleObject (readonly)

Returns the value of attribute title.



599
600
601
# File 'lib/mini_histogram/plot.rb', line 599

def title
  @title
end

#xlabelObject (readonly)

Returns the value of attribute xlabel.



600
601
602
# File 'lib/mini_histogram/plot.rb', line 600

def xlabel
  @xlabel
end

#ylabelObject (readonly)

Returns the value of attribute ylabel.



601
602
603
# File 'lib/mini_histogram/plot.rb', line 601

def ylabel
  @ylabel
end

Instance Method Details

#annotate!(loc, value, color: :normal) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/mini_histogram/plot.rb', line 632

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



659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/mini_histogram/plot.rb', line 659

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_colorObject



685
686
687
688
689
# File 'lib/mini_histogram/plot.rb', line 685

def next_color
  COLOR_CYCLE[@auto_color]
ensure
  @auto_color = (@auto_color + 1) % COLOR_CYCLE.length
end

#render(out) ⇒ Object



672
673
674
# File 'lib/mini_histogram/plot.rb', line 672

def render(out)
  Renderer.render(out, self)
end

#show_labels?Boolean

Returns:

  • (Boolean)


628
629
630
# File 'lib/mini_histogram/plot.rb', line 628

def show_labels?
  @show_labels
end

#title_given?Boolean

Returns:

  • (Boolean)


612
613
614
# File 'lib/mini_histogram/plot.rb', line 612

def title_given?
  title && title != ""
end

#to_sObject



691
692
693
694
695
696
697
# File 'lib/mini_histogram/plot.rb', line 691

def to_s
  StringIO.open do |sio|
    render(sio)
    sio.close
    sio.string
  end
end

#xlabel_given?Boolean

Returns:

  • (Boolean)


616
617
618
# File 'lib/mini_histogram/plot.rb', line 616

def xlabel_given?
  xlabel && xlabel != ""
end

#ylabel_given?Boolean

Returns:

  • (Boolean)


620
621
622
# File 'lib/mini_histogram/plot.rb', line 620

def ylabel_given?
  ylabel && ylabel != ""
end

#ylabel_lengthObject



624
625
626
# File 'lib/mini_histogram/plot.rb', line 624

def ylabel_length
  (ylabel && ylabel.length) || 0
end