@svizzle/histogram/utils

Methods

(static) areValidBins(bins) → {array}

Source:
Since:
  • 0.1.0

Returns true if bins are valid, meaning:

  • there is at least one bin object
  • they all have a range key
  • range is not null or undefined
Example
> areValidBins([])
false

> areValidBins([
	{values: [{a: 2}, {a: 6}]},
	{range: [6, 10], values: [{a: 7}, {a: 8}]},
	{range: [10, 14], values: [{a: 12}, {a: 14}]}
])
false

> areValidBins([
	{range: null, values: [{a: 2}, {a: 6}]},
	{range: [6, 10], values: [{a: 7}, {a: 8}]},
	{range: [10, 14], values: [{a: 12}, {a: 14}]}
])
false

> areValidBins([
	{range: [2, 6], values: [{a: 2}, {a: 6}]},
	{range: [6, 10], values: [{a: 7}, {a: 8}]},
	{range: [10, 14], values: [{a: 12}, {a: 14}]}
])
true
Parameters:
Name Type Description
bins array
Returns:

bins - {range, values}[]

Type
array

(static) exactAmountBins(items, amount, accessoropt, maxExtentopt) → {array}

Source:
Since:
  • 0.1.0

A binning function that returns an exact amount of bins.

Example
> const items = [1, 2, 6, 7, 8, 14, 20];
> exactAmountBins({array: items, amount: 3});
[
	{range: [1, 8], values: [1, 2, 6, 7, 8]},
	{range: [8, 15], values: [14]},
	{range: [15, 22], values: [20]}
]
> exactAmountBins({
	array,
	amount: 3,
	maxExtent: [2, 15]
})
[
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]}
]
> const items = [{a: 1}, {a: 2}, {a: 6}, {a: 7}, {a: 8}, {a: 12}, {a: 14}, {a: 20}];
> exactAmountBins({
	array,
	amount: 3,
	accessor: _.getKey('a'),
	maxExtent: [2, 14]
});
[
	{range: [2, 6], values: [{a: 2}, {a: 6}]},
	{range: [6, 10], values: [{a: 7}, {a: 8}]},
	{range: [10, 14], values: [{a: 12}, {a: 14}]}
]
Parameters:
Name Type Attributes Default Description
items array

items to be binned

amount number

desired amount of bins

accessor function <optional>
_.identity

item accessor

maxExtent Array.<number> | null <optional>
null

the desired output extent

Returns:

bins - {range, values}[]

Type
array

(static) findFirstNonEmptyBinIndex(bins) → {number}

Source:
Since:
  • 0.1.0

Return the index of the first bin with non-empty values

Example
> findFirstNonEmptyBinIndex([
	{range: [-8, -3], values: []},
	{range: [-3, 2], values: []},
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]},
	{range: [17, 22], values: []},
])
2
Parameters:
Name Type Description
bins array

{range, values}[]

Returns:
Type
number

(static) findLastNonEmptyBinIndex(bins) → {number}

Source:
Since:
  • 0.1.0

Return the index of the last bin with non-empty values

Example
> findLastNonEmptyBinIndex([
	{range: [-8, -3], values: []},
	{range: [-3, 2], values: []},
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]},
	{range: [17, 22], values: []},
])
4
Parameters:
Name Type Description
bins array

{range, values}[]

Returns:
Type
number

(static) getBinsExtent(bins) → {array}

Source:
Since:
  • 0.1.0

Return the extent of the provided bins

Example
> getBinsExtent([
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]}
])
[1, 3]
Parameters:
Name Type Description
bins array
Returns:

extent

Type
array

(static) getBinsItems(bins) → {Array.<any>}

Source:
Since:
  • 0.1.0

Return all the values in the provided bins

Example
> getBinsItems([
	{range: [2, 6], values: [{a: 2}, {a: 6}]},
	{range: [6, 10], values: [{a: 7}, {a: 8}]},
	{range: [10, 14], values: [{a: 12}, {a: 14}]}
])
[{a: 2}, {a: 6}, {a: 7}, {a: 8}, {a: 12}, {a: 14}]
Parameters:
Name Type Description
bins array
Returns:

values

Type
Array.<any>

(static) getBinsMax(bins) → {number}

Source:
Since:
  • 0.1.0

Return the length of the longest bin

Example
> getBinsMin([
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]}
])
3
Parameters:
Name Type Description
bins array
Returns:

max - length of the longest bin

Type
number

(static) getBinsMin(bins) → {number}

Source:
Since:
  • 0.1.0

Return the length of the shortest bin

Example
> getBinsMin([
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]}
])
1
Parameters:
Name Type Description
bins array
Returns:

min - length of the shortest bin

Type
number

(static) getBinsTicks(bins) → {Array.<number>}

Source:
Since:
  • 0.1.0

Return the ticks for the provided bins

Example
> getBinsTicks([
	{range: [-8, -3], values: []},
	{range: [-3, 2], values: []},
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: []},
	{range: [12, 17], values: []},
	{range: [17, 22], values: [18, 19, 20]},
	{range: [22, 27], values: [24, 25]},
	{range: [27, 32], values: []},
])
[-8, -3, 2, 7, 12, 17, 22, 27, 32]
Parameters:
Name Type Description
bins array
Returns:

ticks

Type
Array.<number>

(static) getBinsTicksExtent(bins) → {Array.<number>}

Source:
Since:
  • 0.4.0

Return the extent of all ticks for the provided bins

Example
> getBinsTicksExtent([
	{range: [-8, -3], values: []},
	{range: [-3, 2], values: []},
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: []},
	{range: [12, 17], values: []},
	{range: [17, 22], values: [18, 19, 20]},
	{range: [22, 27], values: [24, 25]},
	{range: [27, 32], values: []},
])
[-8, 32]
Parameters:
Name Type Description
bins array
Returns:

ticks extent

Type
Array.<number>

(static) getNonEmptyBinsTicks(bins) → {Array.<number>}

Source:
Since:
  • 0.1.0

Return the ticks for the provided bins using the non-empty ones

Example
> getNonEmptyBinsTicks([
	{range: [-8, -3], values: []},
	{range: [-3, 2], values: []},
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: []},
	{range: [12, 17], values: []},
	{range: [17, 22], values: [18, 19, 20]},
	{range: [22, 27], values: [24, 25]},
	{range: [27, 32], values: []},
])
[2, 7, 17, 22, 27]
Parameters:
Name Type Description
bins array
Returns:

ticks

Type
Array.<number>

(static) getTrimmedBinsStats(bins) → {object}

Source:
Since:
  • 0.1.0

Return an object containing:

  • a copy of the provided bins without the trailing bins with no values
  • start and end of the trim
Example
> getTrimmedBinsStats([
	{range: [-8, -3], values: []},
	{range: [-3, 2], values: []},
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]},
	{range: [17, 22], values: []},
])
{
	bins: [
		{range: [2, 7], values: [2, 6, 7]},
		{range: [7, 12], values: [8]},
		{range: [12, 17], values: [14]},
	],
	end: 4,
	start: 2
}
> getTrimmedBinsStats([
	{range: [2, 7], values: [2, 6, 7]},
	{range: [7, 12], values: [8]},
	{range: [12, 17], values: [14]},
	{range: [17, 22], values: []},
])
{
	bins: [
		{range: [2, 7], values: [2, 6, 7]},
		{range: [7, 12], values: [8]},
		{range: [12, 17], values: [14]},
	],
	end: 2,
	start: 0
}
Parameters:
Name Type Description
bins array
Returns:

object - {bins, end, start}

Type
object

(static) isNonEmptyBin(bin) → {boolean}

Source:
Since:
  • 0.1.0

Return true if the values property of the provided bin is not empty

Example
> isNonEmptyBin({range: [-8, -3], values: []})
false
> isNonEmptyBin({range: [2, 7], values: [2, 6, 7]})
true
Parameters:
Name Type Description
bin object

{range, values}

Returns:
Type
boolean