Class: Termplot::Series

Inherits:
Object
  • Object
show all
Defined in:
lib/termplot/series.rb

Constant Summary collapse

DEFAULT_COLOR =
"red"
DEFAULT_LINE_STYLE =
"line"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_data_points:, title: "Series", color: DEFAULT_COLOR, line_style: DEFAULT_LINE_STYLE) ⇒ Series

Returns a new instance of Series.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/termplot/series.rb', line 9

def initialize(max_data_points:, title: "Series", color: DEFAULT_COLOR, line_style: DEFAULT_LINE_STYLE)
  @data = []
  @max_data_points = max_data_points
  @min = 0
  @max = 0
  @range = 0

  @title = title
  @color = Termplot::Colors.fetch(color, DEFAULT_COLOR)
  @line_style = Termplot::CharacterMap::LINE_STYLES.fetch(
    line_style,
    Termplot::CharacterMap::LINE_STYLES[DEFAULT_LINE_STYLE]
  )
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



3
4
5
# File 'lib/termplot/series.rb', line 3

def color
  @color
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/termplot/series.rb', line 3

def data
  @data
end

#line_styleObject (readonly)

Returns the value of attribute line_style.



3
4
5
# File 'lib/termplot/series.rb', line 3

def line_style
  @line_style
end

#maxObject (readonly)

Returns the value of attribute max.



3
4
5
# File 'lib/termplot/series.rb', line 3

def max
  @max
end

#max_data_pointsObject

Returns the value of attribute max_data_points.



4
5
6
# File 'lib/termplot/series.rb', line 4

def max_data_points
  @max_data_points
end

#minObject (readonly)

Returns the value of attribute min.



3
4
5
# File 'lib/termplot/series.rb', line 3

def min
  @min
end

#rangeObject (readonly)

Returns the value of attribute range.



3
4
5
# File 'lib/termplot/series.rb', line 3

def range
  @range
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/termplot/series.rb', line 3

def title
  @title
end

Instance Method Details

#add_point(point) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/termplot/series.rb', line 24

def add_point(point)
  @data.push(point)

  while @data.length > max_data_points do
    @data.shift
  end

  @min = data.min
  @max = data.max
  @range = (max - min).abs
  @range = 1 if range.zero?
end