chartjs-bezier-curve-utility
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Chart.js Bezier Curve Utility

npm code style: prettier

Motivation

Sometimes you may need to calculate the value of Y for a given X value. Chart.js uses Bezier curve under the hood to create its smooth curves. Each point you give Chart.js, it gets the control points needed to create the curve. On account of getting the control points that Chart.js calculates, we just call its internal function to retrieve the control point that Chart.js already calculates.

Why do we need to know the control points? To calculate the cubic bezier curve we need 4 points, point p0, p1, p2 and p3.

cubic bezier curve

The formula to calculate the bezier is:

cubic bezier formula

Install

To install run the command:

npm i chartjs-bezier-curve-utility

Usage

const data = [...] // [{ x0, y0 }, { x1, y1 }, { xn, yn } ...];
const xPost = 10; // X value
const tension = 0.5; // https://www.chartjs.org/docs/3.9.1/charts/line.html#dataset-properties

const y = findYPositionAtX({ data, xPos, tension }); // Y value at X

License

NPM

Readme

Keywords

Package Sidebar

Install

npm i chartjs-bezier-curve-utility

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

185 kB

Total Files

16

Last publish

Collaborators

  • salatiel