Utils
Type module
Sources
Items from Utils can be imported/included and used via following ways.
/** * Import via main package * Access items like: am5.utils.myVariable */import * as am5 from "@amcharts/amcharts5";
/** * Import via: Utils.ts * Access items like: $utils.myVariable */import * as $utils from "@amcharts/amcharts5/Utils";/** * Include via "index.js" * Access items like: am5.utils.myVariable */Variables
colorCacheType
objectcolorContextType
CanvasRenderingContext2D|null|undefinedinertDocumentType
Document|undefinedsanitizedHTMLType
MapDefaultnew Map()
Functions
addSpacing(str:string)Returns
stringAdds a space before each uppercase letter, except the first character.
Characters without case, such as digits and spaces, count as uppercase.
alternativeColor(color:iRGB,
lightAlternative:iRGB,
darkAlternative:iRGB)Returns
iRGBReturns
lightAlternativeordarkAlternative, whichever contrasts more withcolor.brighten(rgb:$type.Optional,
percent:number)Returns
$type.OptionalReturns a color brightened by
percent, from-1to1. Unlikelighten(), it adds the same amount to every channel.capitalizeFirst(text:string)Returns
stringCapitalizes the first letter of a string.
cleanAttributes(element:Element,
name:string)Returns
booleancleanNode(parent:Node)Returns
voidcontains(a:Element,
b:Element)Returns
booleanChecks if element
acontains elementb, or isb. Looks across shadow DOM boundaries too.escapeForRgex(value:string)Returns
stringEscapes string so it can safely be used in a Regex.
get12Hours(hours:number,
base?:undefined|number)Returns
numberReturns 12-hour representation out of the 24-hour hours.
getDayFromWeek(week:number,
year:number,
weekday:number,
utc:boolean)Returns
numberReturns a year day out of the given week number.
getEventTarget(event:Event|Touch)Returns
Node|nullGets the target of the event, works for shadow DOM too.
getLightnessStep(value:number,
percent:number)Returns
numberGets lightness step.
getMonthWeek(date:Date,
utc:boolean)Returns
numberReturns a week number in the month.
getSafeResolution()Returns
number|undefinedReturns a safe canvas resolution for the current device.
Returns
1on iOS to avoid memory issues,undefinedotherwise.getShadowRoot(a:Node)Returns
ShadowRoot|nullReturns the shadow root the node is in, or
nullif it is not in one.getTimeZone(date:Date,
long:boolean,
savings:boolean,
utc:boolean,
timezone?:undefined|string)Returns
stringReturns a string name of the time zone.
getTimezoneOffset(timezone:string,
targetDate?:Date)Returns
numberReturns the UTC offset in minutes for a given timezone, positive west of UTC, as with
Date.getTimezoneOffset().getWeek(date:Date,
utc:boolean)Returns
numberReturns the ISO 8601 week number of a date: weeks start on Monday, and week 1 is the one with the year's first Thursday.
getWeekYear(date:Date,
utc:boolean)Returns
numberSince 5.3.0Returns the year that the ISO 8601 week of the date belongs to. Around January 1 it can differ from the calendar year.
getYearDay(date:Date,
utc:boolean)Returns
numberReturns the day of the year,
1being January 1.hslToHsv(hsl:iHSL)Returns
iHSVConverts HSL to HSV.
hslToRgb(color:iHSL)Returns
iRGBConverts an HSL color to RGB:
h,sandlfrom0to1in,r,gandbfrom0to255out. Alpha is not carried over.Function adapted from:
http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
hsvToHsl(hsv:iHSV)Returns
iHSLConverts HSV to HSL.
iOS()Returns
booleanReturns
trueif the current device is running iOS.isLight(color:iRGB)Returns
booleanReturns
trueif color is "light", for example to pick dark text to go on it.isLocalEvent(event:IPointerEvent,
target:Sprite)Returns
booleanSince 5.2.8Returns
trueif pointer event originated on an element within Root.isTouchEvent(ev:MouseEvent|Touch)Returns
booleanReturns
trueif the event came from touch or a pen,falseif from a mouse.lighten(rgb:$type.Optional,
percent:number)Returns
$type.OptionalReturns a color lightened by
percent, from-1to1:0.2moves each channel 20% of the way to255. A negative value darkens it toward0.onZoom(listener: () =>void)Returns
IDisposerCalls
listenerwhen the window is resized, which also happens when the browser's zoom changes.padString(value:any,
len:number,
char:string)Returns
stringPads a string with additional characters to certain length.
plainText(text:string)Returns
stringReplaces line breaks in a string with
". "and removes HTML tags.ready(f: () =>void)Returns
voidSince 5.0.2Calls
fwhen the DOM is ready, or right away if it already is.relativeToValue(percent:number|Percent|undefined|null,
full:number)Returns
numberConverts a value that can be a number or
Percentto an absolute number relative to the given full value.removeElement(el:HTMLElement)Returns
voidRemoves a DOM element.
rgbToHsl(color:iRGB)Returns
iHSLConverts an RGB color to HSL:
r,gandbfrom0to255in,h,sandlfrom0to1out. Alpha is not carried over.Function adapted from:
http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
safeURL(value:string,
attribute:string,
element:string)Returns
booleansanitizeHTML(html:string)Returns
stringSince 5.19.0Removes the parts of an HTML string that can run script or take over the page, leaving legitimate markup intact. The string is parsed by the browser into an inert document and the result is cleaned element by element:
<script>,<iframe>,<object>,<embed>,<form>,<style>,<noscript>,<math>and similar elements are removed together with their content;SVG
<animate>and<set>elements that animate a link or an event handler are removed;event handler attributes (
onerror,onload,onclick, ...) are removed, whatever separates them from the rest of the tag, and so are attributes that tie a button to a form elsewhere on the page;URLs are checked after the browser has decoded them: a URL with a protocol that can run script (
javascript:,vbscript:,data:except images, ...) is removed, and a link's becomes#;an
idornamethat would replace a property ofdocumentis removed;inline styles that smuggle in scripts via
expression(),javascript:,behavior, or-moz-bindingare removed, and so are comments.
Rootapplies it to the HTML it puts on the page (htmlandlabelHTMLsettings, HTML tooltips, modals, export menu labels, stock toolbar labels), unless itssanitizeHTMLsetting isfalse.sanitizeOnce(html:string)Returns
stringsanitizeStable(html:string)Returns
stringsaturate(rgb:$type.Optional,
saturation:number)Returns
$type.OptionalReturns
rgbwith its saturation set tosaturation, from0(grey) to1. The value replaces the color's saturation;1returnsrgbunchanged.setInteractive(target:HTMLElement,
interactive:boolean)Returns
voidDisables or enables interactivity of a DOM element.
splitString(source:string)Returns
string[]Splits the string into separate characters. Keeps RTL words non-split.
stripFormatTags(text:string)Returns
stringStrips inline formatting tags (square brackets) from the string.
stripTags(text:string)Returns
stringStrips all tags from the string.
trim(text:string)Returns
stringRemoves whitespace from both ends of a string.
trimLeft(text:string)Returns
stringRemoves whitespace from the beginning of a string.
trimRight(text:string)Returns
stringRemoves whitespace from the end of a string.
truncateTextWithEllipsis(text:string,
maxLength:number,
breakWords:boolean,
ellipsis:string)Returns
stringTruncates a string to a maximum length, appending an ellipsis.