This lightweight npm package can be used to calculate a simple linear scale between two given values (minimum and maximum) and the maximal amount of ticks.
Install
Using Node.js, install the package with the following shell command:
npm install linscale
Load the package into your project:
const LinScale = require( 'linscale' );
Sample of how to use the package in your code:
let scale = new LinScale( -1.341, 6.5, 6 );
if ( scale.calculate() ) {
let min = scale.getMinimum();
// expected: -2
let max = scale.getMaximum();
// expected: 7
let ticks = scale.getTicks();
// expected: [ -2, 0, 2, 4, 6, 8 ]
let pct = scale.pct( 1.5 );
// expected: 35
}
Using JavaScript load this package by embed this file via jsDelivr:
import LinScale from 'https://cdn.jsdelivr.net/npm/linscale@1.0.1/+esm';
Remember: To use import you need to load your JavaScript file as type="module"
.
Here you can find all methods available in the LinScale
class.
Creating a new instance of LinScale
allows to pass the bounds and maximum number of ticks. This replaces methods setBounds
and setMaxTicks
. You still need to run calculate()
.
Set lower / upper bounds for the scale. Requires to run calculate()
afterwards.
Set maximum number of ticks within the scale. Requires to run calculate()
afterwards.
Center scale at the given value (default is zero). Requires to run calculate()
afterwards.
Calculates the scale range, minimum, maximum and step size.
Returns the scale step size.
Returns the scale range (from min to max).
Returns the scale minium value.
Returns the scale maximum value.
Returns an array of the scale ticks (ascending order).
Returns an array of the scale ticks (descending order).
Returns the percentage of a value within the scale from the reference point (either minimum or maximum value).
- Add
centerAt
method
- Initial release