mirror of
				https://github.com/red-data-tools/YouPlot.git
				synced 2025-11-04 11:38:10 +08:00 
			
		
		
		
	Add the Processing module (tentative)
This commit is contained in:
		
							
								
								
									
										24
									
								
								lib/youplot/backends/processing.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								lib/youplot/backends/processing.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module YouPlot
 | 
			
		||||
  # plotting functions.
 | 
			
		||||
  module Backends
 | 
			
		||||
    module Processing
 | 
			
		||||
      module_function
 | 
			
		||||
 | 
			
		||||
      def count_values(arr)
 | 
			
		||||
        # tally was added in Ruby 2.7
 | 
			
		||||
        if 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
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require_relative 'processing'
 | 
			
		||||
require 'unicode_plot'
 | 
			
		||||
 | 
			
		||||
module YouPlot
 | 
			
		||||
@@ -13,7 +14,7 @@ module YouPlot
 | 
			
		||||
        series = data.series
 | 
			
		||||
        # `uplot count`
 | 
			
		||||
        if count
 | 
			
		||||
          series = Preprocessing.count_values(series[0])
 | 
			
		||||
          series = Processing.count_values(series[0])
 | 
			
		||||
          params.title = headers[0] if headers
 | 
			
		||||
        end
 | 
			
		||||
        if series.size == 1
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user