mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-12-16 03:08:54 +08:00
Add reverse to count
This commit is contained in:
@@ -6,22 +6,30 @@ module YouPlot
|
||||
module Processing
|
||||
module_function
|
||||
|
||||
def count_values(arr, tally: true)
|
||||
def count_values(arr, tally: true, reverse: false)
|
||||
# tally was added in Ruby 2.7
|
||||
if tally && Enumerable.method_defined?(:tally)
|
||||
arr.tally
|
||||
else
|
||||
# value_counts Enumerable::Statistics
|
||||
arr.value_counts(dropna: false)
|
||||
end
|
||||
.sort do |a, b|
|
||||
# compare values
|
||||
r = b[1] <=> a[1]
|
||||
# If the values are the same, compare by name
|
||||
r = a[0] <=> b[0] if r == 0
|
||||
r
|
||||
result = \
|
||||
if tally && Enumerable.method_defined?(:tally)
|
||||
arr.tally
|
||||
else
|
||||
# value_counts Enumerable::Statistics
|
||||
arr.value_counts(dropna: false)
|
||||
end
|
||||
.transpose
|
||||
|
||||
# sorting
|
||||
result = result.sort do |a, b|
|
||||
# compare values
|
||||
r = b[1] <=> a[1]
|
||||
# If the values are the same, compare by name
|
||||
r = a[0] <=> b[0] if r == 0
|
||||
r
|
||||
end
|
||||
|
||||
# --reverse option
|
||||
result.reverse! if reverse
|
||||
|
||||
# prepare for barplot
|
||||
result.transpose
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,12 +14,12 @@ module YouPlot
|
||||
|
||||
module_function
|
||||
|
||||
def barplot(data, params, fmt = nil, count: false)
|
||||
def barplot(data, params, fmt = nil, count: false, reverse: false)
|
||||
headers = data.headers
|
||||
series = data.series
|
||||
# `uplot count`
|
||||
if count
|
||||
series = Processing.count_values(series[0])
|
||||
series = Processing.count_values(series[0], reverse: reverse)
|
||||
params.title = headers[0] if headers
|
||||
end
|
||||
if series.size == 1
|
||||
|
||||
@@ -146,7 +146,7 @@ module YouPlot
|
||||
when :bar, :barplot
|
||||
@backend.barplot(data, params, options[:fmt])
|
||||
when :count, :c
|
||||
@backend.barplot(data, params, count: true)
|
||||
@backend.barplot(data, params, count: true, reverse: options[:reverse])
|
||||
when :hist, :histogram
|
||||
@backend.histogram(data, params)
|
||||
when :line, :lineplot
|
||||
|
||||
@@ -11,7 +11,8 @@ module YouPlot
|
||||
:fmt,
|
||||
:progressive,
|
||||
:encoding,
|
||||
:color_names,
|
||||
:reverse, # count
|
||||
:color_names, # color
|
||||
:debug,
|
||||
keyword_init: true
|
||||
)
|
||||
|
||||
@@ -23,6 +23,7 @@ module YouPlot
|
||||
fmt: 'xyy',
|
||||
progressive: false,
|
||||
encoding: nil,
|
||||
reverse: false,
|
||||
color_names: false,
|
||||
debug: false
|
||||
)
|
||||
@@ -163,7 +164,8 @@ module YouPlot
|
||||
end
|
||||
|
||||
def sub_parser_add_canvas
|
||||
sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v|
|
||||
canvas_types = UnicodePlot::Canvas::CANVAS_CLASS_MAP.keys.join(", ")
|
||||
sub_parser.on_head('--canvas STR', String, "type of canvas", "(#{canvas_types})") do |v|
|
||||
params.canvas = v.to_sym
|
||||
end
|
||||
end
|
||||
@@ -226,6 +228,9 @@ module YouPlot
|
||||
sub_parser_add_xscale
|
||||
|
||||
when :count, :c
|
||||
sub_parser.on_head('-r', '--reverse', TrueClass, 'reverse the result of comparisons') do |v|
|
||||
options.reverse = v
|
||||
end
|
||||
sub_parser_add_symbol
|
||||
sub_parser_add_xscale
|
||||
|
||||
@@ -270,7 +275,7 @@ module YouPlot
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :colors, :color, :colours, :colour
|
||||
sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
|
||||
sub_parser.on_head('-n', '--names', TrueClass, 'show color names only') do |v|
|
||||
options[:color_names] = v
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user