Math utilities provide mathematical functions and constants commonly used in chart calculations and geometry operations.
Constants
Value of π (pi), equivalent to Math.PI.
HALFPI
Value of π/2, equivalent to Math.PI / 2.
RADIANS
Conversion factor from degrees to radians (π/180).
DEGREES
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.
Precision (number of decimal points)
If value ends with 0.5 and precision is 0, floor the value instead of ceiling it
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.
Precision (number of decimal points)
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.
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.
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 Functions
normalizeAngle()
function normalizeAngle(value: number): number
Normalizes angle to 0-360 degree range.
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.
First point with x and y coordinates
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.
Range Functions
fitToRange()
function fitToRange(value: number, min: number, max: number): number
Constrains a value within a specified range.
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.
Array of values to search
Reference value to compare against
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.
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.
Position on the line (0-1, where 0 is pointA and 1 is pointB)
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.
Control point for the curve
Position on the curve (0-1)
getCubicControlPointA()
function getCubicControlPointA(
p0: IPoint,
p1: IPoint,
p2: IPoint,
tensionX: number,
tensionY: number
): IPoint
Calculates the first control point for a cubic curve.
getCubicControlPointB()
function getCubicControlPointB(
p1: IPoint,
p2: IPoint,
p3: IPoint,
tensionX: number,
tensionY: number
): IPoint
Calculates the second control point for a cubic curve.
Bounds Functions
getArcBounds()
function getArcBounds(
cx: number,
cy: number,
startAngle: number,
endAngle: number,
radius: number
): IBounds
Calculates the bounding box of an arc.
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.
Array of bounding boxes to merge
inBounds()
function inBounds(point: IPoint, bounds: IBounds): boolean
Checks if a point is within bounding box.
True if point is within bounds
boundsOverlap()
function boundsOverlap(bounds1: IBounds, bounds2: IBounds): boolean
Returns true if two bounding boxes 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
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.
Vertical radius (for elliptical spirals)
How much radius increases per revolution
Array of points along the spiral
See Also