module Chartd::Encoder

Constants

B62

Public Class Methods

encode(dataset = [], min: nil, max: nil) click to toggle source

encode encodes a dataset to a format that chartd.co understands. It optionally takes min and max values that change the resulting chart.

# File lib/chartd/encoder.rb, line 7
def self.encode(dataset = [], min: nil, max: nil)
  return '' if dataset.empty?

  # either use custom min & max values or take them from the dataset
  min ||= dataset.min
  max ||= dataset.max

  range = dim(max, min)

  # if the range of the data is
  return B62[0] * dataset.count if range == 0

  enclen = B62.length - 1
  encoded = dataset.map do |v|
    index = (enclen * (v - min) / range).to_i

    # TODO: see if else case is even possible
    if index >= 0 && index < B62.length
      B62[index]
    else
      B62[0]
    end
  end

  encoded.join
end

Private Class Methods

dim(x, y) click to toggle source

dim returns the maximum of x-y or 0. It is used to calculate the range of the dataset.

# File lib/chartd/encoder.rb, line 36
def self.dim(x, y)
  # TODO: maybe raise an exception if max < min (x < y)
  return 0 if x < y

  x - y
end