mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-07 23:51:12 +08:00
Add barplot and scatterplot
This commit is contained in:
parent
8536abc061
commit
5f979d2e85
@ -28,13 +28,17 @@ module Uplot
|
|||||||
end
|
end
|
||||||
|
|
||||||
def parse_options(argv)
|
def parse_options(argv)
|
||||||
main_parser = opt_new
|
main_parser = opt_new
|
||||||
parsers = {}
|
parsers = {}
|
||||||
parsers['hist'] = opt_new.on('--nbins VAL', Numeric) { |v| @params[:nbins] = v }
|
parsers['hist'] = opt_new.on('--nbins VAL', Numeric) { |v| @params[:nbins] = v }
|
||||||
parsers['histogram'] = parsers['hist']
|
parsers['histogram'] = parsers['hist']
|
||||||
parsers['line'] = opt_new
|
parsers['line'] = opt_new
|
||||||
parsers['lineplot'] = parsers['line']
|
parsers['lineplot'] = parsers['line']
|
||||||
parsers['lines'] = opt_new
|
parsers['lines'] = opt_new
|
||||||
|
parsers['scatter'] = opt_new
|
||||||
|
parsers['scatterplot'] = parsers['scatter']
|
||||||
|
parsers['bar'] = opt_new
|
||||||
|
parsers['barplot'] = parsers['bar']
|
||||||
|
|
||||||
main_parser.banner = <<~MSG
|
main_parser.banner = <<~MSG
|
||||||
Usage:\tuplot <command> [options]
|
Usage:\tuplot <command> [options]
|
||||||
@ -63,6 +67,10 @@ module Uplot
|
|||||||
line(data, headers)
|
line(data, headers)
|
||||||
when 'lines'
|
when 'lines'
|
||||||
lines(data, headers)
|
lines(data, headers)
|
||||||
|
when 'scatter', 'scatterplot'
|
||||||
|
scatter(data, headers)
|
||||||
|
when 'bar', 'barplot'
|
||||||
|
barplot(data, headers)
|
||||||
end.render($stderr)
|
end.render($stderr)
|
||||||
|
|
||||||
print input if @output
|
print input if @output
|
||||||
@ -81,6 +89,11 @@ module Uplot
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def barplot(data, headers)
|
||||||
|
@params[:title] ||= headers[1] if headers
|
||||||
|
UnicodePlot.barplot(data[0], data[1].map(&:to_f), **@params)
|
||||||
|
end
|
||||||
|
|
||||||
def histogram(data, headers)
|
def histogram(data, headers)
|
||||||
@params[:title] ||= headers[0] if headers # labels?
|
@params[:title] ||= headers[0] if headers # labels?
|
||||||
series = data[0].map(&:to_f)
|
series = data[0].map(&:to_f)
|
||||||
@ -103,7 +116,7 @@ module Uplot
|
|||||||
end
|
end
|
||||||
|
|
||||||
def lines(data, headers)
|
def lines(data, headers)
|
||||||
data.map { |series| series.map(&:to_f) }
|
data.map! { |series| series.map(&:to_f) }
|
||||||
@params[:name] ||= headers[1] if headers
|
@params[:name] ||= headers[1] if headers
|
||||||
plot = UnicodePlot.lineplot(data[0], data[1], **@params.compact)
|
plot = UnicodePlot.lineplot(data[0], data[1], **@params.compact)
|
||||||
2.upto(data.size - 1) do |i|
|
2.upto(data.size - 1) do |i|
|
||||||
@ -111,5 +124,15 @@ module Uplot
|
|||||||
end
|
end
|
||||||
plot
|
plot
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def scatter(data, headers)
|
||||||
|
data.map! { |series| series.map(&:to_f) }
|
||||||
|
@params[:name] ||= headers[1] if headers
|
||||||
|
plot = UnicodePlot.scatterplot(data[0], data[1], **@params.compact)
|
||||||
|
2.upto(data.size - 1) do |i|
|
||||||
|
UnicodePlot.scatterplot!(plot, data[0], data[i], name: headers[i])
|
||||||
|
end
|
||||||
|
plot
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user