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
Utils does not have any variables.
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.
contains(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
sanitizeHTML(html:string)Returns
stringSince 5.19.0Removes the parts of an HTML string that are practically only used for cross-site scripting (XSS), leaving legitimate markup intact:
<script>,<iframe>,<object>,<embed>and similar dangerous tags;inline event handler attributes (
onerror,onload,onclick, ...);javascript:/vbscript:(and similar) protocol URLs in attributes;inline styles that smuggle in scripts via
expression(),url(javascript:...),behavior, or-moz-binding.
Rootapplies it to the HTML it puts on the page (htmlandlabelHTMLsettings, HTML tooltips, modals, export menu labels), unless itssanitizeHTMLsetting isfalse.saturate(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.