mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-07 23:51:12 +08:00
Separation of Plot and Command Modules
This commit is contained in:
parent
4a14e4716a
commit
a09a703c33
@ -1,6 +1,7 @@
|
|||||||
require 'unicode_plot'
|
require 'unicode_plot'
|
||||||
require 'uplot/version'
|
require 'uplot/version'
|
||||||
require 'uplot/preprocessing'
|
require 'uplot/preprocessing'
|
||||||
|
require 'uplot/plot'
|
||||||
require 'uplot/command'
|
require 'uplot/command'
|
||||||
|
|
||||||
module Uplot
|
module Uplot
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
require 'optparse'
|
require 'optparse'
|
||||||
require 'unicode_plot'
|
|
||||||
require_relative 'preprocessing'
|
require_relative 'preprocessing'
|
||||||
|
|
||||||
module Uplot
|
module Uplot
|
||||||
@ -34,7 +33,7 @@ module Uplot
|
|||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :params, :plot_type
|
attr_accessor :params, :plot_type
|
||||||
attr_reader :raw_inputs
|
attr_reader :raw_inputs, :data, :fmt
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@params = Params.new
|
@params = Params.new
|
||||||
@ -250,22 +249,21 @@ module Uplot
|
|||||||
@data = Preprocessing.input(input, @delimiter, @headers, @transpose)
|
@data = Preprocessing.input(input, @delimiter, @headers, @transpose)
|
||||||
case plot_type
|
case plot_type
|
||||||
when :bar, :barplot
|
when :bar, :barplot
|
||||||
barplot(@data)
|
Plot.barplot(data, params, @count)
|
||||||
when :count, :c
|
when :count, :c
|
||||||
@count = true
|
Plot.barplot(data, params, count = true)
|
||||||
barplot(@data)
|
|
||||||
when :hist, :histogram
|
when :hist, :histogram
|
||||||
histogram(@data)
|
Plot.histogram(data, params)
|
||||||
when :line, :lineplot
|
when :line, :lineplot
|
||||||
line(@data)
|
Plot.line(data, params)
|
||||||
when :lines, :lineplots
|
when :lines, :lineplots
|
||||||
lines(@data)
|
Plot.lines(data, params, fmt)
|
||||||
when :scatter, :scatterplot
|
when :scatter, :scatterplot
|
||||||
scatter(@data)
|
Plot.scatter(data, params, fmt)
|
||||||
when :density
|
when :density
|
||||||
density(@data)
|
Plot.density(data, params, fmt)
|
||||||
when :box, :boxplot
|
when :box, :boxplot
|
||||||
boxplot(@data)
|
Plot.boxplot(data, params)
|
||||||
else
|
else
|
||||||
raise "unrecognized plot_type: #{plot_type}"
|
raise "unrecognized plot_type: #{plot_type}"
|
||||||
end.render($stderr)
|
end.render($stderr)
|
||||||
@ -273,120 +271,5 @@ module Uplot
|
|||||||
print input if @output
|
print input if @output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def barplot(data)
|
|
||||||
headers = data.headers
|
|
||||||
series = data.series
|
|
||||||
if @count
|
|
||||||
series = Preprocessing.count(series[0])
|
|
||||||
params.title = headers[0] if headers
|
|
||||||
end
|
|
||||||
params.title ||= headers[1] if headers
|
|
||||||
labels = series[0]
|
|
||||||
values = series[1].map(&:to_f)
|
|
||||||
UnicodePlot.barplot(labels, values, **params.to_hc)
|
|
||||||
end
|
|
||||||
|
|
||||||
def histogram(data)
|
|
||||||
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
|
|
||||||
|
|
||||||
def line(data)
|
|
||||||
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,
|
|
||||||
# assume that the first 2 series are the x and y series respectively.
|
|
||||||
if headers
|
|
||||||
params.xlabel ||= headers[0]
|
|
||||||
params.ylabel ||= headers[1]
|
|
||||||
end
|
|
||||||
x = series[0].map(&:to_f)
|
|
||||||
y = series[1].map(&:to_f)
|
|
||||||
UnicodePlot.lineplot(x, y, **params.to_hc)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_method2(method1)
|
|
||||||
(method1.to_s + '!').to_sym
|
|
||||||
end
|
|
||||||
|
|
||||||
def xyy_plot(data, method1)
|
|
||||||
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.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
|
|
||||||
|
|
||||||
def xyxy_plot(data, method1)
|
|
||||||
headers = data.headers
|
|
||||||
series = data.series
|
|
||||||
method2 = get_method2(method1)
|
|
||||||
series.map! { |s| s.map(&:to_f) }
|
|
||||||
series = series.each_slice(2).to_a
|
|
||||||
params.name ||= headers[0] if headers
|
|
||||||
params.xlim = series.map(&:first).flatten.minmax # why need?
|
|
||||||
params.ylim = series.map(&:last).flatten.minmax # why need?
|
|
||||||
x1, y1 = series.shift
|
|
||||||
plot = UnicodePlot.public_send(method1, x1, y1, **params.to_hc)
|
|
||||||
series.each_with_index do |(xi, yi), i|
|
|
||||||
UnicodePlot.public_send(method2, plot, xi, yi, name: headers&.[]((i + 1) * 2))
|
|
||||||
end
|
|
||||||
plot
|
|
||||||
end
|
|
||||||
|
|
||||||
def lines(data)
|
|
||||||
case @fmt
|
|
||||||
when 'xyy'
|
|
||||||
xyy_plot(data, :lineplot)
|
|
||||||
when 'xyxy'
|
|
||||||
xyxy_plot(data, :lineplot)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def scatter(data)
|
|
||||||
case @fmt
|
|
||||||
when 'xyy'
|
|
||||||
xyy_plot(data, :scatterplot)
|
|
||||||
when 'xyxy'
|
|
||||||
xyxy_plot(data, :scatterplot)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def density(data)
|
|
||||||
case @fmt
|
|
||||||
when 'xyy'
|
|
||||||
xyy_plot(data, :densityplot)
|
|
||||||
when 'xyxy'
|
|
||||||
xyxy_plot(data, :densityplot)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def boxplot(data)
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
123
lib/uplot/plot.rb
Normal file
123
lib/uplot/plot.rb
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
require 'unicode_plot'
|
||||||
|
|
||||||
|
module Uplot
|
||||||
|
# plotting functions.
|
||||||
|
module Plot
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def barplot(data, params, count = false)
|
||||||
|
headers = data.headers
|
||||||
|
series = data.series
|
||||||
|
if count
|
||||||
|
series = Preprocessing.count(series[0])
|
||||||
|
params.title = headers[0] if headers
|
||||||
|
end
|
||||||
|
params.title ||= headers[1] if headers
|
||||||
|
labels = series[0]
|
||||||
|
values = series[1].map(&:to_f)
|
||||||
|
UnicodePlot.barplot(labels, values, **params.to_hc)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def line(data, params)
|
||||||
|
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,
|
||||||
|
# assume that the first 2 series are the x and y series respectively.
|
||||||
|
if headers
|
||||||
|
params.xlabel ||= headers[0]
|
||||||
|
params.ylabel ||= headers[1]
|
||||||
|
end
|
||||||
|
x = series[0].map(&:to_f)
|
||||||
|
y = series[1].map(&:to_f)
|
||||||
|
UnicodePlot.lineplot(x, y, **params.to_hc)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_method2(method1)
|
||||||
|
(method1.to_s + '!').to_sym
|
||||||
|
end
|
||||||
|
|
||||||
|
def xyy_plot(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.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
|
||||||
|
|
||||||
|
def xyxy_plot(data, method1, params)
|
||||||
|
headers = data.headers
|
||||||
|
series = data.series
|
||||||
|
method2 = get_method2(method1)
|
||||||
|
series.map! { |s| s.map(&:to_f) }
|
||||||
|
series = series.each_slice(2).to_a
|
||||||
|
params.name ||= headers[0] if headers
|
||||||
|
params.xlim = series.map(&:first).flatten.minmax # why need?
|
||||||
|
params.ylim = series.map(&:last).flatten.minmax # why need?
|
||||||
|
x1, y1 = series.shift
|
||||||
|
plot = UnicodePlot.public_send(method1, x1, y1, **params.to_hc)
|
||||||
|
series.each_with_index do |(xi, yi), i|
|
||||||
|
UnicodePlot.public_send(method2, plot, xi, yi, name: headers&.[]((i + 1) * 2))
|
||||||
|
end
|
||||||
|
plot
|
||||||
|
end
|
||||||
|
|
||||||
|
def lines(data, params, fmt = 'xyy')
|
||||||
|
case fmt
|
||||||
|
when 'xyy'
|
||||||
|
xyy_plot(data, :lineplot, params)
|
||||||
|
when 'xyxy'
|
||||||
|
xyxy_plot(data, :lineplot, params)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def scatter(data, params, fmt = 'xyy')
|
||||||
|
case fmt
|
||||||
|
when 'xyy'
|
||||||
|
xyy_plot(data, :scatterplot, params)
|
||||||
|
when 'xyxy'
|
||||||
|
xyxy_plot(data, :scatterplot, params)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def density(data, params, fmt = 'xyy')
|
||||||
|
case fmt
|
||||||
|
when 'xyy'
|
||||||
|
xyy_plot(data, :densityplot, params)
|
||||||
|
when 'xyxy'
|
||||||
|
xyxy_plot(data, :densityplot, params)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user