mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-06 07:10:10 +08:00
A little refactoring
This commit is contained in:
parent
2fddc98118
commit
1ac39003bc
@ -168,28 +168,34 @@ module Uplot
|
|||||||
UnicodePlot.lineplot(x, y, **@params.compact)
|
UnicodePlot.lineplot(x, y, **@params.compact)
|
||||||
end
|
end
|
||||||
|
|
||||||
def xyy_plot(data, headers, call1, call2) # improve method name
|
def get_method2(_method1)
|
||||||
|
(method_name.to_s + '!').to_sym
|
||||||
|
end
|
||||||
|
|
||||||
|
def xyy_plot(data, headers, method1) # improve method name
|
||||||
|
method2 = get_method2(method1)
|
||||||
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
|
||||||
@params[:xlabel] ||= headers[0] if headers
|
@params[:xlabel] ||= headers[0] if headers
|
||||||
@params[:ylim] ||= data[1..-1].flatten.minmax # need?
|
@params[:ylim] ||= data[1..-1].flatten.minmax # need?
|
||||||
plot = UnicodePlot.public_send(call1, data[0], data[1], **@params.compact)
|
plot = UnicodePlot.public_send(method1, data[0], data[1], **@params.compact)
|
||||||
2.upto(data.size - 1) do |i|
|
2.upto(data.size - 1) do |i|
|
||||||
UnicodePlot.public_send(call2, plot, data[0], data[i], name: headers[i])
|
UnicodePlot.public_send(method2, plot, data[0], data[i], name: headers[i])
|
||||||
end
|
end
|
||||||
plot
|
plot
|
||||||
end
|
end
|
||||||
|
|
||||||
def xyxy_plot(data, headers, call1, call2) # improve method name
|
def xyxy_plot(data, headers, method1) # improve method name
|
||||||
|
method2 = get_method2(method1)
|
||||||
data.map! { |series| series.map(&:to_f) }
|
data.map! { |series| series.map(&:to_f) }
|
||||||
data = data.each_slice(2).to_a
|
data = data.each_slice(2).to_a
|
||||||
@params[:name] ||= headers[0] if headers
|
@params[:name] ||= headers[0] if headers
|
||||||
@params[:xlim] = data.map(&:first).flatten.minmax
|
@params[:xlim] = data.map(&:first).flatten.minmax
|
||||||
@params[:ylim] = data.map(&:last).flatten.minmax
|
@params[:ylim] = data.map(&:last).flatten.minmax
|
||||||
x1, y1 = data.shift
|
x1, y1 = data.shift
|
||||||
plot = UnicodePlot.public_send(call1, x1, y1, **@params.compact)
|
plot = UnicodePlot.public_send(method1, x1, y1, **@params.compact)
|
||||||
data.each_with_index do |(xi, yi), i|
|
data.each_with_index do |(xi, yi), i|
|
||||||
UnicodePlot.public_send(call2, plot, xi, yi, name: headers[(i + 1) * 2])
|
UnicodePlot.public_send(method2, plot, xi, yi, name: headers[(i + 1) * 2])
|
||||||
end
|
end
|
||||||
plot
|
plot
|
||||||
end
|
end
|
||||||
@ -197,27 +203,27 @@ module Uplot
|
|||||||
def lines(data, headers)
|
def lines(data, headers)
|
||||||
case @fmt
|
case @fmt
|
||||||
when 'xyy'
|
when 'xyy'
|
||||||
xyy_plot(data, headers, :lineplot, :lineplot!)
|
xyy_plot(data, headers, :lineplot)
|
||||||
when 'xyxy'
|
when 'xyxy'
|
||||||
xyxy_plot(data, headers, :lineplot, :lineplot!)
|
xyxy_plot(data, headers, :lineplot)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def scatter(data, headers)
|
def scatter(data, headers)
|
||||||
case @fmt
|
case @fmt
|
||||||
when 'xyy'
|
when 'xyy'
|
||||||
xyy_plot(data, headers, :scatterplot, :scatterplot!)
|
xyy_plot(data, headers, :scatterplot)
|
||||||
when 'xyxy'
|
when 'xyxy'
|
||||||
xyxy_plot(data, headers, :scatterplot, :scatterplot!)
|
xyxy_plot(data, headers, :scatterplot)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def density(data, headers)
|
def density(data, headers)
|
||||||
case @fmt
|
case @fmt
|
||||||
when 'xyy'
|
when 'xyy'
|
||||||
xyy_plot(data, headers, :densityplot, :densityplot!)
|
xyy_plot(data, headers, :densityplot)
|
||||||
when 'xyxy'
|
when 'xyxy'
|
||||||
xyxy_plot(data, headers, :densityplot, :densityplot!)
|
xyxy_plot(data, headers, :densityplot)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user