Skip to main content
Math utilities provide mathematical functions and constants commonly used in chart calculations and geometry operations.

Constants

PI

const PI: number
PI
number
Value of π (pi), equivalent to Math.PI.

HALFPI

const HALFPI: number
HALFPI
number
Value of π/2, equivalent to Math.PI / 2.

RADIANS

const RADIANS: number
RADIANS
number
Conversion factor from degrees to radians (π/180).

DEGREES

const DEGREES: number
DEGREES
number
Conversion factor from radians to degrees (180/π).

Rounding Functions

round()

function round(value: number, precision?: number, floor?: boolean): number
Rounds the numeric value to whole number or specific precision.
value
number
required
Value to round
precision
number
Precision (number of decimal points)
floor
boolean
If value ends with 0.5 and precision is 0, floor the value instead of ceiling it
return
number
Rounded value
Example:
import { round } from "@amcharts/amcharts5";

console.log(round(3.14159)); // 3
console.log(round(3.14159, 2)); // 3.14
console.log(round(3.5, 0, true)); // 3 (floored)
console.log(round(3.5)); // 4 (rounded up)

ceil()

function ceil(value: number, precision: number): number
Ceils the numeric value to whole number or specific precision.
value
number
required
Value to ceil
precision
number
required
Precision (number of decimal points)
return
number
Ceiled value
Example:
import { ceil } from "@amcharts/amcharts5";

console.log(ceil(3.14159, 0)); // 4
console.log(ceil(3.14159, 2)); // 3.15

Trigonometric Functions

sin()

function sin(angle: number): number
Returns sine of an angle specified in degrees.
angle
number
required
Angle in degrees
return
number
Sine value
Example:
import { sin } from "@amcharts/amcharts5";

console.log(sin(90)); // 1
console.log(sin(30)); // 0.5

cos()

function cos(angle: number): number
Returns cosine of an angle specified in degrees.
angle
number
required
Angle in degrees
return
number
Cosine value
Example:
import { cos } from "@amcharts/amcharts5";

console.log(cos(0)); // 1
console.log(cos(90)); // 0

tan()

function tan(angle: number): number
Returns tangent of an angle specified in degrees.
angle
number
required
Angle in degrees
return
number
Tangent value

Angle Functions

normalizeAngle()

function normalizeAngle(value: number): number
Normalizes angle to 0-360 degree range.
value
number
required
Angle to normalize
return
number
Normalized angle (0-360)
Example:
import { normalizeAngle } from "@amcharts/amcharts5";

console.log(normalizeAngle(450)); // 90
console.log(normalizeAngle(-90)); // 270

getAngle()

function getAngle(point1: IPoint, point2?: IPoint): number
Calculates the angle between two points.
point1
IPoint
required
First point with x and y coordinates
point2
IPoint
Second point (optional)
return
number
Angle in degrees
Example:
import { getAngle } from "@amcharts/amcharts5";

const angle = getAngle({ x: 0, y: 0 }, { x: 100, y: 100 });
console.log(angle); // 45

fitAngleToRange()

function fitAngleToRange(
  value: number,
  startAngle: number,
  endAngle: number
): number
Fits angle value within a specified range.
value
number
required
Angle to fit
startAngle
number
required
Start of angle range
endAngle
number
required
End of angle range
return
number
Fitted angle

Range Functions

fitToRange()

function fitToRange(value: number, min: number, max: number): number
Constrains a value within a specified range.
value
number
required
Value to constrain
min
number
required
Minimum value
max
number
required
Maximum value
return
number
Constrained value
Example:
import { fitToRange } from "@amcharts/amcharts5";

console.log(fitToRange(150, 0, 100)); // 100
console.log(fitToRange(-10, 0, 100)); // 0
console.log(fitToRange(50, 0, 100)); // 50

closest()

function closest(values: number[], referenceValue: number): number
Returns the closest value from an array of values to the reference value.
values
number[]
required
Array of values to search
referenceValue
number
required
Reference value to compare against
return
number
Closest value from the array
Example:
import { closest } from "@amcharts/amcharts5";

const values = [10, 20, 30, 40, 50];
console.log(closest(values, 27)); // 30
console.log(closest(values, 12)); // 10

Point and Curve Functions

getArcPoint()

function getArcPoint(radius: number, arc: number): IPoint
Returns point on an arc.
radius
number
required
Radius of the arc
arc
number
required
Arc angle in degrees
return
IPoint
Point on the arc with x and y coordinates

getPointOnLine()

function getPointOnLine(
  pointA: IPoint,
  pointB: IPoint,
  position: number
): IPoint
Calculates a point on a line at a specific position.
pointA
IPoint
required
Start point of the line
pointB
IPoint
required
End point of the line
position
number
required
Position on the line (0-1, where 0 is pointA and 1 is pointB)
return
IPoint
Point on the line
Example:
import { getPointOnLine } from "@amcharts/amcharts5";

const pointA = { x: 0, y: 0 };
const pointB = { x: 100, y: 100 };
const midpoint = getPointOnLine(pointA, pointB, 0.5);
console.log(midpoint); // { x: 50, y: 50 }

getPointOnQuadraticCurve()

function getPointOnQuadraticCurve(
  pointA: IPoint,
  pointB: IPoint,
  controlPoint: IPoint,
  position: number
): IPoint
Calculates a point on a quadratic Bézier curve.
pointA
IPoint
required
Start point of the curve
pointB
IPoint
required
End point of the curve
controlPoint
IPoint
required
Control point for the curve
position
number
required
Position on the curve (0-1)
return
IPoint
Point on the curve

getCubicControlPointA()

function getCubicControlPointA(
  p0: IPoint,
  p1: IPoint,
  p2: IPoint,
  tensionX: number,
  tensionY: number
): IPoint
Calculates the first control point for a cubic curve.
p0
IPoint
required
First point
p1
IPoint
required
Second point
p2
IPoint
required
Third point
tensionX
number
required
Horizontal tension
tensionY
number
required
Vertical tension
return
IPoint
Control point

getCubicControlPointB()

function getCubicControlPointB(
  p1: IPoint,
  p2: IPoint,
  p3: IPoint,
  tensionX: number,
  tensionY: number
): IPoint
Calculates the second control point for a cubic curve.
p1
IPoint
required
First point
p2
IPoint
required
Second point
p3
IPoint
required
Third point
tensionX
number
required
Horizontal tension
tensionY
number
required
Vertical tension
return
IPoint
Control point

Bounds Functions

getArcBounds()

function getArcBounds(
  cx: number,
  cy: number,
  startAngle: number,
  endAngle: number,
  radius: number
): IBounds
Calculates the bounding box of an arc.
cx
number
required
Center X coordinate
cy
number
required
Center Y coordinate
startAngle
number
required
Start angle in degrees
endAngle
number
required
End angle in degrees
radius
number
required
Arc radius
return
IBounds
Bounding box with left, top, right, bottom properties

mergeBounds()

function mergeBounds(bounds: IBounds[]): IBounds
Merges multiple bounding boxes into a single bounding box that encompasses all.
bounds
IBounds[]
required
Array of bounding boxes to merge
return
IBounds
Merged bounding box

inBounds()

function inBounds(point: IPoint, bounds: IBounds): boolean
Checks if a point is within bounding box.
point
IPoint
required
Point to check
bounds
IBounds
required
Bounding box
return
boolean
True if point is within bounds

boundsOverlap()

function boundsOverlap(bounds1: IBounds, bounds2: IBounds): boolean
Returns true if two bounding boxes overlap.
bounds1
IBounds
required
First bounding box
bounds2
IBounds
required
Second bounding box
return
boolean
True if bounds overlap

Collision Detection

circlesOverlap()

function circlesOverlap(
  circle1: { x: number; y: number; radius: number },
  circle2: { x: number; y: number; radius: number }
): boolean
Returns true if two circles overlap.
circle1
{ x: number; y: number; radius: number }
required
First circle with center coordinates and radius
circle2
{ x: number; y: number; radius: number }
required
Second circle with center coordinates and radius
return
boolean
True if circles overlap
Example:
import { circlesOverlap } from "@amcharts/amcharts5";

const circle1 = { x: 0, y: 0, radius: 10 };
const circle2 = { x: 15, y: 0, radius: 10 };

console.log(circlesOverlap(circle1, circle2)); // true (they overlap)

Spiral Functions

spiralPoints()

function spiralPoints(
  cx: number,
  cy: number,
  radius: number,
  radiusY: number,
  innerRadius: number,
  step: number,
  radiusStep: number,
  startAngle: number,
  endAngle: number
): IPoint[]
Generates points along a spiral path.
cx
number
required
Center X coordinate
cy
number
required
Center Y coordinate
radius
number
required
Outer radius
radiusY
number
required
Vertical radius (for elliptical spirals)
innerRadius
number
required
Starting radius
step
number
required
Distance between points
radiusStep
number
required
How much radius increases per revolution
startAngle
number
required
Start angle in degrees
endAngle
number
required
End angle in degrees
return
IPoint[]
Array of points along the spiral

See Also

Build docs developers (and LLMs) love