Revert "Use value_counts instead of tally"

This commit is contained in:
kojix2
2021-05-28 09:36:08 +09:00
parent 715ec8f908
commit 97d6c3e73f
5 changed files with 53 additions and 21 deletions

View File

@@ -0,0 +1,24 @@
# frozen_string_literal: true
module YouPlot
# plotting functions.
module Backends
module Processing
module_function
def count_values(arr, tally: true)
# tally was added in Ruby 2.7
if tally && Enumerable.method_defined?(:tally)
arr.tally
else
# https://github.com/marcandre/backports
arr.each_with_object(Hash.new(0)) { |item, res| res[item] += 1 }
.tap { |h| h.default = nil }
end
.sort { |a, b| a[1] <=> b[1] }
.reverse
.transpose
end
end
end
end

View File

@@ -1,5 +1,6 @@
# frozen_string_literal: true
require_relative 'processing'
require 'unicode_plot'
module YouPlot
@@ -15,7 +16,7 @@ module YouPlot
series = data.series
# `uplot count`
if count
series = series[0].value_counts.yield_self { |h| [h.keys, h.values] }
series = Processing.count_values(series[0])
params.title = headers[0] if headers
end
if series.size == 1