Skip to main content

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.
Parameters:
  • number (Number) - Number to format
  • decimals (Number) - Number of decimal places
  • decimalPoint (String) - Decimal separator (default: ’.’)
  • thousandsSep (String) - Thousands separator (default: ’,’)
Returns: String

Highcharts.dateFormat()

Format dates and timestamps.
Format Tokens:
  • %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
Parameters:
  • format (String) - Format string
  • timestamp (Number) - UTC timestamp
  • capitalize (Boolean) - Capitalize first letter
Returns: String

Highcharts.format()

Template string formatting.
Format Specifiers:
  • :.0f - Integer (no decimals)
  • :.2f - Two decimal places
  • :.1f - One decimal place
Parameters:
  • str (String) - Template string
  • ctx (Object) - Context object with values
  • chart (Chart) - Chart instance
Returns: String

Object Utilities

Highcharts.merge()

Deep merge objects.
Parameters:
  • ...objects - Objects to merge
Returns: Object (merged result)

Highcharts.extend()

Shallow extend (overwrite properties).
Parameters:
  • target (Object) - Target object (modified in place)
  • source (Object) - Source object
Returns: Object (target)

Highcharts.pick()

Return first defined value.
Parameters:
  • ...values - Values to check
Returns: First defined value

Highcharts.defined()

Check if value is defined.
Parameters:
  • value - Value to check
Returns: Boolean

Highcharts.isArray()

Check if value is an array.
Parameters:
  • value - Value to check
Returns: Boolean

Highcharts.isObject()

Check if value is an object.
Parameters:
  • value - Value to check
  • strict (Boolean) - If true, exclude arrays
Returns: Boolean

Highcharts.isString()

Check if value is a string.
Parameters:
  • value - Value to check
Returns: Boolean

Highcharts.isNumber()

Check if value is a number.
Parameters:
  • value - Value to check
Returns: Boolean

Array Utilities

Highcharts.find()

Find first matching element.
Parameters:
  • array (Array) - Array to search
  • callback (Function) - Test function
Returns: First matching element or undefined

Highcharts.grep()

Filter array.
Parameters:
  • array (Array) - Array to filter
  • callback (Function) - Test function
Returns: Filtered array

Highcharts.map()

Map array values.
Parameters:
  • array (Array) - Array to map
  • callback (Function) - Transform function
Returns: Mapped array

Highcharts.reduce()

Reduce array to single value.
Parameters:
  • array (Array) - Array to reduce
  • callback (Function) - Reducer function
  • initialValue - Initial accumulator value
Returns: Reduced value

Highcharts.uniqueKey()

Generate unique ID.
Returns: String (unique ID)

Math Utilities

Highcharts.arrayMin() / arrayMax()

Find minimum or maximum in array.
Parameters:
  • array (Array) - Array of numbers
Returns: Number (min or max)

Highcharts.correctFloat()

Correct floating point errors.
Parameters:
  • number (Number) - Number to correct
  • precision (Number) - Decimal precision
Returns: Number

Highcharts.relativeLength()

Convert percentage to pixels.
Parameters:
  • value (String | Number) - Value (‘50%’ or number)
  • relativeTo (Number) - Base value
Returns: Number (pixels)

Highcharts.pad()

Pad number with zeros.
Parameters:
  • number (Number) - Number to pad
  • length (Number) - Target length
Returns: String

DOM Utilities

Highcharts.createElement()

Create DOM element.
Parameters:
  • tag (String) - Element tag name
  • attributes (Object) - Element attributes
  • styles (Object) - CSS styles
  • parent (HTMLElement) - Parent element
Returns: HTMLElement

Highcharts.css()

Set CSS styles on element.
Parameters:
  • element (HTMLElement) - Target element
  • styles (Object) - CSS styles

Highcharts.offset()

Get element offset from document.
Parameters:
  • element (HTMLElement) - Element
Returns: Object with left and top

Error Handling

Highcharts.error()

Display error message. Source: ts/Core/Utilities.ts:75
Parameters:
  • code (Number | String) - Error code or message
  • stop (Boolean) - Whether to throw error
  • chart (Chart) - Chart instance
  • params (Object) - Additional parameters

Complete Example

See Also