mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-06-24 18:11:12 +08:00
Standing on the shoulders of CSV library.
This commit is contained in:
parent
87a8af4957
commit
b4856e45d0
@ -1,5 +1,4 @@
|
|||||||
require 'unicode_plot'
|
require 'unicode_plot'
|
||||||
require 'optparse'
|
|
||||||
require 'uplot/version'
|
require 'uplot/version'
|
||||||
require 'uplot/command.rb'
|
require 'uplot/command.rb'
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
require 'optparse'
|
||||||
|
require 'csv'
|
||||||
|
|
||||||
module Uplot
|
module Uplot
|
||||||
class Command
|
class Command
|
||||||
def initialize(argv)
|
def initialize(argv)
|
||||||
@ -9,6 +12,7 @@ module Uplot
|
|||||||
def opt_new
|
def opt_new
|
||||||
OptionParser.new do |opt|
|
OptionParser.new do |opt|
|
||||||
opt.on('-o', '--output', TrueClass) { |v| @output = v }
|
opt.on('-o', '--output', TrueClass) { |v| @output = v }
|
||||||
|
opt.on('-d', '--delimiter', String) { |v| @delimiter = v }
|
||||||
opt.on('-t', '--title VAL', String) { |v| @params[:title] = v }
|
opt.on('-t', '--title VAL', String) { |v| @params[:title] = v }
|
||||||
opt.on('-w', '--width VAL', Numeric) { |v| @params[:width] = v }
|
opt.on('-w', '--width VAL', Numeric) { |v| @params[:width] = v }
|
||||||
opt.on('-h', '--height VAL', Numeric) { |v| @params[:height] = v }
|
opt.on('-h', '--height VAL', Numeric) { |v| @params[:height] = v }
|
||||||
@ -46,45 +50,48 @@ module Uplot
|
|||||||
def run
|
def run
|
||||||
# Sometimes the input file does not end with a newline code.
|
# Sometimes the input file does not end with a newline code.
|
||||||
while input = Kernel.gets(nil)
|
while input = Kernel.gets(nil)
|
||||||
input_lines = input.split(/\R/)
|
input.freeze
|
||||||
|
@delimiter ||= "\t"
|
||||||
|
@headers ||= false
|
||||||
|
data = CSV.parse(input, headers: @headers, col_sep: @delimiter)
|
||||||
case @ptype
|
case @ptype
|
||||||
when 'hist', 'histogram'
|
when 'hist', 'histogram'
|
||||||
histogram(input_lines)
|
histogram(data)
|
||||||
when 'line', 'lineplot'
|
when 'line', 'lineplot'
|
||||||
line(input_lines)
|
line(data)
|
||||||
when 'lines'
|
when 'lines'
|
||||||
lines(input_lines)
|
lines(data)
|
||||||
end.render($stderr)
|
end.render($stderr)
|
||||||
|
|
||||||
print input if @output
|
print input if @output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def histogram(input_lines)
|
def histogram(data)
|
||||||
series = input_lines.map(&:to_f)
|
series = data.map { |r| r[0].to_f }
|
||||||
UnicodePlot.histogram(series, **@params.compact)
|
UnicodePlot.histogram(series, **@params.compact)
|
||||||
end
|
end
|
||||||
|
|
||||||
def line(input_lines)
|
def line(data)
|
||||||
x = []
|
data = data.transpose
|
||||||
y = []
|
if data.size == 1
|
||||||
input_lines.each_with_index do |l, i|
|
y = data[0]
|
||||||
x[i], y[i] = l.split("\t")[0..1].map(&:to_f)
|
x = (1..y.size).to_a
|
||||||
|
else
|
||||||
|
x = data[0]
|
||||||
|
y = data[1]
|
||||||
end
|
end
|
||||||
|
x = x.map(&:to_f)
|
||||||
|
y = y.map(&:to_f)
|
||||||
UnicodePlot.lineplot(x, y, **@params.compact)
|
UnicodePlot.lineplot(x, y, **@params.compact)
|
||||||
end
|
end
|
||||||
|
|
||||||
def lines(input_lines)
|
def lines(_input_lines)
|
||||||
n_cols = input_lines[0].split("\t").size
|
data = data.transpose
|
||||||
cols = Array.new(n_cols) { [] }
|
data.map { |series| series.map(&:to_f) }
|
||||||
input_lines.each_with_index do |row, i|
|
plot = UnicodePlot.lineplot(data[0], data[1], **@params.compact)
|
||||||
row.split("\t").each_with_index do |v, j|
|
2.upto(data.size - 1) do |i|
|
||||||
cols[j][i] = v.to_f
|
UnicodePlot.lineplot!(plot, data[0], data[i])
|
||||||
end
|
|
||||||
end
|
|
||||||
plot = UnicodePlot.lineplot(cols[0], cols[1], **@params.compact)
|
|
||||||
2.upto(n_cols - 1) do |i|
|
|
||||||
UnicodePlot.lineplot!(plot, cols[0], cols[i])
|
|
||||||
end
|
end
|
||||||
plot
|
plot
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user