Class: TimeWise::Visualization

Inherits:
Object
  • Object
show all
Defined in:
lib/time_wise/visualization.rb

Overview

Visualization methods for time series data

Instance Method Summary collapse

Constructor Details

#initialize(time_series) ⇒ Visualization

Returns a new instance of Visualization.



8
9
10
11
# File 'lib/time_wise/visualization.rb', line 8

def initialize(time_series)
  @ts = time_series
  @data = @ts.data
end

Instance Method Details

#comparison_chart(other_ts, title = "Time Series Comparison") ⇒ String

Create a comparison chart between two time series

Parameters:

  • other_ts (TimeWise::Base)

    Another time series to compare with

  • title (String) (defaults to: "Time Series Comparison")

    Optional title for the chart

Returns:

  • (String)

    ASCII chart representation

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/time_wise/visualization.rb', line 37

def comparison_chart(other_ts, title = "Time Series Comparison")
  # Check if time series have compatible lengths
  raise ArgumentError, "Time series must have the same length for comparison" if @ts.length != other_ts.length

  # Prepare data for both series
  data_points1 = prepare_data_points("Series 1")

  # Prepare data for second series
  other_data = other_ts.data
  data_points2 = []

  if @ts.dates
    other_ts.dates.each_with_index do |date, idx|
      label = date.strftime("%Y-%m-%d")
      data_points2 << [label, other_data[idx]]
    end
  else
    other_data.to_a.each_with_index do |val, idx|
      data_points2 << [idx.to_s, val]
    end
  end

  # Generate side-by-side charts
  chart1 = AsciiCharts::Cartesian.new(
    data_points1,
    title: "#{title} - Series 1",
    bar: false,
    hide_zero: true
  ).draw

  chart2 = AsciiCharts::Cartesian.new(
    data_points2,
    title: "#{title} - Series 2",
    bar: false,
    hide_zero: true
  ).draw

  # Combine the charts
  combined_chart = "#{chart1}\n\n#{chart2}"

  # Print and return the combined chart
  puts combined_chart
  combined_chart
end

#line_chart(title = "Time Series") ⇒ String

Create a line chart of the time series

Parameters:

  • title (String) (defaults to: "Time Series")

    Optional title for the chart

Returns:

  • (String)

    ASCII chart representation



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/time_wise/visualization.rb', line 16

def line_chart(title = "Time Series")
  # Prepare data for ASCII chart
  data_points = prepare_data_points

  # Generate the chart
  chart = AsciiCharts::Cartesian.new(
    data_points,
    title: title,
    bar: false,
    hide_zero: true
  ).draw

  # Print and return the chart
  puts chart
  chart
end