2021-05-28 08:36:08 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module YouPlot
|
|
|
|
# plotting functions.
|
|
|
|
module Backends
|
|
|
|
module Processing
|
|
|
|
module_function
|
|
|
|
|
2021-05-31 21:50:11 +08:00
|
|
|
def count_values(arr, tally: true, reverse: false)
|
2021-05-28 08:36:08 +08:00
|
|
|
# tally was added in Ruby 2.7
|
2021-05-31 21:50:11 +08:00
|
|
|
result = \
|
|
|
|
if tally && Enumerable.method_defined?(:tally)
|
|
|
|
arr.tally
|
|
|
|
else
|
|
|
|
# value_counts Enumerable::Statistics
|
|
|
|
arr.value_counts(dropna: false)
|
2021-05-28 12:20:00 +08:00
|
|
|
end
|
2021-05-31 21:50:11 +08:00
|
|
|
|
|
|
|
# sorting
|
|
|
|
result = result.sort do |a, b|
|
|
|
|
# compare values
|
|
|
|
r = b[1] <=> a[1]
|
|
|
|
# If the values are the same, compare by name
|
2021-06-03 09:32:20 +08:00
|
|
|
r = a[0] <=> b[0] if r.zero?
|
2021-05-31 21:50:11 +08:00
|
|
|
r
|
|
|
|
end
|
|
|
|
|
|
|
|
# --reverse option
|
|
|
|
result.reverse! if reverse
|
|
|
|
|
|
|
|
# prepare for barplot
|
|
|
|
result.transpose
|
2021-05-28 08:36:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|