A collection of utility functions for helping with data visualization
MIT License
A collection of utility functions for helping with data visualization.
Run tests
npm run test
Build the package
npm run build
If you use NPM, npm install vis-utils
. Otherwise, download the latest release.
extentLimited
extentMulti
filterInRect
filterInRectFromQuadtree
findClosestSorted
findClosestUnsorted
findEqualSorted
findEqualUnsorted
interpolateCubicBezier
interpolateCubicBezierAngle
interpolateQuadraticBezier
interpolateQuadraticBezierAngle
rectContains
rectIntersects
rotate
Compute the extent (min and max) of an array, limiting the min and the max by the specified percentiles. Percentiles are values between 0 and 1.
Parameters
Returns
(Array) The extent, limited by the min/max percentiles ([min, max]
)
Compute the extent (min and max) across an array of arrays/objects
For example:
extentMulti([[4, 3], [1, 2]], d => d);
> [1, 4]
extentMulti([{ results: [{ x: 4 }, { x: 3 }] }, { results: [{ x: 1 }, { x: 2 }] }],
d => d.x, array => array.results);
> [1, 4]
Parameters
Returns
(Array) The min and max across the arrays ([min, max]
)
Filters the elements in the passed in array to those that are contained within the specified rectangle.
Parameters
Returns
(Array) The subset of the input array that is contained within the rectangle
Filters the elements in the passed in quadtree to those that are contained within the specified rectangle.
Parameters
Returns
(Array) The subset of the input data that is contained within the rectangle
Helper function to compute distance and find the closest item Since it assumes the data is sorted, it does a binary search O(log n)
Parameters
Returns
(Any) The item in the array that is closest to value
Helper function to compute distance and find the closest item Since it assumes the data is unsorted, it does a linear scan O(n).
Parameters
Returns
(Any) The item in the array that is closest to value
Helper function to find the item that matches this value. Since it assumes the data is sorted, it does a binary search O(log n)
Parameters
Returns
(Any) The item in the array that has this value or null if not found
Helper function to find the item that matches this value. Since it assumes the data is unsorted, it does a linear scan O(n).
Parameters
Returns
(Any) The item in the array that has this value or null if not found
Given the definition of a cubic bezier: a start point, two control points, and end point, return a function that interpolates between the start point and end point following the curve.
Parameters
Returns (Function) the interpolating function that maps from 0 <= t <= 1 to a point on the curve.
Given the definition of a cubic bezier: a start point, two control points, and end point, return a function that interpolates the angle on the curve. For example, at t = 0, the interpolator returns the angle at the start point, at t = 0.5, it returns the angle midway through the curve and at t = 1 it returns the angle at the end of the curve (useful for things like arrowheads). The angles are in degrees.
Parameters
Returns (Function) the interpolating function that maps from 0 <= t <= 1 to an angle in degrees along the curve.
Given the definition of a quadratic bezier: a start point, control point, and end point, return a function that interpolates between the start point and end point following the curve.
Parameters
Returns (Function) the interpolating function that maps from 0 <= t <= 1 to a point on the curve.
Given the definition of a quadratic bezier: a start point, control point, and end point, return a function that interpolates the angle on the curve. For example, at t = 0, the interpolator returns the angle at the start point, at t = 0.5, it returns the angle midway through the curve and at t = 1 it returns the angle at the end of the curve (useful for things like arrowheads). The angles are in degrees.
Parameters
Returns (Function) the interpolating function that maps from 0 <= t <= 1 to an angle in degrees along the curve.
Determines if a point is inside a rectangle. The rectangle is
defined by two points [[rx1, ry1], [rx2, ry2]]
.
Note that it is assumed that the top Y value is less than the bottom Y value.
Parameters
Returns
(Boolean) true if the point is inside the rectangle, false otherwise
Determines if two rectangles intersect. Here a rectangle is defined by its upper left and lower right corners.
Note that it is assumed that the top Y value is less than the bottom Y value.
Parameters
Returns
(Boolean) true if the rectangles intersect, false otherwise
Rotate a point ([x, y]) around an origin ([x, y]) by theta radians
Parameters
Returns
(Number[]) The rotated point [x, y]