Cango Axes Module User Guide
Cango axes drawing extension
The Cango Axes extension module provides additional methods to the Cango library to simplify generating graph axes when plotting data with Cango.
The extension requires Cango canvas graphics library Version 11 or later. Download the source code from CangoAxes-4v00.js and the minified version at CangoAxes-4v00-min.js
Also included in the module are additional Cango methods for drawing arrows, vector text which can be treated as a Path, and HTML text which is within the DOM and so can be processed by the MathJax utility. These additional Cango methods are:
drawAxes
drawBoxAxes
drawHTMLText
drawVectorText
drawArrow
drawArrowArc
The CangoAxes module also provides two global utility functions to format numbers in engineering notation which is scientific notation but the powers of ten are restricted to the preferred; nano, micro, milli, kilo, Mega, Giga etc. values. A JavaScript implementation of very useful 'sprintf' function is also included.
Cango methods
drawAxes
Syntax:
cgo.drawAxes(xmin, xmax, ymin, ymax [, options])
Description:
Draws a set of Cartesian coordinate (X, Y) axes.
Parameters:
xmin:Number - Minimum X axis value, the value of the left end of the X axis. Measured in the X axis world coordinates.
xmax:Number - Maximum X axis value, the value of the right end of the X axis. Measured in the X axis world coordinates.
ymin:Number - Minimum Y axis value, the value of the bottom end of the Y axis for RHC coordinates, top end for SVG style systems. Measured in the Y axis world coordinates.
ymax:Number - Maximum Y axis value, the value of the top end of the Y axis for RHC coordinates, bottom end for SVG style systems. Measured in the Y axis world coordinates.
options:Object - The values for the various optional properties may be passed as key-value pairs in this object (see Optional Properties below).
Optional Properties:
Name | Type | Range |
---|---|---|
xOrigin | Number (world coordinates) | Point along X axis where Y axis will intersect. |
yOrigin | Number (world coordinates) | Point along Y axis where X axis will intersect. |
xLabel | String | Text label to be writing at end of X axis |
yLabel | String | Text label to be writing at end of Y axis |
xUnits | String | Text to be written in parentheses after the xLabel |
yUnits | String | Text to be written in parentheses after the yLabel. |
xTickInterval | Number (world coordinates) | Distance between ticks along X axis. If omitted or set to "auto", a suitable tick interval will be calculated and the X axis ticked and labeled automatically. If set to 0 then no ticks are drawn. If a value is assigned it will be rounded to nearest 1, 2, 5 sequence value. |
yTickInterval | Number (world coordinates) | Distance between ticks along Y axis. If omitted or set to "auto", a suitable tick interval will be calculated and the Y axis ticked and labeled automatically. If set to 0 then no ticks are drawn. If a value is assigned it will be rounded to nearest 1, 2, 5 sequence value. |
tickLength | Number (pixels) | Length of the (double sided) minor ticks (default is 5). Major ticks are twice as long and have labels. |
fontFamily | String | CSS font family definitions (default set by Cango default fontFamily). |
fontSize | Number (pixels) | Height of text characters (default set by Cango default fontSize). |
fontWeight | Number | 100..900 (default set by Cango default fontWeight). |
strokeColor | String | Color to be used for axes and tick marks (default set by Cango default strokeColor). |
fillColor | String | Color to be used for text labels (default set by Cango default fillColor). |
Example:
function drawAxesTest(cvsID) { var gc = new Cango(cvsID), xmin = -20000, xmax = 25000, ymin = -300, ymax = 600; gc.gridboxPadding(5); gc.setWorldCoordsRHC(xmin, ymin, xmax-xmin, ymax-ymin); gc.drawAxes(xmin, xmax, ymin, ymax, { xOrigin: -10000, yOrigin: 0, xUnits:"nm", yUnits:"ft", xLabel:"X", yLabel:"Fn" }); }
drawBoxAxes
Syntax:
cgo.drawBoxAxes(xmin, xmax, ymin, ymax [, options])
Description:
Draws a set of X, Y axes in the shape of a box.
Parameters:
xmin:Number - Minimum X axis value, the value of the left end of the X axis. Measured in the X axis world coordinates.
xmax:Number - Maximum X axis value, the value of the right end of the X axis. Measured in the X axis world coordinates.
ymin:Number - Minimum Y axis value, the value of the bottom end of the Y axis for RHC coordinates, top end for SVG style systems. Measured in the Y axis world coordinates.
ymax:Number - Maximum Y axis value, the value of the top end of the Y axis for RHC coordinates, bottom end for SVG style systems. Measured in the Y axis world coordinates.
options:Object - The values for the various optional properties may be passed as key-value pairs in this object (see Optional Properties below).
Optional Properties:
Name | Type | Range |
---|---|---|
title | String | Text of the label to be drawn at the top left of the axes box, normally used to describe the graphed data. |
xUnits | String | Text of the label to be drawn below the center of the X axis. The string is prepended with the xTickInterval value and the string "/div" is appended. |
yUnits | String | Text of the label to be drawn to the left of the center of the Y axis. The string is prepended with the yTickInterval value and string "/div" is drawn below the yUnits label. |
xTickInterval | Number (world coordinates) | Distance between ticks along X axis. If omitted the tick interval will be automatic. If a value is supplied it will be rounded to nearest 1, 2, 5 sequence value. |
yTickInterval | Number (world coordinates) | Distance between ticks along Y axis. If omitted the tick interval will default to 1/5th of the Y axis span. If a value is supplied it will be rounded to nearest 1, 2, 5 sequence value. |
fontFamily | String | CSS font family definitions (default set by Cango default fontFamily). |
fontSize | Number (pixels) | Height of text characters (default set by Cango default fontSize). |
fontWeight | Number | 100..900 (default set by Cango default fontWeight) |
strokeColor | String | Color to be used for axes and tick marks (default value "#ffffff"). |
fillColor | String | Color to be used for text labels (default value "#cccccc"). |
gridColor | String | Color to be used for the grid drawn within the box, usually semi-transparent or dashed (default value "rgba(255,255,255,0.2)"). |
Example:
function drawBoxAxesTest(cvsID) { var g = new Cango(cvsID), data = [], i; g.fillGridbox("#303a30"); g.gridboxPadding(12, 8, 3, 8); g.setWorldCoordsRHC(0, -10, 1200, 20); g.drawBoxAxes(0, 1200, -10, 10, {xUnits:"Hz", yUnits:"dB", title:"REAL"}); for (i=0; i<=200; i++) { data.push(i*6, 5*Math.sin(i/6)); } g.drawPath(data, {strokeColor:'#c0e0ff'}); }
drawHTMLText
Syntax:
cgo.drawHTMLText(str [, options]);
Description:
The drawHTMLText method allows the HTML text 'str' to be written over the canvas element integrated with the Cango graphics context. The Cango method creates a small transparent overlay DIV element for each text string and positions it using the Cango world coordinate system. Any HTML text can be used. Mathematical expressions in LaTeX syntax will be correctly parsed by MathJax.
The 'options' parameter is a JavaScript object which can hold several properties that configure how the text will be formatted as it is rendered onto the page over the canvas. Supported properties include 'x' and 'y' which set the world coordinates where the text drawing origin will be drawn. The options property 'lorg' (locate origin) can specify the spot within the text bounding box that will be drawn at the x,y coordinates. This may specify any of 9 possible locations 1=top-left, 2=top center, ... 9=lower-right corner.
NOTE: Spaces in multi-word strings will generate newline breaks, Use non-breaking spaces ( ) to keep words on the same line.
NOTE: If the text is to be further processed after rendering, such as by MathJax utility then the '\' characters should be entered into 'str' as '\\' to be correctly interpreted.
Parameters:
str:String - The text string to be written.
options:Object - The values for the various optional properties may be passed as key-value pairs in this object (see Optional Properties below).
Optional Properties:
Name | Type | Range |
---|---|---|
x | Number (world coordinates) | Point along X axis where Y axis will intersect (default x=0). |
y | Number (world coordinates) | Point along Y axis where X axis will intersect (default y=0). |
degs | Number | The number of degs a rotation applied to the text measured counter-clockwise. The center of rotation is the text drawing origin set by the 'lorg' value. The default, lorg=1, will set the top left corner of the bounding box as the drawing origin. |
lorg | Number | 1..9 (see lorg) (default lorg=1) |
fontSize | Number (pixels) | Height of text characters measured in pixels, default is the current Cango fontSize. |
fontWeight | Number or String | 'bold', 'normal' etc or a number 100..900, default is the current Cango fontWeight. |
fontFamily | String | Font family name, default is the current Cango fontFamily. |
fillColor | String | String specifying any valid CSS color to be used to render the text. |
Example:
function drawHTML(cvsID) { var gc = new Cango(cvsID), xmin = -50, xmax = 250, ymin = 0, ymax = 100, r, t, s; gc.setWorldCoordsRHC(xmin, ymin, xmax-xmin, ymax-ymin); r = "Test rotation degs:30°"; gc.drawHTMLText(r, {x:170, y:70, fontsize:18, fontWeight:400, degs:30, fillcolor:"blue", lorg:5}); t = "HTML text test multi-line and special chars too < & etc."; gc.drawHTMLText(t, {x:-25, y:80, fontSize:16, fontWeight:"bold", fillColor:"brown"}); // test the MathJax access to the HTML overlay text s = "MathJax and KaTeX work $$ v = \\sqrt{\\frac{p}{m + \\tfrac{1}{3}p} ln(\\frac{L}{c})}$$"; gc.drawHTMLText(s, {x:-25, y:40, fillColor:"red", fontSize:20, fontWeight:800}); }
drawVectorText
Syntax:
cgo.drawVectorText(str[, options]);
Description:
Converts the text 'str' to a Path object. The font used is the Hershey vector font. The character set comprises 96 printable ASCII characters and the full Greek alphabet.
Special characters within the string may be specified by Unicode escapes. These are six characters long, \u followed by exactly four hexadecimal characters. If the hex character code is only one, two or three characters long, then the code must be padded with leading zeroes. The Greek letter 'π' which has character code 240 is written '\u03C0'. The hexadecimal part of this kind of character escape is not case sensitive ie. '\u03C0' and '\u03c0' are equivalent.
The 'options' parameter is a JavaScript object which can hold several properties that configure how the text will be formatted as it is rendered onto the page over the canvas. Supported properties include 'x' and 'y' which set the world coordinates where the text drawing origin will be drawn. The options property 'lorg' (locate origin) can specify the spot within the text bounding box that will be drawn at the x,y coordinates. This may specify any of 9 possible locations 1=top-left, 2=top center, ... 9=lower-right corner.
NOTE: Since the string is drawn as a stroked Path, the color is set by the options property "strokeColor" rather than "fillColor" which sets the color of Text objects.
Parameters:
str:String - The text string to be written.
options:Object - The values for the various optional properties may be passed as key-value pairs in this object (see Optional Properties below).
Optional Properties:
Name | Type | Range |
---|---|---|
x | Number (world coordinates) | Point along X axis where Y axis will intersect (default x=0). |
y | Number (world coordinates) | Point along Y axis where X axis will intersect (default y=0). |
degs | Number | The number of degs a rotation applied to the text measured counter-clockwise. The center of rotation is the text drawing origin set by the 'lorg' value. The default, lorg=1, will set the top left corner of the bounding box as the drawing origin. |
lorg | Number | 1..9 (see lorg) (default lorg=1) |
fontSize | Number (pixels) | Height of text characters measured in pixels, default is the current Cango fontSize. |
fontWeight | Number or String | A number in the range 100..900, default is the current Cango fontWeight. |
strokeColor | String | String specifying any valid CSS color to be used to render the text. |
bgFillColor | String | String specifying any valid CSS color to be used to fill the text bounding box. |
Example:
function vectorText(cvsID) { var g = new Cango(cvsID); g.setWorldCoordsRHC(-15, -15, 40); g.drawVectorText("abcd \u0026 ABCD", {x:0, y:10, fontSize: 30}); g.drawVectorText("Greek Letters: \u03B1\u03B2\u03B3\u03B4 \u0391\u0392\u0393\u0394", {x:0, y:5, fontSize: 15}); g.drawVectorText("strokeColor:'blue', lorg=7", {x:0, y:0, strokeColor:"blue", fontSize:20, fontWeight:200, lorg:7}); g.drawVectorText("bgFillColor: lime, lorg 3", {x:0, y:0, fontSize:15, fontWeight:700, lorg:3, bgFillColor:'lime', degs: 30}); }
Vector Text character set
To generate the characters shown, insert the character (or its Unicode) into the string.
ASCII characters | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sp \u0020 |
! \u0021 |
" \u0022 |
# \u0023 |
$ \u0024 |
% \u0025 |
& \u0026 |
' \u0027 |
( \u0028 |
) \u0029 |
* \u002A |
+ \u002B |
, \u002C |
- \u002D |
. \u02E |
/ \u002F |
0 \u0030 |
1 \u0331 |
2 \u0332 |
3 \u0333 |
4 \u0334 |
5 \u0335 |
6 \u0336 |
7 \u0337 |
8 \u0338 |
9 \u0339 |
: \u033A |
; \u033B |
< \u033C |
= \u033D |
> \u033E |
? \u033F |
@ \u0040 |
A \u0041 |
B \u0042 |
C \u0043 |
D \u0044 |
E \u0045 |
F \u0046 |
G \u0047 |
H \u0048 |
I \u0049 |
J \u004A |
K \u004B |
L \u004C |
M \u004D |
N \u004E |
O \u004F |
P \u0050 |
Q \u0051 |
R \u0052 |
S \u0053 |
T \u0054 |
U \u0055 |
V \u0056 |
W \u0057 |
X \u0058 |
Y \u0059 |
Z \u005A |
[ \u005B |
\ \u005C |
] \u005D |
^ \u005E |
_ \u005F |
` \u0060 |
a \u0061 |
b \u0062 |
c \u0063 |
d \u0064 |
e \u0065 |
f \u0066 |
g \u0067 |
h \u0068 |
i \u0069 |
j \u006A |
k \u006B |
l \u006C |
m \u006D |
n \u006E |
o \u006F |
p \u0070 |
q \u0071 |
r \u0072 |
s \u0073 |
t \u0074 |
u \u0075 |
v \u0076 |
w \u0077 |
x \u0078 |
y \u0079 |
z \u007A |
{ \u007B |
| \u007C |
} \u007D |
~ \u007E |
|
To generate the symbols shown, insert the Unicode shown below the symbol into the string.
Special characters | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Α \u0391 |
Β \u0392 |
Γ \u0393 |
Δ \u0394 |
Ε \u0395 |
Ζ \u0396 |
Η \u0397 |
Θ \u0398 |
Ι \u0399 |
Κ \u039A |
Λ \u039B |
Μ \u039C |
Ν \u039D |
Ξ \u039E |
Ο \u039F |
Π \u03A0 |
Ρ \u03A1 |
Σ \u03A3 |
Τ \u03A4 |
ϒ \u03A5 |
Φ \u03A6 |
Χ \u03A7 |
Ψ \u03A8 |
Ω \u03A9 |
α \u03B1 |
β \u03B2 |
γ \u03B3 |
δ \u03B4 |
ε \u03B5 |
ζ \u03B6 |
η \u03B7 |
θ \u03B8 |
ι \u03B9 |
κ \u03BA |
λ \u03BB |
μ \u03BC |
ν \u03BD |
ξ \u03BE |
ο \u03BF |
π \u03C0 |
ρ \u03C1 |
σ \u03C3 |
τ \u03C4 |
υ \u03C5 |
φ \u03C6 |
χ \u03C7 |
ψ \u03C8 |
ω \u03C9 |
° \u00B0 |
· \u00B7 |
× \u00D7 |
÷ \u00F7 |
|
|
|
|
|
|
|
|
|
|
|
|
drawArrow
cgo.drawArrow(headX, headY [, options]);
Description:
Draws a straight arrow shape. The arrow starting at world coordinates 0, 0 unless the options parameter has properties 'x' and/or 'y' in which case the arrow will be drawn from these x,y coordinates. The arrow head will end at the point with world coordinates (headX, headY). An arrow head is draw at the end point. The arrow shaft width mat be set in either pixels or world coordinates and head size can be adjusted by specifying a head size scale factor relative to the shaft width.
The arrow is a Shape object so it can be drawn with different colored border, a drop shadow or any of the other properties supported by Shape objects.
NOTE: The arrow shape is always drawn with the 'iso' property set true to preserve the arrow shape when independent X and Y world coordinate scaling is used.
Parameters:
headX, headY:Numbers - X and Y coordinates of the tip of the arrow head. Measured in the world coordinates.
options:Object - The values for the various optional properties may be passed as key-value pairs in this object (see Options Properties below).
Optional Properties:
Property Name | Type | Description |
---|---|---|
x | Number (World Coordinates) | X coordinates of the drawing origin, start of arrow shaft (default = 0). |
y | Number (World Coordinates) | Y coordinates of the drawing origin, start of arrow shaft (default = 0). |
shaftWidth | Number (pixels) | Width of the arrow shaft. |
shaftWidthWC | Number (world coordinates) | Width of the arrow shaft, this value takes precedence over shaft width set in pixels by 'shaftWidth' property. |
headSize | Number | Relative size of the head. Length of head is ~headSize*shaftWidth (default = 4). |
fillColor | CSS Color or gradient | see Colors |
The other Shape object optional properties are also supported:
- scl
- degs
- border
- strokeColor
- lineWidthWC
- lineWidth
- dashed
- dashOffset
- shadowOffsetX
- shadowOffsetY
- shadowBlur
- shadowColor
(see Shape.setProperty method for more details).
Example:
function drawArrows(cvsID) { var gc = new Cango(cvsID), xmin = -15, xmax = 25, ymin = 0, ymax = 4; gc.gridboxPadding(10, 8, 2, 8); gc.setWorldCoordsRHC(xmin, ymin, xmax-xmin, ymax-ymin); gc.drawAxes(xmin, xmax, ymin, ymax, {xOrigin:0, yOrigin:0, strokeColor:'gray'}); // test straight arrow gc.drawArrow(20, 3, { x:5, y:1, shaftWidthWC:2, headSize:4, fillColor:'red', border:true, strokeColor:'blue', lineWidth:2}); gc.drawArrow(-10, 4, { x:0, y:1, shaftWidth: 8, fillColor:'green'}); }
drawArrowArc
Syntax:
cgo.drawArrowArc(radius, startAngle, stopAngle, options);
Description:
Draws a curved arrow whose shape is a circular arc of radius 'radius' starting at the angle 'startAngle' from the X axis and finishing at 'stopAngle'. An arrow head is drawn at the end with its tip at radius 'radius' and angle 'stopAngle'. The drawing origin will be at the center of the circular arc. If options properties 'x' and/or 'y' are specified then the center of the arc will be drawn at world coordinates x,y otherwise the center will be drawn at world coordinates 0,0.
NOTE: The arrow shape is always drawn with the 'iso' property set true to preserve the arrow shape when independent X and Y world coordinate scaling is used.
Parameters:
radius:Number - The radius of the circular arc formed by the arrow shaft. Measured in the world coordinates.
startAngle:Number - The angle at which the arrow arc starts . Measured in degrees from X axis.
stopAngle:Number - The angle at which the arrow arc stops. Measured in degrees from X axis.
options:Object - The values for the various optional properties may be passed as key-value pairs in this object (see Options Properties below).
Optional Properties:
Property Name | Type | Description |
---|---|---|
x | Number (World Coordinates) | X coordinates of the drawing origin, start of arrow shaft (default = 0). |
y | Number (World Coordinates) | Y coordinates of the drawing origin, start of arrow shaft (default = 0). |
shaftWidth | Number (pixels) | Width of the arrow shaft. |
shaftWidthWC | Number (world coordinates) | Width of the arrow shaft, this value takes precedence over shaft width set in pixels by 'shaftWidth' property. |
headSize | Number | Relative size of the head. Length of head is ~headSize*shaftWidth (default = 4). |
clockwise | Boolean | If true the arrow sweeps clockwise from startAngle to stopAngle, if false it sweeps counter-clockwise |
fillColor | CSS Color or gradient | see Colors |
The other Shape object optional properties are also supported:
- scl
- degs
- border
- strokeColor
- lineWidthWC
- lineWidth
- dashed
- dashOffset
- shadowOffsetX
- shadowOffsetY
- shadowBlur
- shadowColor
(see Shape.setProperty method for more details).
Example:
function arrowArcDemo(cvsID) { var xmin = -15, xmax = 25, ymin = -4, ymax = 4; cgo = new Cango(cvsID), cgo.clearCanvas("beige"); cgo.gridboxPadding(15, 2, 15, 2); // data area, units are % of canvas width cgo.setWorldCoordsRHC(xmin, ymin, xmax-xmin, ymax-ymin); cgo.drawAxes(xmin, xmax, ymin, ymax, { xOrigin: 0, yOrigin: 0, xLabel:"X", yLabel:"Y", fontSize: 10 }); cgo.drawArrowArc(8, 40, 280, { x:-10, y:-2, clockwise:true, shaftWidth:4, fillColor:'black'}); cgo.drawArrowArc(12, 280, 40, { clockwise:false, shaftWidth:8, fillColor:'green', shadowOffsetX:0.50, shadowOffsetY:-0.1, shadowBlur:0.5, shadowColor:'gray'}); cgo.drawArrowArc(15, 20, 156, { clockwise: false, shaftWidthWC: 1.7, fillColor: 'steelblue', border:true, strokeColor:'red'}); }
Global utilities added
toEngFixed
Syntax:
var str = toEngFixed(val, decPlaces);
Description:
Converts the value val to engineering notation, i.e. mantissa and exponent where the exponent is restricted to values that are multiples of 1000. The string returned denotes the exponent by the standard single letter values µ, m, k, M, G etc. All mantissa values are expressed in the range 1 to 1000 with the number of decimal places set by the decPlaces parameter. The result is rounded to the decPlaces decimal places with no trailing zeroes are removed.
Parameters:
val:Number - The value to be formatted.
decPlaces:Number - the number of decimal places in the mantissa.
Returns:
String - The formatted string version of val.
Examples:
toEngFixed(1200.0, 3) returns "1.200k". toEngFixed(0.012345, 2) returns "12.35m".
toEngPrec
Syntax:
var str = toEngPrec(val, sigFigs);
Description:
Converts the value val to engineering notation, i.e. mantissa and exponent where the exponent is restricted to values that are multiples of 1000. The result is then rounded to a precision of sigFigs significant figures which must be 3 or greater. The string returned denotes the exponent by the standard single letter values p, n, µ, m, k, M, G. All mantissa values are expressed in the range 1 to <1000. Trailing zeroes are removed.
Parameters:
val:Number - The value to be formatted.
sigFigs:Number - the number, greater than 3 of significant figures in the mantissa.
Returns:
String - The formatted string version of val.
Examples:
toEngPrec(1234.5678, 5) returns "1.2346k". toEngPrec(0.012345, 3) returns "12.3m".
sprintf
Syntax:
var str = sprintf(format, arg1, arg2 ...);
Description:
This JavaScript implementation of the 'sprintf' utility was originally written by Ash Searle (2007/03/printf-sprintf/) and enhanced by Kevin van Zonneveld (http://kevin.vanzonneveld.net).
The 'format' string is composed of ordinary characters (excluding %) and optional conversion specifications. Ordinary characters are copied directly to the result. Each conversion specification consists of a percent sign (%), followed by one or more of these elements, flags, width, precision and type specifier. Each conversion specifications takes an argument in turn from those following 'format' and converts it a substring which replaces the specifier in the output.
Parameters:
format:String - Which may contain characters which are returned unaltered, and conversion specifiers. The conversion specifier sub-string should be in the form:
%[flags][width][.precision]type_specifier
The optional flags are shown in the table below.
flags | description |
---|---|
- | Left-justify within the given field width; Right justification is the default (see width sub-specifier). |
+ | Forces the result to be preceded by a '+' or '-' sign. By default, only negative numbers are preceded with a - sign. |
(space) | If no sign is going to be written, a blank space is inserted before the value. |
# | Used with o, x or X specifiers. The value is preceded by a 0, 0x or 0X respectively, for values different than zero. Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written. |
0 | Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier). |
The optional width specifier is a number that says how many characters (minimum) this conversion should result in.
An optional precision specifier in the form of a period ('.') followed by an optional decimal digit string that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string.
The type specifier defines what type the argument data should be treated as. Possible types are shown in the table below.
type specifier | Output | Example |
---|---|---|
d or i | Signed decimal integer | 392 |
u | Unsigned decimal integer | 7235 |
o | Unsigned octal | 610 |
x | Unsigned hexadecimal integer | 7fa |
X | Unsigned hexadecimal integer (uppercase) | 7FA |
f or F | Decimal floating point | 392.65 |
e | Scientific notation (mantissa/exponent), lowercase | 3.9265e+2 |
E | Scientific notation (mantissa/exponent), uppercase | 3.9265E+2 |
g | Use the shortest representation: %e or %f | 392.65 |
G | Use the shortest representation: %E or %F | 392.65 |
c | Character | a |
s | String of characters | sample |
% | %% will write a single % to the string. | % |
arg1, arg2, . . . :mixed types - The parameters to be converted to strings and substituted for conversion specifiers.
Returns:
String - The formatted version of format with specifiers replaced with arguments in the order passed.
Examples:
var str = sprintf("pi = %4.2f", Math.PI); // returns "pi = 3.14".