Utility Functions
Highcharts provides a collection of utility functions for common operations like formatting, object manipulation, and mathematical calculations. Source:ts/Core/Utilities.ts and ts/Shared/Utilities.js
Formatting Functions
Highcharts.numberFormat()
Format numbers with thousands separators and decimals.number(Number) - Number to formatdecimals(Number) - Number of decimal placesdecimalPoint(String) - Decimal separator (default: ’.’)thousandsSep(String) - Thousands separator (default: ’,’)
Highcharts.dateFormat()
Format dates and timestamps.%Y- Year (4 digits)%y- Year (2 digits)%m- Month (01-12)%b- Month name (short)%B- Month name (full)%d- Day of month (01-31)%e- Day of month (1-31)%a- Weekday (short)%A- Weekday (full)%H- Hour (00-23)%I- Hour (01-12)%M- Minute (00-59)%S- Second (00-59)%L- Milliseconds%p- AM/PM
format(String) - Format stringtimestamp(Number) - UTC timestampcapitalize(Boolean) - Capitalize first letter
Highcharts.format()
Template string formatting.:.0f- Integer (no decimals):.2f- Two decimal places:.1f- One decimal place
str(String) - Template stringctx(Object) - Context object with valueschart(Chart) - Chart instance
Object Utilities
Highcharts.merge()
Deep merge objects....objects- Objects to merge
Highcharts.extend()
Shallow extend (overwrite properties).target(Object) - Target object (modified in place)source(Object) - Source object
Highcharts.pick()
Return first defined value....values- Values to check
Highcharts.defined()
Check if value is defined.value- Value to check
Highcharts.isArray()
Check if value is an array.value- Value to check
Highcharts.isObject()
Check if value is an object.value- Value to checkstrict(Boolean) - If true, exclude arrays
Highcharts.isString()
Check if value is a string.value- Value to check
Highcharts.isNumber()
Check if value is a number.value- Value to check
Array Utilities
Highcharts.find()
Find first matching element.array(Array) - Array to searchcallback(Function) - Test function
Highcharts.grep()
Filter array.array(Array) - Array to filtercallback(Function) - Test function
Highcharts.map()
Map array values.array(Array) - Array to mapcallback(Function) - Transform function
Highcharts.reduce()
Reduce array to single value.array(Array) - Array to reducecallback(Function) - Reducer functioninitialValue- Initial accumulator value
Highcharts.uniqueKey()
Generate unique ID.Math Utilities
Highcharts.arrayMin() / arrayMax()
Find minimum or maximum in array.array(Array) - Array of numbers
Highcharts.correctFloat()
Correct floating point errors.number(Number) - Number to correctprecision(Number) - Decimal precision
Highcharts.relativeLength()
Convert percentage to pixels.value(String | Number) - Value (‘50%’ or number)relativeTo(Number) - Base value
Highcharts.pad()
Pad number with zeros.number(Number) - Number to padlength(Number) - Target length
DOM Utilities
Highcharts.createElement()
Create DOM element.tag(String) - Element tag nameattributes(Object) - Element attributesstyles(Object) - CSS stylesparent(HTMLElement) - Parent element
Highcharts.css()
Set CSS styles on element.element(HTMLElement) - Target elementstyles(Object) - CSS styles
Highcharts.offset()
Get element offset from document.element(HTMLElement) - Element
left and top
Error Handling
Highcharts.error()
Display error message. Source:ts/Core/Utilities.ts:75
code(Number | String) - Error code or messagestop(Boolean) - Whether to throw errorchart(Chart) - Chart instanceparams(Object) - Additional parameters
Complete Example
See Also
- Methods API - Chart manipulation methods
- Chart Class - Chart instance
- Events API - Event handling
- Chart Options - Configuration