Module: YouPlot::Backends::UnicodePlot

Defined in:
lib/youplot/backends/unicode_plot.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.barplot(data, params, fmt = nil, count: false, reverse: false) ⇒ Object



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
# File 'lib/youplot/backends/unicode_plot.rb', line 38

def barplot(data, params, fmt = nil, count: false, reverse: false)
  headers = data.headers
  series = data.series
  # `uplot count`
  if count
    series = YouPlot::Aggregation.count_values(series[0], reverse: reverse)
    params.title = headers[0] if headers
  end
  if series.size == 1
    # If there is only one series.use the line number for label.
    params.title ||= headers[0] if headers
    labels = Array.new(series[0].size) { |i| (i + 1).to_s }
    raw_values = series[0]
    values = series[0].map(&:to_f)
  else
    # If there are 2 or more series...
    if fmt == 'yx'
      # assume that the first 2 series are the y and x series respectively.
      x_col = 1
      y_col = 0
    else
      # assume that the first 2 series are the x and y series respectively.
      x_col = 0
      y_col = 1
    end
    params.title ||= headers[y_col] if headers
    labels = series[x_col]
    raw_values = series[y_col]
    values = if count
               series[y_col].map(&:to_i)
             else
               series[y_col].map(&:to_f)
             end
  end
  values = values.map(&:to_i) if count || integer_display_values?(values, raw_values)
  ::UnicodePlot.barplot(labels, values, **params.to_hc)
end

.boxplot(data, params) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/youplot/backends/unicode_plot.rb', line 191

def boxplot(data, params)
  headers = data.headers
  series = data.series
  headers ||= (1..series.size).map(&:to_s)
  series.map! { |s| s.map(&:to_f) }
  ::UnicodePlot.boxplot(headers, series, **params.to_hc)
end

.check_series_size(data, fmt) ⇒ Object



219
220
221
222
223
# File 'lib/youplot/backends/unicode_plot.rb', line 219

def check_series_size(data, fmt)
  series = data.series
  raise_if_single_series(data, series) if series.size == 1
  raise_if_odd_series_for_xyxy(data, fmt, series)
end

.colors(color_names = false) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/youplot/backends/unicode_plot.rb', line 199

def colors(color_names = false)
  # FIXME
  s = String.new
  ::UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v|
    s << v
    s << k.to_s
    unless color_names
      s << "\t"
      s << ''
    end
    s << "\033[0m"
    s << "\t"
  end
  s << "\n"
  def s.render(obj)
    obj.print(self)
  end
  s
end

.density(data, params, fmt = 'xyy') ⇒ Object



186
187
188
189
# File 'lib/youplot/backends/unicode_plot.rb', line 186

def density(data, params, fmt = 'xyy')
  check_series_size(data, fmt)
  plot_fmt(data, fmt, :densityplot, params)
end

.get_method2(method1) ⇒ Object



124
125
126
# File 'lib/youplot/backends/unicode_plot.rb', line 124

def get_method2(method1)
  "#{method1}!".to_sym
end

.histogram(data, params) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/youplot/backends/unicode_plot.rb', line 87

def histogram(data, params)
  headers = data.headers
  series = data.series
  params.title ||= data.headers[0] if headers
  values = series[0].map(&:to_f)
  ::UnicodePlot.histogram(values, **params.to_hc)
end

.integer_display_values?(values, raw_values) ⇒ Boolean

True only for integer literals: “3” => true, “3.0” => false.

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/youplot/backends/unicode_plot.rb', line 77

def integer_display_values?(values, raw_values)
  values.all? { |v| v.finite? && v == v.to_i } &&
    raw_values.all? { |v| integer_literal?(v) }
end

.integer_literal?(value) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/youplot/backends/unicode_plot.rb', line 82

def integer_literal?(value)
  # Matches "3" and "-12", but not "3.0".
  value.to_s.match?(/\A[+-]?\d+\z/)
end

.line(data, params, fmt = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/youplot/backends/unicode_plot.rb', line 95

def line(data, params, fmt = nil)
  headers = data.headers
  series = data.series
  if series.size == 1
    # If there is only one series, it is assumed to be sequential data.
    params.ylabel ||= headers[0] if headers
    y = series[0].map(&:to_f)
    ::UnicodePlot.lineplot(y, **params.to_hc)
  else
    # If there are 2 or more series...
    if fmt == 'yx'
      # assume that the first 2 series are the y and x series respectively.
      x_col = 1
      y_col = 0
    else
      # assume that the first 2 series are the x and y series respectively.
      x_col = 0
      y_col = 1
    end
    if headers
      params.xlabel ||= headers[x_col]
      params.ylabel ||= headers[y_col]
    end
    x = series[x_col].map(&:to_f)
    y = series[y_col].map(&:to_f)
    ::UnicodePlot.lineplot(x, y, **params.to_hc)
  end
end

.lines(data, params, fmt = 'xyy') ⇒ Object



176
177
178
179
# File 'lib/youplot/backends/unicode_plot.rb', line 176

def lines(data, params, fmt = 'xyy')
  check_series_size(data, fmt)
  plot_fmt(data, fmt, :lineplot, params)
end

.plot_fmt(data, fmt, method1, params) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/youplot/backends/unicode_plot.rb', line 163

def plot_fmt(data, fmt, method1, params)
  case fmt
  when 'xyy'
    plot_xyy(data, method1, params)
  when 'xyxy'
    plot_xyxy(data, method1, params)
  when 'yx'
    raise "Incorrect format: #{fmt}"
  else
    raise "Unknown format: #{fmt}"
  end
end

.plot_xyxy(data, method1, params) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/youplot/backends/unicode_plot.rb', line 146

def plot_xyxy(data, method1, params)
  headers = data.headers
  series2 = data.series
                .map { |s| s.map(&:to_f) }
                .each_slice(2).to_a
  method2 = get_method2(method1)
  params.name ||= headers[0] if headers
  params.xlim ||= series2.map(&:first).flatten.minmax # why need?
  params.ylim ||= series2.map(&:last).flatten.minmax # why need?
  x1, y1 = series2.shift
  plot = ::UnicodePlot.public_send(method1, x1, y1, **params.to_hc)
  series2.each_with_index do |(xi, yi), i|
    ::UnicodePlot.public_send(method2, plot, xi, yi, name: headers&.[]((i + 1) * 2))
  end
  plot
end

.plot_xyy(data, method1, params) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/youplot/backends/unicode_plot.rb', line 128

def plot_xyy(data, method1, params)
  headers = data.headers
  series = data.series
  method2 = get_method2(method1)
  series.map! { |s| s.map(&:to_f) }
  if headers
    params.name   ||= headers[1]
    params.xlabel ||= headers[0]
  end
  params.xlim ||= series[0].flatten.minmax # why need?
  params.ylim ||= series[1..-1].flatten.minmax # why need?
  plot = ::UnicodePlot.public_send(method1, series[0], series[1], **params.to_hc)
  2.upto(series.size - 1) do |i|
    ::UnicodePlot.public_send(method2, plot, series[0], series[i], name: headers&.[](i))
  end
  plot
end

.raise_if_odd_series_for_xyxy(data, fmt, series) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/youplot/backends/unicode_plot.rb', line 236

def raise_if_odd_series_for_xyxy(data, fmt, series)
  return unless fmt == 'xyxy'
  return if series.size.even?

  warn <<~EOS
    YouPlot: In the xyxy format, the number of series must be even.

    Number of series: \e[35m#{series.size}\e[0m
    Headers: \e[35m#{data.headers.inspect}\e[0m
  EOS
  raise_plot_error
end

.raise_if_single_series(data, series) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/youplot/backends/unicode_plot.rb', line 225

def raise_if_single_series(data, series)
  warn <<~EOS
    YouPlot: There is only one series of input data. Please check the delimiter.

    Headers: \e[35m#{data.headers.inspect}\e[0m
    The first item is: \e[35m\"#{series[0][0]}\"\e[0m
    The last item is : \e[35m\"#{series[0][-1]}\"\e[0m
  EOS
  raise_plot_error
end

.raise_plot_errorObject



249
250
251
252
# File 'lib/youplot/backends/unicode_plot.rb', line 249

def raise_plot_error
  # NOTE: Error messages cannot be colored.
  YouPlot.run_as_executable ? exit(1) : raise(Error)
end

.scatter(data, params, fmt = 'xyy') ⇒ Object



181
182
183
184
# File 'lib/youplot/backends/unicode_plot.rb', line 181

def scatter(data, params, fmt = 'xyy')
  check_series_size(data, fmt)
  plot_fmt(data, fmt, :scatterplot, params)
end