SciGrapher.js
SciGrapher.js is a JavaScript/TypeScript library capable of producing high quality scientific oriented graphs on the browser with minimum configuration. It's based on the HTML canvas element to ensure high responsively and avoid overload the DOM with potentially hundreds of elements.
What do I mean with "scientific oriented"?
SciGrapher.js, unlike other tools like Chart.js or D3.js, is not a general purpose charting library, its goal is to accurately represent numeric data generated by mathematical equations or other (physical) sources. The resulting graphs are closer to those generated by MATLAB or Matplotlib, rather than an illustrative panel, for example.
At the moment, the library offers four chart types, including:
- Line chart, this can be used to generate dispersion graphs as well.
- Area, used to represent the area between two curves.
- Heatmap, to represent scalar fields.
- Vector field, to represent two-dimensional vector fields.
Table of Contents
Installation
To install just type
npm install scigrapher
In a terminal on your project folder.
Usage
Each graph is controlled by a single object created by calling the graph2D function.
graph2D(element)
graph2D(element, options)
Where
-
elementis a div element. -
optionsis an object containing the options to change the default behavior and appearance of the graph.
Note: The options object has a lot of properties and complex structure, its use is intended for copying or generate graphs from another graphs. All its properties are breake down in the
graph2Dmethods, is best to use those instead.
To start, call the graph2D function and assign the object generated to a variable, use its methods to change the appearance and behavior to your needs and finally call the draw method to render the final result.
The methods can be chained.
HTML:
<body>
<div id="my-graph"></div>
</body>
JS:
const element = document.querrySelector("#my-graph");
const graph = graph2D(element).axisPoition("center").draw();
Result:
Almost every method is overcharged, so you can set or get the properties, for example calling graph.axisType() will return the current axis type used in the graph, and calling graph.axisType("polar") will set the graph axis type to polar and return a reference to the same graph object, so more methods can be chained.
Additionally, almost every method accepts a callback function as a second parameter. This callback will be executed right after the graph properties are updated, but before is render to the screen.
The callback function can accept one optional parameter that represents the graph object from which the method is call upon.
Note: The execution of the optional callback function is unique to that method call, so in order to execute that function more than ones it needs to be passed to the method on each call.
Warning: The callback function will only run if there is a change in the graph properties. For example, if you try to change the axis
typetopolarbut the axis already have that property value the function will skyp all calculations, including the callback, and continue with the normal flow.
To add data to the graph, you need to call the addDataset method specifying the type of the data you intend to create, the return value can be stored in a variable to later use.
addDataset(datatype)
addDataset(datatype, options)
Where:
-
datatypeis one of the data types available : "linechart", "area", "heatmap" or "vectorfield". -
optionsis an object containing the options to change the default behavior and appearance of the dataset.
Note. The options object has a lot of properties and complex structure, its use is intended for copying or generate datasets from another datasets. All its properties are breake down in the corresponding dataset methods, is best to use those instead.
The resulting dataset is bound to the graph, so in order to render the result, you must call the draw method of the graph after the dataset creation or any modification made to it.
Example:
const element = document.querrySelector("#my-graph");
const graph = graph2D(element)
.axisDomain({
x : {start : -8, end : 8},
y : {start : -1.2, end : 1.2}
})
.containerSize({width : 800, height : 300});
const data = graph.addDataset("linechart")
.dataX(linspace(-8, 8, 100))
.dataY((dataset)=>{
return dataset.dataX().map(x=>Math.cos(x));
});
graph.draw();
Result:
The details on how to manipulate the datasets will be discussed in its corresponding section.
Graph Methods
Background
Color:
This method returns or changes the graph background color.
Method:
backgroundColor()
backgroundColor(option)
backgroundColor(option, callback)
Where:
-
optionis a string representing the background color in the format #rrggbb or any of the standard color names. -
callbackis a function that is run after the color is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- A string representing the graph background color if no arguments are passed.
- A reference to the graph object from which the method is called upon.
Default value:
- The default value for the background color is
"#ffffff"(white)
Opacity:
This method returns or changes the graph background opacity.
Method:
backgroundOpacity()
backgroundOpacity(option)
backgroundOpacity(option, callback)
Where:
-
optionis a number between 0 and 1 representing the graph background opacity. -
callbackis a function that is run after the opacity is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- A number between 0 and 1 representing the graph background opacity if no arguments are passed.
- A reference to the graph object from which the method is called upon.
Default value:
- The default value of the background opacity is
1
Axis
Position:
This method returns or change the current axis position.
Method:
axisPosition()
axisPosition(option)
axisPosition(option, callback)
Where:
-
optionis a string representing the axis position, the available options are:- "center"
- "bottom-left"
- "bottom-right"
- "top-left"
- "top-right"
-
callbackis a function that is run after the axis position is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- A string representing the current axis position if no arguments are passed.
- A reference to the graph object from which the method is called upon.
Default value:
- The default value for the axis position is
"center".
Type:
This method returns or changes the current axis type.
Method:
axisType()
axisType(option)
axisType(option, callback)
Where:
-
optionis a string representing the axis type, the available options are:- "rectangular": Linear scales on both axis.
- "polar": Effectively the same as
"rectangular"but with the grid in polar representation. - "x-log": Base 10 logarithmic scale on the
xaxis and linear scale on theyaxis. - "y-log": Base 10 logarithmic scale on the
yaxis and linear scale on thexaxis. - "log-log" Base 10 logarithmic scale on both axis.
-
callbackis a function that is run after the axis type is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Warning: An axis cannot have a logarithmic scale set unless its domain is strictly positive or negative, in other words the axis domain must contain only positive values or only negative values not include zero.
Returns:
- A string representing the current axis type if no arguments are passed.
- A reference to the graph object from which the method is called upon.
Default value:
- The default value for the axis type is
"rectangular".
Domain:
This method returns or sets the current domain of each axis. It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisDomain()
axisDomain(options)
axisDomain(options, callback)
Where:
-
optionsis an object representing the axis domain, this object has the properties:-
x: An object containing the propertiesstartandend.- The property
startis the value at the left most side of thexaxis. - The property
endis the value at the right most side of thexaxis.
- The property
-
y: An object containing the propertiesstartandend.- The property
startis the value at the bottom of theyaxis. - The property
endis the value at the top of theyaxis.
- The property
-
-
callbackis a function that is run after the axis domain is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Why the
endvalue of theyaxis is at the top and not the bottom? : Because of the convention of having the vertical axis values increase from bottom to top, this is a convention to follow for other similar properties.
On that note, it is not necessary to have the
startvalue be smaller than theendvalue. In that case, the axis "direction" will be reverted.
Returns:
- An object representing the current axis domain if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the axis domain are as follows:
{
x : {
start : -5,
end : 5
},
y : {
start : -5,
end : 5
}
}
Color:
This method lets you set or get the current colors of each axis base, ticks and text independently.
The options object has some properties that change the colors in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisColor()
axisColor(options)
axisColor(options, callback)
Where:
-
optionsis an object that represent the color of each axis base, ticks and text. This object has the following properties:-
axis: a string representing the color of both whole axis. -
xAxis: a string representing the color of the wholexaxis. -
yAxis: a string representing the color of the wholeyaxis. -
base: an object containing the following properties:-
x: a string representing the color of thexaxisbase. -
y: a string representing the color of theyaxisbase.
-
-
tick: an object containing the following properties:-
x: a string representing the color of thexaxisticks. -
y: a string representing the color of theyaxisticks.
-
-
text: an object containing the following properties:-
x: a string representing the color of thexaxistext. -
y: a string representing the color of theyaxistext.
-
-
-
callbackis a function that is run after the axis color is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Return:
- An object representing the current axis colors if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the axis color are as follows:
("#000000" represents the color black)
{
base : {
x : "#000000",
y : "#000000"
},
tick : {
x : "#000000",
y : "#000000"
},
text : {
x : "#000000",
y : "#000000"
}
}
Opacity:
This method lets you set or get the current opacities of each axis base, ticks and text independently.
The options object has some properties that change the opacities in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisOpacity()
axisOpacity(options)
axisOpacity(options, callback)
Where:
-
optionsis an object that represent the opacity of each axis base, ticks and text. This object has the following properties:-
axis: a number between 0 and 1 representing the opacity of both whole axis. -
xAxis: a number between 0 and 1 representing the opacity of the wholexaxis. -
yAxis: a number between 0 and 1 representing the opacity of the wholeyaxis. -
base: an object containing the following properties:-
x: a number between 0 and 1 representing the opacity of thexaxisbase. -
y: a number between 0 and 1 representing the opacity of theyaxisbase.
-
-
tick: an object containing the following properties:-
x: a number between 0 and 1 representing the opacity of thexaxisticks. -
y: a number between 0 and 1 representing the opacity of theyaxisticks.
-
-
text: an object containing the following properties:-
x: a number between 0 and 1 representing the opacity of thexaxistext. -
y: a number between 0 and 1 representing the opacity of theyaxistext.
-
-
-
callbackis a function that is run after the axis opacity is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Return:
- An object representing the current axis opacities if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the axis opacity are as follows:
{
base : {
x : 1,
y : 1
},
tick : {
x : 1,
y : 1
},
text : {
x : 1,
y : 1
}
}
Units:
This property lets you set or get the units of each axis. The units appear on each tick at the right of the number.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisUnits()
axisUnits(options)
axisUnits(options, callback)
Where:
-
optionsis an object containing the following properties:-
x: a string representig thexaxis units: -
y: a string representig theyaxis units:
-
-
callbackis a function that is run after the axis units are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- An object representing the current axis units if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Dafault Values:
The default values for the axis units are as follow:
{
x : "",
y : ""
}
Base:
This metod lets you set or get the properties of each axis base.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisBase()
axisBase(options)
axisBase(options, callback)
Where:
-
optionsis an object containing the following properties:-
x: an object containing the following properties:-
color: a string representing thexaxis base color. -
opacity: a number between 0 and 1 representing thexaxis base opacity. -
width: an integer positive number representing the line width of thexaxis base.
-
-
y: an object containing the following properties:-
color: a string representing theyaxis base color. -
opacity: a number between 0 and 1 representing theyaxis base opacity. -
width: an integer positive number representing the line width of theyaxis base.
-
-
-
callbackis a function that is run after the properties of the axis base are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Returns:
- An object representing the current properties of the axis base if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The default values of the properties of the axis base are as follow:
("#000000" represents the color black)
{
x : {
color : "#000000",
opacity : 1,
width : 1
},
y : {
color : "#000000",
opacity : 1,
width : 1
},
}
Ticks:
This method lets you set or get the properties of each axis ticks.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisTicks()
axisTicks(options)
axisTicks(options, callback)
Where:
-
options: is an object with the followin properties:-
x: an object with the following properties:-
color: a string representing the color of thexaxis ticks. -
opacity: a number between 0 and 1 representing the opacity of thexaxis ticks. -
width: a positive integer representing the line width of thexaxis ticks. -
size: a number representing the size in pixels of thexaxis ticks. -
ticks: this property controls how many ticks will be displayed, the value can be one of the following options:- The string
"auto"to let thegraphobject decide where to put the ticks. - A number representing the amount of ticks in the axis. The ticks will be equally spaced and will contain the start and end of the axis.
- An array of numbers representing the location of the ticks to be displayed.
- The string
-
minSpacing: If theticksproperty is set to"auto"controls the minimum space in pixels that the ticks can have between them, otherwise it has no effect.
-
-
y: an object with the following properties:-
color: a string representing the color of theyaxis ticks. -
opacity: a number between 0 and 1 representing the opacity of theyaxis ticks. -
width: a positive integer representing the line width of theyaxis ticks. -
size: a number representing the size in pixels of theyaxis ticks. -
ticks: this property controls how many ticks will be displayed, the value can be one of the following options:- The string
"auto"to let thegraphobject decide where to put the ticks. - A number representing the amount of ticks in the axis. The ticks will be equally spaced and will contain the start and end of the axis.
- An array of numbers representing the location of the ticks to be displayed.
- The string
-
minSpacing: If theticksproperty is set to"auto"controls the minimum space in pixels that the ticks can have between them, otherwise it has no effect.
-
-
-
callbackis a function that is run after the properties of the axis ticks are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: If the axis position is set to
centerthe tick size will be double of thesizeproperty. This is because in that position, the tick runs on both sides of the axis base.
Returns:
- An object representing the current properties of the axis ticks if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the axis ticks are:
("#000000" represents the color black)
{
x : {
color : "#000000",
opacity : 1,
width : 1,
size : 5,
ticks : "auto",
minSpacing : 45
},
y : {
color : "#000000",
opacity : 1,
width : 1,
size : 5,
ticks : "auto",
minSpacing : 45
},
}
Text:
This method lets you set or get the text properties of each axis.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisText()
axisText(options)
axisText(options, callback)
Where:
-
options: is an object containing the following properties-
x: and object with the following properties:-
color: a string representing the color of the text in thexaxis. -
opacity: a number between 0 and 1 representing the opacity of the text in thexaxis. -
font: a string representing the font of the text in thexaxis. -
size: a string representing the size of the text in thexaxis. -
specifier: a string representing the font specifier of the text in thexaxis.
-
-
y: and object with the following properties:-
color: a string representing the color of the text in theyaxis. -
opacity: a number between 0 and 1 representing the opacity of the text in theyaxis. -
font: a string representing the font of the text in theyaxis. -
size: a string representing the size of the text in theyaxis. -
specifier: a string representing the font specifier of the text in theyaxis.
-
-
-
callbackis a function that is run after the properties of the axis text are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
- An object representing the current properties of the axis text if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Defaul values:
The defaul values for the properties of the axis text are as follows:
("#000000" represents the color black)
{
x : {
color : "#000000",
opacity : 1,
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "10px",
specifier : ""
},
y : {
color : "#000000",
opacity : 1,
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "10px",
specifier : ""
}
}
Dynamic
This method lets you set or get the dynamic properties of each axis.
This method only is relevant if the axis position is set to center.
In this position, the axis text is located below the x axis and at the left of the y axis, and the two axis intersects at the coordinate (0,0).
When that coordinate is no longer inside the graph area, the axis shift to remain visible and the text change positions relative to the axis base.
Those two are the behaviors that this method can change.
For example, in the next illustration, the axis shift when the start value of the y axis domain is bigger than zero.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisDynamic()
axisDynamic(dynamic)
axisDynamic(dynamic, callback)
Where:
-
dynamicis an object containing the following properties:-
x: and object with the following properties:-
dynamic: a boolean that determines whether thexaxis text changes position relative to the base. -
contained: a boolean that determines whether thexaxis shifts to remain visible.
-
-
y: and object with the following properties:-
dynamic: a boolean that determines whether theyaxis text changes position relative to the base. -
contained: a boolean that determines whether theyaxis shifts to remain visible.
-
-
-
callbackis a function that is run after the properties of the axis dynamics are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- An object representing the current properties of the axis dynamics if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Dafault values:
The default values for the properties of the axis dynamics are:
{
x : {
dynamic : true,
contained : true
},
y : {
dynamic : true,
contained : true
}
}
Overlap
This method lets you set or get the overlap properties of each axis.
This method only is relevant if the axis position is set to center.
In this position, the axis text may overlap with each other or with the axis itself.
To enhance visibility, a little area around each text is clear, so neither the base nor ticks will render on top of the text.
This method lets you modify that behavior.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
axisOverlap()
axisOverlap(overlap)
axisOverlap(overlap, callback)
Where:
-
overlapis an object cotaining the following properties:-
priority: a string with the values"x"or"y". -
x: a boolean representing whether a small area arround the text of thexaxis should be cleared. -
y: a boolean representing whether a small area arround the text of theyaxis should be cleared.
-
-
callbackis a function that is run after the properties of the axis overlap are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: In the case that the text of one axis overlaps with the text of the other, the one render on top will be determined by the value of the
priorityproperty.
Returns:
- An object representing the current properties of the axis overlap if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Dafault values:
The default values for the properties of the axis overlap are:
{
priority : "x",
x : true,
y : true
}
Secondary Enable:
This method lets you enable the secondary set of axis.
Each graph have the capability of defining two sets of axis, the primary x and y axis are always defined while the secondary x and y can be defined independently.
The properties of the secondary axis can be manipulated in the same way the primaries can, but the axis must be defined first and the axis position must NOT be center.
Each dataset can decide what pair of axis use.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Note:
centerpositioned axis cannot have secondary axis defined.
Method:
secondaryAxisEnable()
secondaryAxisEnable(options)
secondaryAxisEnable(options, callback)
Where:
-
optionsis an object containing the following properties:-
x: a boolean that determines whether the secondaryxaxis must be enable. -
y: a boolean that determines whether the secondaryyaxis must be enable.
-
-
callbackis a function that is run after the properties of the secondary axis enable are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The secondary axis will not be enabled if the axis
positionis set tocenter.
Returns:
- An object representing the current properties of the secondary axis enable if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The default values of the secondary axis enable are as follows:
{
x : false,
y : false
}
Seocndary Domain:
This method lets you set or get the properties of the secondary axis domain.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisDomain()
secondaryAxisDomain(options)
secondaryAxisDomain(options, callback)
Where:
-
optionsis an object representing the axis domain, this object has the properties:-
x: An object containing the propertiesstartandend.- The property
startis the value at the left most side of thexaxis. - The property
endis the value at the right most side of thexaxis.
- The property
-
y: An object containing the propertiesstartandend.- The property
startis the value at the bottom of theyaxis. - The property
endis the value at the top of theyaxis.
- The property
-
-
callbackis a function that is run after the axis domain is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Why the
endvalue of theyaxis is at the top and not the bottom? : Because of the convention of having the vertical axis values increase from bottom to top, this is a convention to follow for other similar properties.
On that note, it is not necessary to have the
startvalue be smaller than theendvalue. In that case, the axis "direction" will be reverted.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Returns:
- An object representing the current secondary axis domain if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the secondary axis domain are as follows:
{
x : {
start : -5,
end : 5
},
y : {
start : -5,
end : 5
}
}
Secondary Color:
This method lets you set or get the current colors of each secondary axis base, ticks and text independently.
The options object has some properties that change the colors in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisColor()
secondaryAxisColor(options)
secondaryAxisColor(options, callback)
Where:
-
optionsis an object that represent the color of each axis base, ticks and text. This object has the following properties:-
axis: a string representing the color of both whole axis. -
xAxis: a string representing the color of the wholexaxis. -
yAxis: a string representing the color of the wholeyaxis. -
base: an object containing the following properties:-
x: a string representing the color of thexaxisbase. -
y: a string representing the color of theyaxisbase.
-
-
tick: an object containing the following properties:-
x: a string representing the color of thexaxisticks. -
y: a string representing the color of theyaxisticks.
-
-
text: an object containing the following properties:-
x: a string representing the color of thexaxistext. -
y: a string representing the color of theyaxistext.
-
-
-
callbackis a function that is run after the axis color is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Return:
- An object representing the current secondary axis colors if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the secondary axis color are as follows:
("#000000" represents the color black)
{
base : {
x : "#000000",
y : "#000000"
},
tick : {
x : "#000000",
y : "#000000"
},
text : {
x : "#000000",
y : "#000000"
}
}
Secondary Opacity:
This method lets you set or get the current opacities of each secondary axis base, ticks and text independently.
The options object has some properties that change the opacities in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisOpacity()
secondaryAxisOpacity(options)
secondaryAxisOpacity(options, callback)
Where:
-
optionsis an object that represent the opacity of each axis base, ticks and text. This object has the following properties:-
axis: a number between 0 and 1 representing the opacity of both whole axis. -
xAxis: a number between 0 and 1 representing the opacity of the wholexaxis. -
yAxis: a number between 0 and 1 representing the opacity of the wholeyaxis. -
base: an object containing the following properties:-
x: a number between 0 and 1 representing the opacity of thexaxisbase. -
y: a number between 0 and 1 representing the opacity of theyaxisbase.
-
-
tick: an object containing the following properties:-
x: a number between 0 and 1 representing the opacity of thexaxisticks. -
y: a number between 0 and 1 representing the opacity of theyaxisticks.
-
-
text: an object containing the following properties:-
x: a number between 0 and 1 representing the opacity of thexaxistext. -
y: a number between 0 and 1 representing the opacity of theyaxistext.
-
-
-
callbackis a function that is run after the axis opacity is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Return:
- An object representing the current secondary axis opacities if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the secondary axis opacity are as follows:
{
base : {
x : 1,
y : 1
},
tick : {
x : 1,
y : 1
},
text : {
x : 1,
y : 1
}
}
Secondary Units:
This property lets you set or get the units of each secondary axis. The units appear on each tick at the right of the number.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisUnits()
secondaryAxisUnits(options)
secondaryAxisUnits(options, callback)
Where:
-
optionsis an object containing the following properties:-
x: a string representig thexaxis units: -
y: a string representig theyaxis units:
-
-
callbackis a function that is run after the axis units are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Returns:
- An object representing the current secondary axis units if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Dafault Values:
The default values for the secondary axis units are as follow:
{
x : "",
y : ""
}
Secondary Base:
This metod lets you set or get the properties of each secondary axis base.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisBase()
secondaryAxisBase(options)
secondaryAxisBase(options, callback)
Where:
-
optionsis an object containing the following properties:-
x: an object containing the following properties:-
color: a string representing thexaxis base color. -
opacity: a number between 0 and 1 representing thexaxis base opacity. -
width: an integer positive number representing the line width of thexaxis base.
-
-
y: an object containing the following properties:-
color: a string representing theyaxis base color. -
opacity: a number between 0 and 1 representing theyaxis base opacity. -
width: an integer positive number representing the line width of theyaxis base.
-
-
-
callbackis a function that is run after the properties of the axis base are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Returns:
- An object representing the current properties of the secondary axis base if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The default values of the properties of the secondary axis base are as follow:
("#000000" represents the color black)
{
x : {
color : "#000000",
opacity : 1,
width : 1
},
y : {
color : "#000000",
opacity : 1,
width : 1
},
}
Secondary Ticks:
This method lets you set or get the properties of each secondary axis ticks.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisTicks()
secondaryAxisTicks(options)
secondaryAxisTicks(options, callback)
Where:
-
options: is an object with the followin properties:-
x: an object with the following properties:-
color: a string representing the color of thexaxis ticks. -
opacity: a number between 0 and 1 representing the opacity of thexaxis ticks. -
width: a positive integer representing the line width of thexaxis ticks. -
size: a number representing the size in pixels of thexaxis ticks. -
ticks: this property controls how many ticks will be displayed, the value can be one of the following options:- The string
"auto"to let thegraphobject decide where to put the ticks. - A number representing the amount of ticks in the axis. The ticks will be equally spaced and will contain the start and end of the axis.
- An array of numbers representing the location of the ticks to be displayed.
- The string
-
minSpacing: If theticksproperty is set to"auto"controls the minimum space in pixels that the ticks can have between them, otherwise it has no effect.
-
-
y: an object with the following properties:-
color: a string representing the color of theyaxis ticks. -
opacity: a number between 0 and 1 representing the opacity of theyaxis ticks. -
width: a positive integer representing the line width of theyaxis ticks. -
size: a number representing the size in pixels of theyaxis ticks. -
ticks: this property controls how many ticks will be displayed, the value can be one of the following options:- The string
"auto"to let thegraphobject decide where to put the ticks. - A number representing the amount of ticks in the axis. The ticks will be equally spaced and will contain the start and end of the axis.
- An array of numbers representing the location of the ticks to be displayed.
- The string
-
minSpacing: If theticksproperty is set to"auto"controls the minimum space in pixels that the ticks can have between them, otherwise it has no effect.
-
-
-
callbackis a function that is run after the properties of the axis ticks are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Returns:
- An object representing the current properties of the secondary axis ticks if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the secondary axis ticks are:
("#000000" represents the color black)
{
x : {
color : "#000000",
opacity : 1,
width : 1,
size : 5,
ticks : "auto",
minSpacing : 45
},
y : {
color : "#000000",
opacity : 1,
width : 1,
size : 5,
ticks : "auto",
minSpacing : 45
},
}
Secondary Text:
This method lets you set or get the text properties of each secondary axis.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisText()
secondaryAxisText(options)
secondaryAxisText(options, callback)
Where:
-
options: is an object containing the following properties-
x: and object with the following properties:-
color: a string representing the color of the text in thexaxis. -
opacity: a number between 0 and 1 representing the opacity of the text in thexaxis. -
font: a string representing the font of the text in thexaxis. -
size: a string representing the size of the text in thexaxis. -
specifier: a string representing the font specifier of the text in thexaxis.
-
-
y: and object with the following properties:-
color: a string representing the color of the text in theyaxis. -
opacity: a number between 0 and 1 representing the opacity of the text in theyaxis. -
font: a string representing the font of the text in theyaxis. -
size: a string representing the size of the text in theyaxis. -
specifier: a string representing the font specifier of the text in theyaxis.
-
-
-
callbackis a function that is run after the properties of the axis ticks are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
- An object representing the current properties of the secondary axis text if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Defaul values:
The defaul values for the properties of the secondary axis text are as follows:
("#000000" represents the color black)
{
x : {
color : "#000000",
opacity : 1,
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "10px",
specifier : ""
},
y : {
color : "#000000",
opacity : 1,
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "10px",
specifier : ""
}
}
Secondary Type:
This method let you set or get the properties of each secondary axis type.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryAxisType()
secondaryAxisType(opions)
secondaryAxisType(opions, callback)
Where:
-
optionsis an object containing the following properties:-
x: a string with the value "rectangular" or "log" representing the type of the secondary axisx. -
y: a string with the value "rectangular" or "log" representing the type of the secondary axisy.
-
-
callbackis a function that is run after the properties of the axis type are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Warning: An axis cannot have a logarithmic scale set unless its domain is strictly positive or negative, in other words the axis domain must contain only positive values or only negative values not include zero.
Note: To change any secondary axis property, the corresponding axis must be enabled first.
Warning: If any of the axis is not defined yet, the correspondign property will return the value
undefined.
Returns:
- An object representing the current properties of the secondary axis type if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The defaul values for the properties of the secondary axis type are as follows:
{
x : "rectangular",
y : "rectangular"
}
Grid
The graph can define the use of grids to improve the data reading. There are two types of grids:
-
Primary: The primary grid runs along the axis ticks. -
Secondary: The secondary grid runs in between the axis ticks.
Each grid have an x and y component, and runs perpendicular to its corresponding axis.
For example, the x component of the primary grid is composed of parallel vertical lines extending the x axis ticks.
Note: If the axis type is set to
polarthe grid behavior changes.
In the case that the axis position is set to polar, then the behavior of the grids are as follows:
-
Primary:- The
xcomponent of the primary grid is composed of concentric circles around the origin with radii equal to each tick in thexdirection. - The
ycomponent of the primary grid is composed of rays coming out of the origin with equal angle variation.
- The
-
Secondary:- The
xcomponent of the secondary grid is composed of concentric circles arround the origin with raii equal to values in between the ticks in thexdirection. - The
ycomponent of the secondary grid is composed of rays coming out of the origin, with equal angle variation in between each ray of theycomponent of the primary grid.
- The
The x component of the grids is meant to improve the reading of the radial component of the data, while the y component improves the reading of the angle data.
Color:
This method let you set or get the color properties of each grid.
The options object has some properties that change the colors in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
gridColor()
gridColor(options)
gridColor(options, callback)
Where:
-
optionsis an object containing the following properties:-
grid: a string representing the color of both whole grids. -
primary:a string representing the color of the wholeprimarygrid. -
secondary:a string representing the color of the wholesecondarygrid. -
x: an object with the following properties:-
primary: a string representing the color of thexcomponent of theprimarygrid. -
secondary: a string representing the color of thexcomponent of thesecondarygrid.
-
-
y: an object with the following properties:-
primary: a string representing the color of theycomponent of theprimarygrid. -
secondary: a string representing the color of theycomponent of thesecondarygrid.
-
-
-
callbackis a function that is run after the color properties of the grids are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Returns:
- An object representing the current color properties of the grids if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The defaul values for the color properties of the grids are as follows:
("#000000" represents the color black)
{
x : {
primary : "#000000",
secondary : "#000000",
},
y : {
primary : "#000000",
secondary : "#000000",
}
}
Opacity:
This method let you set or get the opacity properties of each grid.
The options object has some properties that change the opacities in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
gridOpacity()
gridOpacity(options)
gridOpacity(options, callback)
Where:
-
optionsis an object containing the following properties:-
grid: a number representing the opacity of both whole grids. -
primary:a number representing the opacity of the wholeprimarygrid. -
secondary:a number representing the opacity of the wholesecondarygrid. -
x: an object with the following properties:-
primary: a number representing the opacity of thexcomponent of theprimarygrid. -
secondary: a number representing the opacity of thexcomponent of thesecondarygrid.
-
-
y: an object with the following properties:-
primary: a number representing the opacity of theycomponent of theprimarygrid. -
secondary: a number representing the opacity of theycomponent of thesecondarygrid.
-
-
-
callbackis a function that is run after the opacity properties of the grids are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The value of the opacity must be a number between 0 and 1.
Returns:
- An object representing the current opacity properties of the grids if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The defaul values for the color properties of the grids are as follows:
{
x : {
primary : 0.2,
secondary : 0.1,
},
y : {
primary : 0.2,
secondary : 0.1,
}
}
Style:
This method let you set or get the style properties of each grid.
The options object has some properties that change the styles in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
gridStyle()
gridStyle(options)
gridStyle(options, callback)
Where:
-
optionsis an object containing the following properties:-
grid: a string representing the style of both whole grids. -
primary:a string representing the style of the wholeprimarygrid. -
secondary:a string representing the style of the wholesecondarygrid. -
x: an object with the following properties:-
primary: a string representing the style of thexcomponent of theprimarygrid. -
secondary: a string representing the style of thexcomponent of thesecondarygrid.
-
-
y: an object with the following properties:-
primary: a string representing the style of theycomponent of theprimarygrid. -
secondary: a string representing the style of theycomponent of thesecondarygrid.
-
-
-
callbackis a function that is run after the style properties of the grids are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- An object representing the current style properties of the grids if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The defaul values for the style properties of the grids are as follows:
{
x : {
primary : "solid",
secondary : "dot",
},
y : {
primary : "solid",
secondary : "dot",
}
}
Width:
This method let you set or get the width properties of each grid.
The options object has some properties that change the widths in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
gridWidth()
gridWidth(options)
gridWidth(options, callback)
Where:
-
optionsis an object containing the following properties:-
grid: a number representing the width of both whole grids. -
primary:a number representing the width of the wholeprimarygrid. -
secondary:a number representing the width of the wholesecondarygrid. -
x: an object with the following properties:-
primary: a number representing the width of thexcomponent of theprimarygrid. -
secondary: a number representing the width of thexcomponent of thesecondarygrid.
-
-
y: an object with the following properties:-
primary: a number representing the width of theycomponent of theprimarygrid. -
secondary: a number representing the width of theycomponent of thesecondarygrid.
-
-
-
callbackis a function that is run after the width properties of the grids are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The value of the width must be a positive integer.
Returns:
- An object representing the current width properties of the grids if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Values:
The defaul values for the width properties of the grids are as follows:
{
x : {
primary : 1,
secondary : 1,
},
y : {
primary : 1,
secondary : 1,
}
}
Primary:
This method lets you set or get all properties of the primary grid.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
primaryGrid()
primaryGrid(options)
primaryGrid(options, callback)
Where:
-
optionsis an object with the following properties:-
grid: an object with the properties:-
enable: a boolean that determines whether the whole gid should be enabled. -
color: a string representing the color of the whole grid. -
opacity: a number representing the opacity of the whole grid. -
width: a number representing the line width of the whole grid. -
style: a string representing the line style of the whole grid.
-
-
x: an object with the properties:-
enable: a boolean that determines whether the gridxcomponent should be enabled. -
color: a string representing the color of the gridxcomponent. -
opacity: a number representing the opacity of the gridxcomponent. -
width: a number representing the line width of the gridxcomponent. -
style: a string representing the line style of the gridxcomponent.
-
-
y: an object with the properties:-
enable: a boolean that determines whether the gridycomponent should be enabled. -
color: a string representing the color of the gridycomponent. -
opacity: a number representing the opacity of the gridycomponent. -
width: a number representing the line width of the gridycomponent. -
style: a string representing the line style of the gridycomponent.
-
-
-
callbackis a function that is run after the grid properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The value of the opacity must be a number between 0 and 1.
Note: The value of the width must be a positive integer.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- An object representing the current properties of the primary grid if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Defaul Values:
The defaul values for the properties of the primary grid are as follows:
("#000000" represents the color black)
{
x : {
enable : true,
color : "#000000",
opacity : 0.2,
width : 1,
style : "solid"
},
y : {
enable : true,
color : "#000000",
opacity : 0.2,
width : 1,
style : "solid"
}
}
Secondary:
This method lets you set or get all properties of the secondary grid.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
secondaryGrid()
secondaryGrid(options)
secondaryGrid(options, callback)
Where:
-
optionsis an object with the following properties:-
grid: an object with the properties:-
enable: a boolean that determines whether the whole gid should be enabled. -
color: a string representing the color of the whole grid. -
opacity: a number representing the opacity of the whole grid. -
width: a number representing the line width of the whole grid. -
style: a string representing the line style of the whole grid. -
density: this property controls how many parts each tick is subdivided into, it can have one of these values:- The string
"auto"to compute the optimal number of divisions automatically. - A number representing how many divisions to make on each tick.
- The string
-
minSpacing: a number that represent the minimum length in pixels that a subdivision can have. This property is only relevant when thedensityvalue is set to"auto". -
maxDensity: a number that represents the maximum number of subdivision that a tick can have. This property is only relevant when thedensityvalue is set to"auto".
-
-
x: an object with the properties:-
enable: a boolean that determines whether the gridxcomponent should be enabled. -
color: a string representing the color of the gridxcomponent. -
opacity: a number representing the opacity of the gridxcomponent. -
width: a number representing the line width of the gridxcomponent. -
style: a string representing the line style of the gridxcomponent. -
density: this property controls how many parts each tick is subdivided into, it can have one of these values:- The string
"auto"to compute the optimal number of divisions automatically. - A number representing how many divisions to make on each tick.
- The string
-
minSpacing: a number that represent the minimum length in pixels that a subdivision can have. This property is only relevant when thedensityvalue is set to"auto". -
maxDensity: a number that represents the maximum number of subdivision that a tick can have. This property is only relevant when thedensityvalue is set to"auto".
-
-
y: an object with the properties:-
enable: a boolean that determines whether the gridycomponent should be enabled. -
color: a string representing the color of the gridycomponent. -
opacity: a number representing the opacity of the gridycomponent. -
width: a number representing the line width of the gridycomponent. -
style: a string representing the line style of the gridycomponent. -
density: this property controls how many parts each tick is subdivided into, it can have one of these values:- The string
"auto"to compute the optimal number of divisions automatically. - A number representing how many divisions to make on each tick.
- The string
-
minSpacing: a number that represent the minimum length in pixels that a subdivision can have. This property is only relevant when thedensityvalue is set to"auto". -
maxDensity: a number that represents the maximum number of subdivision that a tick can have. This property is only relevant when thedensityvalue is set to"auto".
-
-
-
callbackis a function that is run after the grid properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The value of the opacity must be a number between 0 and 1.
Note: The value of the width must be a positive integer.
Note: The properties
minSpacingandmaxDensityare only relevant when thedensityproperty is set to"auto", and its values must be a positive integer.
Note: The values of the property
density, if not set to"auto", must be a positive integer.
If the graph is using polar as the axis type, the value of density represents the amount of subdivision of the angle grid, in this case if is set to "auto" there will be always four subdivisions.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- An object representing the current properties of the secondary grid if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Defaul Values:
The defaul values for the properties of the secondary grid are as follows:
("#000000" represents the color black)
{
x : {
enable : true,
color : "#000000",
opacity : 0.1,
width : 1,
style : "dot",
density : "auto",
minSpacing : 20,
maxDensity : 5
},
y : {
enable : true,
color : "#000000",
opacity : 0.1,
width : 1,
style : "dot",
density : "auto",
minSpacing : 20,
maxDensity : 5
}
}
Polar:
This method lets you set or get the amount of divisions in the main grid if the axis type is set to center.
This only affects the y component of the primary grid. The angle divisions always start from the x axis.
Method:
polarGrid()
polarGrid(option)
polarGrid(option, callback)
Where:
-
optionis a number representing the number of main divisions in the angle grid if no arguments are pass. -
callbackis a function that is run after the grid polar property is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note:
optionmust be a positive integer, this property is only relevant when the axistypeis set topolar.
Returns:
- A number representing the number of main divisions in the angle grid if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default Value:
- The defaul values for this property is:
16.
Labels
Currently, the available labels are:
-
Title: Located at the top of the graph. -
Subtitle: Located below theTitle. -
x Label: Located beside thexaxis. -
y Label: Located beside theyaxis. -
Secondary x Label: Located beside thexsecondary axis. -
Secondary y Label: Located beside theysecondary axis.
The Title and Subtitle can be set at any time, while the x Label and y Label can be set only when the axis position is NOT set to center.
The secondary labels will render only if the corresponding axis is enabled.
The graph client area will change to acomodate the labels.
Title:
This method lets you set or get the properties of the title.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
title()
title(options)
title(options, callback)
Where:
-
optionsis an object containing the following properties:-
enable: a boolean that determines whether thetitlemust be enabled. -
text: a string representing the text to show as atitle. -
font: a string representing the font of thetitle. -
size: a string representing the size of thetitle. -
specifier: a string representing the font specifier of thetitle. -
color: a string representing the color of thetitle. -
opacity: a number between 0 and 1 representing the opacity of thetitle. -
filled: a boolean that determines whether the text must be filled or just outlined. -
position: this property controls the relative position of thetitle, can be one of the following values:-
"start". -
"center". -
"end".
-
-
-
callbackis a function that is run after the title properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
-
undefinedif thetitleis not defined yet and no arguments are pass. - An object representing the properties of the
titleif no arguments are pass. - A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the title are as follows:
("#000000" represents the color black)
{
enable : true,
text : "",
font : "Perpetua, Baskerville, Big Caslon, Palatino Linotype, Palatino, serif",
size : "25px",
specifier : "",
color : "#000000",
opacity : 1,
filled : true,
position : "start"
}
Subtitle:
This method lets you set or get the properties of the subtitle.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
subtitle()
subtitle(options)
subtitle(options, callback)
Where:
-
optionsis an object containing the following properties:-
enable: a boolean that determines whether thesubtitlemust be enabled. -
text: a string representing the text to show as asubtitle. -
font: a string representing the font of thesubtitle. -
size: a string representing the size of thesubtitle. -
specifier: a string representing the font specifier of thesubtitle. -
color: a string representing the color of thesubtitle. -
opacity: a number between 0 and 1 representing the opacity of thesubtitle. -
filled: a boolean that determines whether the text must be filled or just outlined. -
position: this property controls the relative position of thesubtitle, can be one of the following values:-
"start". -
"center". -
"end".
-
-
-
callbackis a function that is run after the subtitle properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
-
undefinedif thesubtitleis not defined yet and no arguments are pass. - An object representing the properties of the
subtitleif no arguments are pass. - A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the subtitle are as follows:
("#000000" represents the color black)
{
enable : true,
text : "",
font : "Perpetua, Baskerville, Big Caslon, Palatino Linotype, Palatino, serif",
size : "15px",
specifier : "",
color : "#000000",
opacity : 1,
filled : true,
position : "start"
}
x Label:
This method lets you set or get the properties of the x label.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
xLabel()
xLabel(options)
xLabel(options, callback)
Where:
-
optionsis an object containing the following properties:-
enable: a boolean that determines whether thex labelmust be enabled. -
text: a string representing the text to show as ax label. -
font: a string representing the font of thex label. -
size: a string representing the size of thex label. -
specifier: a string representing the font specifier of thex label. -
color: a string representing the color of thex label. -
opacity: a number between 0 and 1 representing the opacity of thex label. -
filled: a boolean that determines whether the text must be filled or just outlined. -
position: this property controls the relative position of thex label, can be one of the following values:-
"start". -
"center". -
"end".
-
-
-
callbackis a function that is run after the x label properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
positionis not set tocenter.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
-
undefinedif thex labelis not defined yet and no arguments are pass. - An object representing the properties of the
x labelif no arguments are pass. - A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the x label are as follows:
("#000000" represents the color black)
{
enable : true,
text : "",
font : "Perpetua, Baskerville, Big Caslon, Palatino Linotype, Palatino, serif",
size : "15px",
specifier : "",
color : "#000000",
opacity : 1,
filled : true,
position : "center"
}
y Label:
This method lets you set or get the properties of the y label.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
yLabel()
yLabel(options)
yLabel(options, callback)
Where:
-
optionsis an object containing the following properties:-
enable: a boolean that determines whether they labelmust be enabled. -
text: a string representing the text to show as ay label. -
font: a string representing the font of they label. -
size: a string representing the size of they label. -
specifier: a string representing the font specifier of they label. -
color: a string representing the color of they label. -
opacity: a number between 0 and 1 representing the opacity of they label. -
filled: a boolean that determines whether the text must be filled or just outlined. -
position: this property controls the relative position of they label, can be one of the following values:-
"start". -
"center". -
"end".
-
-
-
callbackis a function that is run after the y label properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
positionis not set tocenter.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
-
undefinedif they labelis not defined yet and no arguments are pass. - An object representing the properties of the
y labelif no arguments are pass. - A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the y label are as follows:
("#000000" represents the color black)
{
enable : true,
text : "",
font : "Perpetua, Baskerville, Big Caslon, Palatino Linotype, Palatino, serif",
size : "15px",
specifier : "",
color : "#000000",
opacity : 1,
filled : true,
position : "center"
}
Secondary x Label:
This method lets you set or get the properties of the secondary x label.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
xLabelSecondary()
xLabelSecondary(options)
xLabelSecondary(options, callback)
Where:
-
optionsis an object containing the following properties:-
enable: a boolean that determines whether thesecondary x labelmust be enabled. -
text: a string representing the text to show as asecondary x label. -
font: a string representing the font of thesecondary x label. -
size: a string representing the size of thesecondary x label. -
specifier: a string representing the font specifier of thesecondary x label. -
color: a string representing the color of thesecondary x label. -
opacity: a number between 0 and 1 representing the opacity of thesecondary x label. -
filled: a boolean that determines whether the text must be filled or just outlined. -
position: this property controls the relative position of thesecondary x label, can be one of the following values:-
"start". -
"center". -
"end".
-
-
-
callbackis a function that is run after the secondary x label properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
positionis not set tocenter.
Note: This label will not render if the secodary
xaxis is not enabled.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
-
undefinedif thesecondary x labelis not defined yet and no arguments are pass. - An object representing the properties of the
secondary x labelif no arguments are pass. - A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the secondary x label are as follows:
("#000000" represents the color black)
{
enable : true,
text : "",
font : "Perpetua, Baskerville, Big Caslon, Palatino Linotype, Palatino, serif",
size : "15px",
specifier : "",
color : "#000000",
opacity : 1,
filled : true,
position : "center"
}
Secondary y Label:
This method lets you set or get the properties of the secondary y label.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
yLabelSecondary()
yLabelSecondary(options)
yLabelSecondary(options, callback)
Where:
-
optionsis an object containing the following properties:-
enable: a boolean that determines whether thesecondary y labelmust be enabled. -
text: a string representing the text to show as asecondary y label. -
font: a string representing the font of thesecondary y label. -
size: a string representing the size of thesecondary y label. -
specifier: a string representing the font specifier of thesecondary y label. -
color: a string representing the color of thesecondary y label. -
opacity: a number between 0 and 1 representing the opacity of thesecondary y label. -
filled: a boolean that determines whether the text must be filled or just outlined. -
position: this property controls the relative position of thesecondary y label, can be one of the following values:-
"start". -
"center". -
"end".
-
-
-
callbackis a function that is run after the secondary y label properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
positionis not set tocenter.
Note: This label will not render if the secodary
yaxis is not enabled.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
Returns:
-
undefinedif thesecondary y labelis not defined yet and no arguments are pass. - An object representing the properties of the
secondary y labelif no arguments are pass. - A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the secondary y label are as follows:
("#000000" represents the color black)
{
enable : true,
text : "",
font : "Perpetua, Baskerville, Big Caslon, Palatino Linotype, Palatino, serif",
size : "15px",
specifier : "",
color : "#000000",
opacity : 1,
filled : true,
position : "center"
}
Utilities
The graph object implements some general utility methods to help you to move, zoom and resize the graph and all its components.
Three of the four methods set event listeners on the container div element, all these event perform changes on the graph properties.
As usual, all this methods accept an optional callback if you need aditional functionality.
The available utility methods are:
-
pointerMove: Lets you pan the graph with a pointer based event (mouse and touch). -
pointerZoom: Lets you zoom the graph with a pointer based event (mouse and touch). -
containerResize: Lets you adjust the graph size with a resize observer event. -
aspectRatio: Sets the aspect ratio of an axis with respect of other.
Note: The utilities
pointerMoveandpointerZoomare incompatibles when used with a non touch device.
It is importan to note that the utilities pointerMove and pointerZoom use both pointer based events, so if a mouse fire the event and are both enabled, only pointerMove will take effect. For this to work you must enable and disable the utilities manually.
This is not a problem when usin a movile (touch based) device. This is because diferent gestures are used each one, touch and grab for pointerMove and pinch for pointerZoom.
Pointer Move:
This method allows you to move the graph using the pointer (mouse or touch).
This method creates a new instance of the event listener, discartign any previous one.
It is only necessary to define the values that you want to customize, the rest will be set to its default.
Method:
pointerMove()
pointerMove(options)
Where:
-
optionsis an object with the following properties:-
enable: a boolean tha determines whether the utility must be enabled. -
delay: a number representing the delay in milliseconds of the throttle implementation. -
pointerCapture: a boolean that determines whether the pointer event should be capture. -
defaultCursor: a string that represent the default cursor. -
hoverCursor: a string that represent the cursor when its hovering the active area. -
moveCursor: a string that represent the cursor when is performing the move. -
primaryAxis: a boolean that determines if the move affects theprimaryaxis. -
secondaryAxis: a boolean that determines if the move affects thesecondaryaxis. -
callback: a function that runs afther the graph new properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
-
Note: The properties of the graph after the move is made are computed in a throttle function to avoid overload de browser. The
delayproperty affects how often that calculations are made.
Note: The value of the
defaultCursor,hoverCursor,moveCursorcan be any of the standar cursor names.
Note: When disabled, the cursor will be set to
defaultCursor.
Note: Unlike other methods, the
callbackfunction is contained inside theoptionsobject.
Returns:
- A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the pointerMove method are as follow:
{
enable : true,
delay : 12,
pointerCapture: true,
defaultCursor : "default",
hoverCursor : "grab",
moveCursor : "grabbing",
primaryAxis : true,
secondaryAxis : true,
callback : undefined
}
Pointer Zoom:
This method allows you to zoom the graph using the pointer (mouse or touch).
This method creates a new instance of the event listener, discartign any previous one.
The zoom applied is calculated using the primary axis.
It is only necessary to define the values that you want to customize, the rest will be set to its default.
Method:
pointerZoom()
pointerZoom(options)
Where:
-
optionsis an object with the following properties:-
enable: a boolean tha determines whether the utility must be enabled. -
delay: a number representing the delay in milliseconds of the throttle implementation. -
pointerCapture: a boolean that determines whether the pointer event should be capture. -
defaultCursor: a string that represent the default cursor. -
hoverCursor: a string that represent the cursor when its hovering the active area. -
moveCursor: a string that represent the cursor when is performing the move. -
primaryAxis: a boolean that determines if the move affects theprimaryaxis. -
secondaryAxis: a boolean that determines if the move affects thesecondaryaxis. -
anchor: this property controls the point that will remain constant during the zoom. It can be one of the following options:- The string
"center"to fix the mid point of both axis. - The string
"pointer" to fix the point that the pointer was at the start of the action. - An array of two numbers
[x, y]representing the coordinate of the fixed point.
- The string
-
type: this property control the type of zoom gesture. It can be one of the following options:- The string
"area"to make zoom drawing a rectangle. - The string
"drag"to make zoom dragging the pointer.
- The string
-
strength: a number representing the strength of the zoom. This property is only relevant when thetypeproperty is set todragor when using touch devices. -
rect: an object representing the rectangle draw when usin thedragzoomtype:-
background: a string representing the background color of the rectangle. -
opacity: a number representing the opacity of the rectangle. -
borderColor: a string representing the color of the rectangle border. -
borderOpacity: a number representing the opacity of the rectangle border. -
borderWidth: a number representing the line width of the rectangle border.
-
-
callback: a function that runs afther the graph new properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
-
Note: The properties of the graph after the zoom is made are computed in a throttle function to avoid overload de browser. The
delayproperty affects how often that calculations are made.
Note: The value of the
defaultCursor,hoverCursor,moveCursorcan be any of the standar cursor names.
Note: When disabled, the cursor will be set to
defaultCursor.
Note: Unlike other methods, the
callbackfunction is contained inside theoptionsobject.
When using the zoom with the type set to `"area" you can draw a rectangle, and the area of that rectangle will be represented by the axis after the mouse is released.
Also, while drawing the area, you can hold the shift key to move the origin of the rectangle.
In this mode, only zoom-in is available.
When using the zoom whith the type set to "drag" you must click and drag to perform the zoom. Dragging to the right relative to the initial click will zoom-in, while gragging to the left will perform a zoom-out.
In this mode, the zoom conserve the axis aspect ratio.
If you are using a touch device, the zoom will perform using the pinch gesture regardless the value of the property type.
Returns:
- A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the pointerZoom method are as follow:
{
enable : true,
delay : 12,
pointerCapture: true,
defaultCursor : "default",
hoverCursor : "zoom-in",
moveCursor : "zoom-in",
primaryAxis : true,
secondaryAxis : true,
callback : undefined,
anchor : "pointer",
type : "area",
strength : 5,
rect : {
background : "#0075FF",
opacity : 0.05,
borderColor : "#0057bd",
borderOpacity : 0.5,
borderWidth : 1
}
}
Container Resize:
This method allows the resize of the graph whether the container div element changes size.
This method creates a new instance of the event listener, discartign any previous one.
It is only necessary to define the values that you want to customize, the rest will be set to its default.
Method:
constainerResize()
constainerResize(options)
Where:
-
optionsis an object containing the following properties:-
enable: a boolean tha determines whether the utility must be enabled. -
delay: a number representing the delay in milliseconds of the throttle implementation. -
preserveAspectRatio: a boolean that determines whether the axis aspect ratio must be conserved after the resize. -
anchor: this property controls the point in the primary axis that will remain constant during the resize. It can be one of the following options:- The string
"center"to fix the mid point of both axis. - An array of two numbers
[x, y]representing the coordinate in the primary axis of the fixed point.
- The string
-
secondaryAnchor: this property controls the point in the secondary axis that will remain constant during the resize. It can be one of the following options:- The string
"center"to fix the mid point of both axis. - An array of two numbers
[x, y]representing the coordinate in the secondary axis of the fixed point.
- The string
-
primaryAxis: a boolean that determines if the move affects theprimaryaxis. -
secondaryAxis: a boolean that determines if the move affects thesecondaryaxis. -
callback: a function that runs afther the graph new properties are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
-
preserveAspectRatio |
Result |
|---|---|
true |
![]() |
false |
![]() |
Returns:
- A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the containerResize method are as follow:
{
enable : true,
delay : 12,
preserveAspectRatio : true,
anchor : "center",
secondaryAnchor : "center",
primaryAxis : true,
secondaryAxis : true,
callback : undefined
}
Aspect Ratio:
This method allows you to set the aspect ratio of one axis relative to another.
This utility is not event-based, so its effects will only be applied once.
It is only necessary to define the values that you want to customize, the rest will be set to its default.
Method:
aspectRatio()
aspectRatio(options)
aspectRatio(options, callback)
Where:
-
optionsis an object containing the following properties:-
ratio: a number representing the aspect ratio to set. -
source: a string representing the source axis. It can be one of the folloging options:- The string
"x"to set thexaxis as thesource. - The string
"y"to set theyaxis as thesource. - The string
"xSecondary"to set the secondaryxaxis as thesource. - The string
"ySecondary"to set the secondaryyaxis as thesource.
- The string
-
target: a string representing the target axis. It can be one of the folloging options:- The string
"x"to set thexaxis as thetarget. - The string
"y"to set theyaxis as thetarget. - The string
"xSecondary"to set the secondaryxaxis as thetarget. - The string
"ySecondary"to set the secondaryyaxis as thetarget.
- The string
-
anchor: this property controls the point on thetargetaxis that will remain unchaged afther the transformation. It can be one of the following values:- The string
"start"to fix thestartvalue of thetargetaxis domain. - The string
"end"to fix theendvalue of thetargetaxis domain. - A number to fix that coordinate of the
targetaxis domain.
- The string
-
-
callbackis a function that is run after the aspect ratio is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
The aspect ratio represents the amount of pixels that each unit of the target axis takes with respect the source axis.
For example, if the ratio property is set to 2, each unit of the target axis will occupy twice as much pixels that one unit of the source axis.
Note: If the
sourceortargetproperties are set to either secondary axis, and that axis is not enabled, the method will take no effect.
Note: The the
sourceortargetproperties can both be set to the same axis, in that case only that axis will change.
Returns:
- A reference to the graph object from which the method is called upon.
Default Values:
The default values for the properties of the aspectRatio method are as follow:
{
ratio : 1,
source : "x",
target : "y",
anchor : "start"
}
Graph Properties
Margin:
This method lets you set or get the margins of the graph.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
margin()
margin(options)
margin(options, callback)
Where:
-
optionsis an object containing the following properties:-
x: an object whith the properties:-
start: this property controls the margin at the left most side of thexaxis. It can have one of the following values:- The string
"auto"to compute the margin automatically. - A number representing the margin in pixels.
- The string
-
end: this property controls the margin at the right most side of thexaxis. It can have one of the following values:- The string
"auto"to compute the margin automatically. - A number representing the margin in pixels.
- The string
-
-
y: an object whith the properties:-
start: this property controls the margin at the bottom of theyaxis. It can have one of the following values:- The string
"auto"to compute the margin automatically. - A number representing the margin in pixels.
- The string
-
end: this property controls the margin at the top of theyaxis. It can have one of the following values:- The string
"auto"to compute the margin automatically. - A number representing the margin in pixels.
- The string
-
-
-
callbackis a function that is run after the margins are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Why the
endvalue of theycomponent is at the top and not the bottom? : Because of the convention of having the vertical axis values increase from bottom to top, this is a convention to follow for other similar properties.
Note: Any margin can have negative values, the behavior is as expected.
Return:
- An object representing the current margins if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Note: Even when the value of any margin is set to
"auto", this method will always return an object with numeric values.
Default values:
The default values for the margins are as follows:
{
x : {
start : "auto",
end : "auto",
},
y : {
start : "auto",
end : "auto",
},
}
Border:
This method lets you set or get the border properties of the graph.
The options object has some properties that change the borders in a generalized way, and those changes cascaded towards the more specific properties. The more specific the property is, the more priority takes.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
border()
border(options)
border(options, callback)
Where:
-
optionsis an object containing the following properties:-
border: an object with the properties:-
enable: a boolean that determines whether all borders should be enabled. -
color: a string that represent the color of all borders. -
opacity: a number between 0 and 1 that represent the opacity of all borders. -
width: a number that represent the line width of all borders. -
style: a string that represents the line style of all borders.
-
-
x: a object with the following properties:-
start: an object that represent the properties of the left border:-
enable: a boolean that determines whether the border should be enabled. -
color: a string that represent the color of the border. -
opacity: a number between 0 and 1 that represent the opacity of the border. -
width: a number that represent the line width of the border. -
style: a string that represents the line style of the border.
-
-
end: an object that represent the properties of the right border:-
enable: a boolean that determines whether the border should be enabled. -
color: a string that represent the color of the border. -
opacity: a number between 0 and 1 that represent the opacity of the border. -
width: a number that represent the line width of the border. -
style: a string that represents the line style of the border.
-
-
-
y: a object with the following properties:-
start: an object that represent the properties of the bottom border:-
enable: a boolean that determines whether the border should be enabled. -
color: a string that represent the color of the border. -
opacity: a number between 0 and 1 that represent the opacity of the border. -
width: a number that represent the line width of the border. -
style: a string that represents the line style of the border.
-
-
end: an object that represent the properties of the top border:-
enable: a boolean that determines whether the border should be enabled. -
color: a string that represent the color of the border. -
opacity: a number between 0 and 1 that represent the opacity of the border. -
width: a number that represent the line width of the border. -
style: a string that represents the line style of the border.
-
-
-
-
callbackis a function that is run after the borders are set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The color can be in the format "#rrggbb" or be any of the standard color names. Note: The value of the width must be a positive integer.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- An object representing the current border properties if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the borders are as follows:
{
x : {
start : {
enable : false,
color : "#000000",
opacity : 1,
width : 1,
style : "solid"
},
end : {
enable : false,
color : "#000000",
opacity : 1,
width : 1,
style : "solid"
}
},
y : {
start : {
enable : false,
color : "#000000",
opacity : 1,
width : 1,
style : "solid"
},
end : {
enable : false,
color : "#000000",
opacity : 1,
width : 1,
style : "solid"
}
}
}
Container Size:
This method lets you set or get the size of the container div element.
The graph change sizes accordingly, maintaining the values of the axis domain. In other words, this method do not conserve the axis aspect ratio.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
containerSize()
containerSize(options)
containerSize(options, callback)
Where:
-
optionsis an object containing the following properties:-
width: a number representing the width of the container in pixels. -
height: a number representing the height of the container in pixels.
-
-
callbackis a function that is run after the container size is set but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note: The
widthandheightproperties mus be a positive integer.
Returns:
- An object representing the current size of the container if no arguments are pass.
- A reference to the graph object from which the method is called upon.
Default values:
The default values for the container size are as follows:
{
width : 500,
height : 500
}
Readonly Methods
The graph implements some read only methods that are valuable for external interactions/calculations. For example if you want to write a custom event listener.
Draw:
This method render the graph only if its properties have been changed since the last call.
You need to call this method every time you made some change to the graph, add, remove or change data.
Method:
draw()
Returns:
- A reference to the graph object from which the method is called upon.
Canvas Elements:
This method returns the html canvas elements used in the graph.
Those are children elements of the div container.
Method:
canvasElements()
Returns:
- An array of two html canvas elements, the first is used to draw the axis, grids and labels, and the second one is were the data, colorbars and legends are draw.
Note: The pointer events of the
pointerMoveandpointerZoommethods are set on the second element of the array.
Client Rect:
This method returns the client rec.
The client rect is a rectangle defined inside the graph and contains the axis and the graph rect. This area is affected by the labels.
The graph rect is contained within the client rect.
Method:
clientRect()
Returns:
- An object representing the
xandycoordinates of the top left corner and the width and height in pixels of the client rect.
Examples of the client rect:
Graph Rect:
This method returns the graph rec.
The graph rect is a rectangle defined inside the graph and contains only the graphing area. This area is affected by the labels, margins and the axis size.
Method:
graphRect()
Returns:
- An object representing the
xandycoordinates of the top left corner and the width and height in pixels of the graph rect.
Examples of the graph rect:
Coordinate Maps:
This method returns the mappin objets used to transform between pixel coordinates and internal axis coordinates.
The mapping domain is the axis coordinates, while the range is the pixel coordinates. The pixel coordinates are measured from the graph rect.
Method:
coordinateMaps()
Returns:
- An object with the mapping or scale of each axis. The mapping of the secondary axis may be undefined if that axis is not enabled.
Save:
This method lets you save the graph state, including the datasets, legends and colorbars. The resulting object can be serialized, usin JSON.stringify() for example.
This method can be used alongside the utility function restoreGraph to restore a saved graph.
Method:
save()
Returns:
- An object with the following properties:
-
graph: an object with the state of the graph. This object is compatible with theoptionsarument of thegraph2Dfunction. -
assets: an array of objects, this objects represent all the datasets, colorbars and legends associated with the graph. Each object contains:-
options: an object with the state of the asset. This object is compatible with theoptionsargument of its corresponding function. -
assetType: a string that identifies the asset type, can be one of the following options:"linechart""area""heatmap""vectorfield""colorbar""legend"
-
-
Note: This method saves the graph statically. That is if any dataset has its data points or any property generated dynamically via a callback function, that callback will be evaluated and only the result will be saved.
Assets:
The graph object can hold a variety of assets, that includes colorbars, legends or any of the available datasets.
Each of these assets is bound to the graph at the moment of its creation.
Add Dataset:
This method lets you create a dataset.
Method:
addDataset(type)
addDataset(type, options)
addDataset(type, options, callback)
Where:
-
typeis a string indicating one of the available datasets. The posible values are:-
"linechart". -
"area". -
"heatmap". -
"vectorfield".
-
-
optionsis an object containing the options to change the default behavior and appearance of the dataset. -
callbackis a function that is run after the dataset is created but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note. The options object has a lot of properties and complex structure, its use is intended for copying or generate datasets from another datasets. All its properties are breake down in the corresponding dataset methods, is best to use those instead.
Returns:
- A dataset object. This object contains the methods of the selected
typeso it's convenient to assign this value to a variable for later use.
Get Datasets
This method returns an object containing all the datasets associated with the graph.
Method:
getDatasets()
Returns:
An object with the following properties:
-
linechart: an array with alllinechartobjects. -
area: an array with allareaobjects. -
heatmap: an array with allheatmapobjects. -
vectorfield: an array with allvectorfieldobjects.
Remove Dataset
This method removes a dataset form the graph.
Method:
removeDataset(id)
removeDataset(id, callback)
Where:
-
idis the id string of the dataset you want to remove. -
callbackis a function that is run after the dataset is removed but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- A reference to the graph object from which the method is called upon.
Add Colorbar:
This method lets you create a colorbar.
Method:
addColorbar()
addColorbar(options)
addColorbar(options, callback)
Where:
-
optionsis an object containing the options to change the default behavior and appearance of the colorbar. -
callbackis a function that is run after the colorbar is created but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note. The options object has a lot of properties and complex structure, its use is intended for copying or generate colorbars from another colorbars. All its properties are breake down in the colorbar methods, is best to use those instead.
Returns:
- A colorbar object. This object contains the methods to change the behavior and appearence of the colorbar, so it's convenient to assign this value to a variable for later use.
Get Colorbars
This method returns an array containing all the colorbars associated with the graph.
Method:
getColorbars()
Returns:
- An array with all colorbars.
Remove Colorbar
This method removes a colorbar form the graph.
Method:
removeColorbar(id)
removeColorbar(id, callback)
Where:
-
idis the id string of the colorbar you want to remove. -
callbackis a function that is run after the colorbar is removed but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- A reference to the graph object from which the method is called upon.
Add Legend:
This method lets you create a colorbar.
Method:
addLegend()
addLegend(options)
addLegend(options, callback)
Where:
-
optionsis an object containing the options to change the default behavior and appearance of the legend. -
callbackis a function that is run after the legend is created but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Note. The options object has a lot of properties and complex structure, its use is intended for copying or generate legends from another legends. All its properties are breake down in the legend methods, is best to use those instead.
Returns:
- A legend object. This object contains the methods to change the behavior and appearence of the legend, so it's convenient to assign this value to a variable for later use.
Get Legends
This method returns an array containing all the legends associated with the graph.
Method:
getLegends()
Returns:
- An array with all legends.
Remove Legend
This method removes a legend form the graph.
Method:
removeLegend(id)
removeLegend(id, callback)
Where:
-
idis the id string of the legend you want to remove. -
callbackis a function that is run after the legend is removed but before the next render, this callback accepts an optional argument that represents the graph object from which the method is called upon.
Returns:
- A reference to the graph object from which the method is called upon.
Datasets
Line Chart:
The linechart dataset is use to create, as the name implies, line charts but also can create dispersion graphs.
It uses a set of datapoints that consist of two arrays of numbers with the same size representing the [x, y] coordinates of each point.
The main components of a line chart are:
- The data itself.
- The
linesthat can be draw connecting each datapoint. - The
markersthat can be draw on each datapoint. - The
error barsthat can be draw on each datapoint.
The datapoints and properties of that datapoints can be defined to be static or dynamic, meaning that its values can be static and never change or be computed on each draw and even be based on the values of other properties.
If you set any of the datapoint components to be dynamic, that component must be defined by a function of the type:
generator()
generator(dataset)
generator(dataset, graph)
Where:
-
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
These two optional arguments always holds the dataset and graph latest state at the moment of the call.
Note: Unlike the callback that most methods accept, the generator function runs on every draw.
The linechart data generator must return an array of numbers.
Example:
First, lets create a static line chart.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisDomain({
x : {start : -8, end : 8},
y : {start : -1.2, end : 1.2}
})
.containerSize({width : 800, height : 300})
.pointerMove();
//Creates the linechart
const line_chart = my_graph.addDataset("linechart");
//Creates the data arrays
const x_data = linspace(-8, 8, 150);
const y_data = x_data.map(x => Math.cos(x));
//Add the data to the linechart
line_chart.dataX(x_data).dataY(y_data);
//Finally draws the graph
my_graph.draw();
Result:
Note:
linspaceis a utility function, you can learn more about this functions on the extras section.
The properties of the linechart can be easily modified, for example by adding to the last script.
line_cart.lineColor("#cf0808");
//Don't forget to call the draw() method after each change.
my_graph.draw();
Changes the line color
Now let's recreate the same graph but using dynamic data instead.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisDomain({
x : {start : -8, end : 8},
y : {start : -1.2, end : 1.2}
})
.containerSize({width : 800, height : 300})
.pointerMove();
//Creates the linechart and add the data
const line_chart = my_graph.addDataset("linechart");
line_chart.dataX((dataset, graph)=>{
//This function will run on every draw
//Inside this function, dataset makes reference to line_chart and
//graph to my_graph
const domain = graph.axisDomain();
//The return value must be an array of numbers
return linspace(domain.x.start, domain.x.end, 150)
})
.dataY( dataset=>{
//In this case we only need a reference to the dataset
const x_values = dataset.dataX(); //From here we can get the generated x values and other non dynamic properties.
const y = x_values.map(x=>Math.cos(x));
//Again, the return value must be an array of numbers
return y;
});
//Finally draws the graph
my_graph.draw();
Result:
As shown, the graph can be moved indefinitely and the data moves along with it.
Warning: You may only read static properties from dynamic data generators, this is because dynamic properties depend on the
xandydata values, so calling those from a data generator will create a circular dependency and the program will stop.
Warning: You must not call the
dataX()ordraw()method from thexdata generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
dataY()ordraw()method from theydata generator because that will create a circular dependency and the program will stop.
Note: It is safe to call the
dataX()method from within theydata generator and vice versa.
The creation of dynamic properties is slightly different. This is because the properties are assigned to each datapoint so the generator function depends directly on the x and y data components.
The generator function will be call once for each datapoint and must return a suitable value for that datapoint.
The generator can accept some optional parameters that must be call in the next order:
generator(x, y, index, arrayX, arrayY, dataset, graph)
Where:
-
x: is thexvalue of the datapoint. -
y: is theyvalue of the datapoint. -
index: is the index corresponding to thexandyvalues. -
arrayX: is the complete arary ofxdata values. -
arrayY: is the complete arary ofydata values. -
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
Note: All the arguments of the property generator function are optional.
Note: The property generator operates in a similar way of the array methods (map, forEach, etc).
For example, the line color of the last graph can be dynamically change to be dependent of the y values by adding:
//Create a colormap
const color = colorMap({
from : -1,
to : 1,
type : "magnet"
});
line_chart.lineColor((x, y)=>{
//In this case we only need access to the data values in the arguments.
//The return value will be assigned to the corresponding datapoint
return color(y);
});
my_graph.draw();
Result:
Note:
colorMapis a utility function, you can learn more about this functions on the extras section.
ID:
This method lets you set or get the id string of the dataset.
Method:
id()
id(option)
id(option, callback)
Where:
-
option: is a string representing theidof the dataset. -
callback: is a function that is run after theidis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
idis used to identify the dataset, so the string must be unique among other datasets.
By default, the id is generated automatically to guarantee its uniqueness, so in the majority of cases it is best to leave it as it is.
Returns:
- A string representing the
idof the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Index:
This method lets you set or get the index of the dataset.
The index is used to determine the order in which the datasets will be drawn. The dataset with the lower index value will be drawn first and appear to be behind other datasets.
Method:
index()
index(option)
index(option, callback)
Where:
-
option: is a number representing theindexvalue of the dataset. -
callback: is a function that is run after theindexis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The draw order of datasets with the same
indexvalue will be indeterminate.
Returns:
- A number that represents the
indexof the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
Dataset Type:
This method returns the linechart dataset type.
Method:
datasetType()
Returns:
- This method always return the string
"linechart".
This is usefull when you read the dataset from the graph2D getDatasets() method or the restoreGraph() function.
Line Data
The data in the linechart is composed of two arrays of numbers with equal length. Each pair of numbers that share the same index in the arrays represent the [x, y] coordinates of one data point.
Each array must be defined independently.
Data x:
This method lets you get ot set the x component of the data points.
Method:
dataX()
dataX(data)
dataX(data, callback)
Where:
-
data: represents thexcomponent of the data points. It can be one of the following options:- An array of numbers.
- A function that returns an array of numbers. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the data as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
dataX()nordraw()should be called from within the data function, because that will cause a circular dependency and the program will stop.
Warning: Any dynamic property should not be read from within the data function, because that will cause a circular dependency and the program will stop.
Note: It is safe to call static properties.
When the data is defined as a function, we say that is dynamically generated.
If the data is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the data will not update.
Note: The
linechartdata generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
xcomponent of the datapoints if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the data is defined as a function, this method will return an array of numbers.
Data y:
This method lets you get ot set the y component of the data points.
Method:
dataY()
dataY(data)
dataY(data, callback)
Where:
-
data: represents theycomponent of the data points. It can be one of the following options:- An array of numbers.
- A function that returns an array of numbers. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the data as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
dataY()nordraw()should be called from within the data function, because that will cause a circular dependency and the program will stop.
Warning: Any dynamic property should not be read from within the data function, because that will cause a circular dependency and the program will stop.
Note: It is safe to call static properties.
When the data is defined as a function, we say that is dynamically generated.
If the data is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the data will not update.
Note: The
linechartdata generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
ycomponent of the datapoints if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the data is defined as a function, this method will return an array of numbers.
Axis Used:
This method lets you set or get the axis used by the linechart.
Method:
axisUsed()
axisUsed(options)
axisUsed(options, callback)
Where:
-
options: is an object containing the following properties:-
x: a string representing thexaxis that the dataset is goin to use. It can be"primary"or"secondary". -
y: a string representing theyaxis that the dataset is goin to use. It can be"primary"or"secondary".
-
-
callback: is a function that is run after the axis used is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- An object representing the axis being used by the dataset if no arguments are pass.
- A reference to the dataset object from which the method is called upon.
Default Value:
The default values for the axis used property are as follow.
{
x : "primary",
y : "primary"
}
Polar:
This method lets you set or get the polar property of the linechart.
This property controls whether the data arrays must be interpreted in rectangular or polar coordinates system.
Method:
polar()
polar(option)
polar(option, callback)
Where:
-
option: is a boolean that determines whether thedatamust be interpreted inpolarcoordinates. -
callback: is a function that is run after thepolarproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
If the polar property is set to true then:
-
dataXwill be interpreted as the radial coordinates. -
dataYwill be interpreted as the angular coordinates.
For example, consider the data arrays:
const r = linspace(1, 1, 200); //[1,1,1, ..., 1]
const theta = linspace(0, 2*Math.PI, 200); //[0, 0.042, ..., 6.283]
//Assign the data to the linechart
line_chart.dataX(r).dataY(theta);
//Don't forget to render the new state.
my_graph.draw();
polar(false) |
polar(true) |
|---|---|
![]() |
![]() |
When the polar property is set to false the linechart only shows a straight line but the data is correctly interpreted as a full circumference of radius one if the polar property is set as true.
Returns:
- A boolean that represents the linechart
polarproperty value if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
The default value for the linechart polar property is false.
Line Properties
Line Enable:
This method lets you set or get the enable status of the line drawn.
Method:
lineEnable()
lineEnable(option)
lineEnable(option, callback)
Where:
-
option: is a boolean that determines whether the line must be enabled. -
callback: is a function that is run after the lineenableproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: This method only affects the line that is draw between datapoints, to fully disable a
linechartthe marks and error bars must be disabled as well.
Returns:
- A bolean representing the
enablestatus of the line. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enableproperty of the line istrue.
Line Color:
This method lets you set or get the color of the line drawn between datapoints.
Method:
lineColor()
lineColor(option)
lineColor(option, callback)
Where:
-
option: represents the linecolor. It can be one of the following options:- A string that represents the whole line
color. - An array of strings, each representing the line
coloron each datapoint. - A generator function that returns a string representing the
colorat each datapoint.
- A string that represents the whole line
-
callback: is a function that is run after the linecoloris set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: If the
coloris defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the line
colorif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
coloris defined as a function.
Default Value:
- The default value for the line
coloris"#0043e0".
Line Opacity:
This method lets you set or get the opacity of the line drawn between datapoints.
Method:
lineOpacity()
lineOpacity(option)
lineOpacity(option, callback)
Where:
-
option: represents the lineopacity. It can be one of the following options:- A number that represents the whole line
opacity. - An array of numbers, each representing the line
opacityon each datapoint. - A generator function that returns a number representing the
opacityat each datapoint.
- A number that represents the whole line
-
callback: is a function that is run after the lineopacityis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
opacityvalues must be a number between 0 and 1.
Note: If the
opacityis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
opacityif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
opacityis defined as a function.
Default Value:
- The default value for the line
opacityis1.
Line Style:
This method lets you set or get the style of the line drawn between datapoints.
Method:
lineStyle()
lineStyle(option)
lineStyle(option, callback)
Where:
-
option: represents the linestyle. It can be one of the following options:- A string that represents the whole line
style. - An array of strings, each representing the line
styleon each datapoint. - A generator function that returns a string representing the
styleat each datapoint.
- A string that represents the whole line
-
callback: is a function that is run after the linestyleis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
styleis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- A string or an array of strings representing the line
styleif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
styleis defined as a function.
Default Value:
- The default value for the line
styleis"solid".
Line Width:
This method lets you set or get the width of the line drawn between datapoints.
Method:
lineWidth()
lineWidth(option)
lineWidth(option, callback)
Where:
-
option: represents the linewidth. It can be one of the following options:- A number that represents the whole line
width. - An array of numbers, each representing the line
widthon each datapoint. - A generator function that returns a number representing the
widthat each datapoint.
- A number that represents the whole line
-
callback: is a function that is run after the linewidthis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
widthvalues must be a positive integer.
Note: If the
widthis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
widthif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
widthis defined as a function.
Default Value:
- The default value for the line
widthis1.
Marker
The markers are symbols that can be drawn on each data point.
These are useful when the data is not that dense and you need to show exactly where each datapoint is.
Marker Enable:
This method lets you set or get the enable status of the markers.
Method:
markerEnable()
markerEnable(option)
markerEnable(option, callback)
Where:
-
option: is a boolean that determines whether the markers must be enabled. -
callback: is a function that is run after the markersenableproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: This method only affects the markers, to fully disable a
linechartthe line and error bars must be disabled as well.
Returns:
- A bolean representing the
enablestatus of the marker. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for the
enableproperty of the marker isfalse.
Marker Color:
This method lets you set or get the color of the markers.
Method:
markerColor()
markerColor(option)
markerColor(option, callback)
Where:
-
option: represents the markercolor. It can be one of the following options:- A string that represents the
colorof all markers. - An array of strings, each representing the marker
colorat each datapoint. - A generator function that returns a string representing the
colorat each datapoint.
- A string that represents the
-
callback: is a function that is run after the markercoloris set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: If the
coloris defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the markers
colorif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
coloris defined as a function.
Default Value:
- The default value for the markers
coloris"#0043e0".
Marker Opacity:
This method lets you set or get the opacity of the markers.
Method:
markerOpacity()
markerOpacity(option)
markerOpacity(option, callback)
Where:
-
option: represents the markeropacity. It can be one of the following options:- A number that represents the
opacityof all markers. - An array of numbers, each representing the marker
opacityat each datapoint. - A generator function that returns a number representing the marker
opacityat each datapoint.
- A number that represents the
-
callback: is a function that is run after the markeropacityis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
opacityvalues must be a number between 0 and 1.
Note: If the
opacityis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the marker
opacityif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
opacityis defined as a function.
Default Value:
- The default value for the marker
opacityis1.
Marker Style:
This method lets you set or get the style of the markers.
Method:
markerStyle()
markerStyle(option)
markerStyle(option, callback)
Where:
-
option: represents the marker linestyle. It can be one of the following options:- A string that represents the
styleof all markers. - An array of strings, each representing the marker line
styleat each datapoint. - A generator function that returns a string representing the marker line
styleat each datapoint.
- A string that represents the
-
callback: is a function that is run after the markerstyleis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: This method is only relevant wen the marker
filledproperty is set tofalse.
Note: If the
styleis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- A string or an array of strings representing the marker line
styleif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
styleis defined as a function.
Default Value:
- The default value for the marker line
styleis"solid".
Marker Width:
This method lets you set or get the line width of markers.
Method:
markerWidth()
markerWidth(option)
markerWidth(option, callback)
Where:
-
option: represents the marker linewidth. It can be one of the following options:- A number that represents line
widthof all markers. - An array of numbers, each representing the marker line
widthat each datapoint. - A generator function that returns a number representing the marker line
widthat each datapoint.
- A number that represents line
-
callback: is a function that is run after the marker linewidthis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: This method is only relevant wen the marker
filledproperty is set tofalse.
Note: The
widthvalues must be a positive integer.
Note: If the
widthis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the marker line
widthif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
widthis defined as a function.
Default Value:
- The default value for the marker line
widthis1.
Marker Filled:
This method lets you set or get the filled property of the markers.
If set to true, the marker will be drawn as a solid figure, otherwise will only be outlined.
Method:
markerFilled()
markerFilled(option)
markerFilled(option, callback)
Where:
-
option: represents the markerfilledproperty. It can be one of the following options:- A boolean that determine whether all markers should be
filled. - An array of boolean, each representing whether the marker should be
filledat each datapoint. - A generator function that returns a boolean determining whether the marker should be
filledat each datapoint.
- A boolean that determine whether all markers should be
-
callback: is a function that is run after the markerfilledproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
filledproperty is defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A bolean or a string of boolean representing the marker
filledproperty. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if
filledis defined as a function.
Default Value:
- The default values for the
enableproperty of the line istrue.
Marker Type:
This method lets you set or get the marker type.
Method:
markerType()
markerType(option)
markerType(option, callback)
Where:
-
option: represents the markertype. It can be one of the following options:- A string representing the
typeof all markers. - An array of strings representing the marker
typeat each datapoint. - A generator function that returns a string representing the marker
typeat each datapoint.
- A string representing the
-
callback: is a function that is run after the markertypeis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the marker
typeis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
The property type can be any of the following options:
| Type | Type | ||||
|---|---|---|---|---|---|
| "circle" | ![]() |
![]() |
"square" | ![]() |
![]() |
| "v-rect" | ![]() |
![]() |
"h-rect" | ![]() |
![]() |
| "cross" | ![]() |
![]() |
"star" | ![]() |
![]() |
| "triangle" | ![]() |
![]() |
"inv-triangle" | ![]() |
![]() |
Returns:
- A string or an array of strings representing the marker
typeif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
typeis defined as a function.
Default Value:
- The default value for the marker
typeis"circle".
Marker Size:
This method lets you set or get the size of the markers.
Method:
markerSize()
markerSize(option)
markerSize(option, callback)
Where:
-
option: represents the markersize. It can be one of the following options:- A number representing the
sizeof all markers. - An array of numbers representing the marker
sizeat each datapoint. - A generator function that returns a number representing the marker
sizeat each datapoint.
- A number representing the
-
callback: is a function that is run after the markersizeis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The marker
sizemust be a positive number.
Note: If the marker
sizeis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Unlike other numeric properties, the marker size is not expressed in pixels, but relative to the default size.
That is, the default size is 1, so a value of 2 will be double the size and 0.5 the half.
Note: Each
sizeis different to maintain the apparent size between them consistent. For reference, the default size of the markers is approximately 10px.
Returns:
- A number or an array of numbers representing the marker
sizeif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
sizeis defined as a function.
Default Value:
- The defaul value for the marker
sizeis1.
Error Bars
The error bars are aditional markers that represent the error or uncertainty in the values or the datapoints.
Unlike regular markers, the error bars have a precise and measurable meaning. It can represent a standad deviation, standard error or some confidence interval.
Either way, the value associated with a given error bar will extend equally on both sides of the datapoint in the corresponding axis.
Error Bar Enabled
This method lets you set or get the enable status of the errorbars.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
markerEnable()
markerEnable(option)
markerEnable(option, callback)
Where:
-
option: is an object containing the following properties.-
x: a boolean that determines whether thexerror bars must be enabled. -
y: a boolean that determines whether theyerror bars must be enabled.
-
-
callback: is a function that is run after the error barsenableproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: This method only affects the error bars, to fully disable a
linechartthe line and markers must be disabled as well.
Returns:
- An object representing the
enableproperty of the error bars. - A reference to the dataset object from which the method is called upon.
Default Value:
The default values for the enable property of the error bars are as follows:
{
x : false,
y : false
}
Error Bar Type:
This method lets you set or get the type of the error bars.
Method:
errorbarType()
errorbarType(option)
errorbarType(option, callback)
Where:
-
option: represents the error bartype. It can be one of the following options:- A string representing the
typeof all error bars. - An array of strings representing the error bar
typeat each datapoint. - A generator function that returns a string representing the error bar
typeat each datapoint.
- A string representing the
-
callback: is a function that is run after the error bartypeis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the error bar
typeis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
The property type can be any of the following options:
| Type | Type | ||
|---|---|---|---|
"rectangle" |
![]() |
"cross" |
![]() |
"corner" |
![]() |
"tail-cross" |
![]() |
Returns:
- A string or an array of strings representing the error bar
typeif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
typeis defined as a function.
Default Value:
- The default value for the error bar
typeis"tail-cross".
Error Bar Data x
This method lets you set or get the data values for the error bar x component.
Method:
errorbarDataX()
errorbarDataX(option)
errorbarDataX(option, callback)
Where:
-
option: represents thedataof the error barxcomponent. It can be one of the following options:- A number that represents the
xdataof all error bars. - An array of numbers, each representing the errorbar
xdataat each datapoint. - A generator function that returns a number representing the error bar
xdataat each datapoint.
- A number that represents the
-
callback: is a function that is run after the error bardatais set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
datais defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
dataof the error barxcomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
datais defined as a function.
Default Value:
- The default value for the error bar
xdatais0.
Error Bar Data y
This method lets you set or get the data values for the error bar y component.
Method:
errorbarDataY()
errorbarDataY(option)
errorbarDataY(option, callback)
Where:
-
option: represents thedataof the error barycomponent. It can be one of the following options:- A number that represents the
ydataof all error bars. - An array of numbers, each representing the errorbar
ydataat each datapoint. - A generator function that returns a number representing the error bar
ydataat each datapoint.
- A number that represents the
-
callback: is a function that is run after the error bardatais set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
datais defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
dataof the error barycomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
datais defined as a function.
Default Value:
- The default value for the error bar
ydatais0.
Error Bar Color x:
This method lets you set or get the color of the error bar x component.
Method:
errorBarColorX()
errorBarColorX(option)
errorBarColorX(option, callback)
Where:
-
option: represents thecolorof the error barxcomponent. It can be one of the following options:- A string that represents the
colorof all error bars. - An array of strings, each representing the error bar
colorat each datapoint. - A generator function that returns a string representing the error bar
colorat each datapoint.
- A string that represents the
-
callback: is a function that is run after the error barcoloris set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: If the
coloris defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the
colorof the error barxcomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
coloris defined as a function.
Default Value:
- The default value for the error bars
xcoloris"#666666".
Error Bar Color y:
This method lets you set or get the color of the error bar y component.
Method:
errorBarColorY()
errorBarColorY(option)
errorBarColorY(option, callback)
Where:
-
option: represents thecolorof the error barycomponent. It can be one of the following options:- A string that represents the
colorof all error bars. - An array of strings, each representing the error bar
colorat each datapoint. - A generator function that returns a string representing the error bar
colorat each datapoint.
- A string that represents the
-
callback: is a function that is run after the error barcoloris set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: If the
coloris defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the
colorof the error barycomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
coloris defined as a function.
Default Value:
- The default value for the error bars
ycoloris"#666666".
Error Bar Opacity x:
This method lets you set or get the opacity of the error bar x component.
Method:
errorbarOpacityX()
errorbarOpacityX(option)
errorbarOpacityX(option, callback)
Where:
-
option: represents theopacityof the error barxcomponent. It can be one of the following options:- A number that represents the
opacityof all error bars. - An array of numbers, each representing the error bar
opacityat each datapoint. - A generator function that returns a number representing the error bar
opacityat each datapoint.
- A number that represents the
-
callback: is a function that is run after the error baropacityis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
opacityvalues must be a number between 0 and 1.
Note: If the
opacityis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
opacityof the error barxcomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
opacityis defined as a function.
Default Value:
- The default value for the error bar
xopacityis1.
Error Bar Opacity y:
This method lets you set or get the opacity of the error bar y component.
Method:
errorbarOpacityY()
errorbarOpacityY(option)
errorbarOpacityY(option, callback)
Where:
-
option: represents theopacityof the error barycomponent. It can be one of the following options:- A number that represents the
opacityof all error bars. - An array of numbers, each representing the error bar
opacityat each datapoint. - A generator function that returns a number representing the error bar
opacityat each datapoint.
- A number that represents the
-
callback: is a function that is run after the error baropacityis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
opacityvalues must be a number between 0 and 1.
Note: If the
opacityis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
opacityof the error barycomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
opacityis defined as a function.
Default Value:
- The default value for the error bar
yopacityis1.
Error Bar Style x:
This method lets you set or get the style of the error bar x component.
Method:
errorbarStyleX()
errorbarStyleX(option)
errorbarStyleX(option, callback)
Where:
-
option: represents the linestyleof the error barxcomponent. It can be one of the following options:- A string that represents the
styleof all error bars. - An array of strings, each representing the error bar line
styleat each datapoint. - A generator function that returns a string representing the error bar line
styleat each datapoint.
- A string that represents the
-
callback: is a function that is run after the error barstyleis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
styleis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- A string or an array of strings representing the line
styleof the error barxcomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
styleis defined as a function.
Default Value:
- The default value for the error bar
xlinestyleis"solid".
Error Bar Style y:
This method lets you set or get the style of the error bar y component.
Method:
errorbarStyleY()
errorbarStyleY(option)
errorbarStyleY(option, callback)
Where:
-
option: represents the linestyleof the error barycomponent. It can be one of the following options:- A string that represents the
styleof all error bars. - An array of strings, each representing the error bar line
styleat each datapoint. - A generator function that returns a string representing the error bar line
styleat each datapoint.
- A string that represents the
-
callback: is a function that is run after the error barstyleis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
styleis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- A string or an array of strings representing the line
styleof the error barycomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
styleis defined as a function.
Default Value:
- The default value for the error bar
ylinestyleis"solid".
Error Bar Width x:
This method lets you set or get the line width of the error bar x component.
Method:
errorbarWidthX()
errorbarWidthX(option)
errorbarWidthX(option, callback)
Where:
-
option: represents the linewidthof the error barxcomponent. It can be one of the following options:- A number that represents line
widthof all error bars. - An array of numbers, each representing the error bar line
widthat each datapoint. - A generator function that returns a number representing the error bar line
widthat each datapoint.
- A number that represents line
-
callback: is a function that is run after the error bar linewidthis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
widthvalues must be a positive integer.
Note: If the
widthis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
widthof the error barxcomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
widthis defined as a function.
Default Value:
- The default value for the error bar
xlinewidthis1.
Error Bar Width y:
This method lets you set or get the line width of the error bar y component.
Method:
errorbarWidthY()
errorbarWidthY(option)
errorbarWidthY(option, callback)
Where:
-
option: represents the linewidthof the error barycomponent. It can be one of the following options:- A number that represents line
widthof all error bars. - An array of numbers, each representing the error bar line
widthat each datapoint. - A generator function that returns a number representing the error bar line
widthat each datapoint.
- A number that represents line
-
callback: is a function that is run after the error bar linewidthis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
widthvalues must be a positive integer.
Note: If the
widthis defined as an array, its length must be the same as the data arrays.
Note: The
linechartproperty generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
widthof the error barycomponent if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if the
widthis defined as a function.
Default Value:
- The default value for the error bar
ylinewidthis1.
Area:
The area dataset is used to show the area between two curves, for example when you need to represent the integral of a given function.
It uses two sets of datapoints, each consisting of two array of numbers representing the [x, y] coordinates of each point.
The two sets of datapoints can be defined to be static or dynamic, meaning that its values can be static and never change or be computed on each draw and even be based on the values of other properties.
If you set any of the datapoint components to be dynamic, that component must be defined by a function of the type:
generator()
generator(dataset)
generator(dataset, graph)
Where:
-
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
These two optional arguments always holds the dataset and graph latest state at the moment of the call.
Note: Unlike the callback that most methods accept, the generator function runs on every draw.
First lets create a static area graph.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisDomain({
x : {start : -12, end : 12},
y : {start : -0.3, end : 1.1}
})
.containerSize({width : 800, height : 300})
.pointerMove();
//Creates a simple linechart for reference
const line_chart = my_graph.addDataset("linechart");
const line_data_x = linspace(-12, 12, 150);
const line_data_y = line_data_x.map(x => Math.sin(x)/x);
line_chart.dataX(line_data_x).dataY(line_data_y);
//Now creates the area graph.
const area = my_graph.addDataset("area");
//Creates the data arrays
const area_data_x = linspace(-12, 12, 150);
const area_data_y = area_data_x.map(x => Math.sin(x)/x);
//The area need two sets of data arrays
const area_base_x = [-12, 12];
const area_base_y : [0, 0];
//Add the data to the area graph
area.dataX(area_data_x)
.dataY(area_data_y)
.baseX(area_base_x)
.baseY(area_base_y);
//Finally draws the graph
my_graph.draw();
Result:
Note:
linspaceis a utility function, you can learn more about this functions on the extras section.
As shown, the area is being drawn between the data and the base curves. In this case, the data curve follows the linechar and the base curve follows the x axis, that is, a straight line from [-12, 0] to [12, 0].
Note: It is not necessary for the
baseto be a straight line, it can be any arbitrary curve.
Note: It is not necessary for the
dataandbasecurves to share the same endpoints or datapoint number, they both are independent.
Now let's recreate the same graph but using dynamic data instead.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisDomain({
x : {start : -12, end : 12},
y : {start : -0.3, end : 1.1}
})
.containerSize({width : 800, height : 300})
.pointerMove();
//Creates a simple linechart for reference
const line_chart = my_graph.addDataset("linechart");
//The linechart data remains static
const line_data_x = linspace(-12, 12, 150);
const line_data_y = line_data_x.map(x => Math.sin(x)/x);
line_chart.dataX(line_data_x).dataY(line_data_y);
//Now creates the area graph ans add the data.
const area = my_graph.addDataset("area");
area.dataX((dataset, graph)=>{
//This function will run on every draw
//Inside this function, dataset makes reference to line_chart and
//graph to my_graph
const domain = graph.axisDomain();
//The return value must be an array of numbers
return linspace(domain.x.start, domain.x.end, 150)
})
.dataY( dataset=>{
//In this case we only need a reference to the dataset
const x_values = dataset.dataX(); //From here we can get the generated x values
const y = x_values.map(x=>Math.sin(x)/x);
//Again, the return value must be an array of numbers
return y;
} )
.baseX((dataset, graph)=>{
const domain = graph.axisDomain();
//In this case only the axis endpoints are need.
return [domain.x.start, domain.x.end];
})
.baseY([0, 0]);
//Finally draws the graph
my_graph.draw();
Result:
As shown, the graph can be moved indefinitely and the area moves along with it.
Warning: You must not call the
dataX()ordraw()method from thexdata generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
dataY()ordraw()method from theydata generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
baseX()ordraw()method from thexbase generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
baseY()ordraw()method from theybase generator because that will create a circular dependency and the program will stop.
It is safe to call every other method from the generator functions.
ID:
This method lets you set or get the id string of the dataset.
Method:
id()
id(option)
id(option, callback)
Where:
-
option: is a string representing theidof the dataset. -
callback: is a function that is run after theidis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
idis used to identify the dataset, so the string must be unique among other datasets.
By default, the id is generated automatically to guarantee its uniqueness, so in the majority of cases it is best to leave it as it is.
Returns:
- A string representing the
idof the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Index:
This method lets you set or get the index of the dataset.
The index is used to determine the order in which the datasets will be drawn. The dataset with the lower index value will be drawn first and appear to be behind other datasets.
Method:
index()
index(option)
index(option, callback)
Where:
-
option: is a number representing theindexvalue of the dataset. -
callback: is a function that is run after theindexis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The draw order of datasets with the same
indexvalue will be indeterminate.
Returns:
- A number that represents the
indexof the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
Enable:
This method lets you set or get the enable status of the area.
Method:
enable()
enable(option)
enable(option, callback)
Where:
-
option: is a boolean that determines whether the area must be enabled. -
callback: is a function that is run after the areaenableproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A bolean representing the
enablestatus of the area. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enableproperty of the area istrue.
Dataset Type:
This method returns the area dataset type.
Method:
datasetType()
Returns:
- This method always return the string
"area".
This is usefull when you read the dataset from the graph2D getDatasets() method or the restoreGraph() function.
Area Data
The data in the area dataset is composed of two different sets each with two arrays of numbers with equal length. Each pair of numbers that share the same index in the arrays represent the [x, y] coordinates of one data point.
Each set of data is independent and are named the data and the base curves. The area is drawn between these two curves.
Each array must be defined independently.
Data x:
This method lets you get ot set the x component of the data curve.
Method:
dataX()
dataX(data)
dataX(data, callback)
Where:
-
data: represents thexcomponent of thedatacurve. It can be one of the following options:- An array of numbers.
- A function that returns an array of numbers. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the data as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
dataX()nordraw()should be called from within the data function, because that will cause a circular dependency and the program will stop.
When the data is defined as a function, we say that is dynamically generated.
If the data is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the data will not update.
Note: The
areadata generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
xcomponent of thedatacurve if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the data is defined as a function, this method will return an array of numbers.
Data y:
This method lets you get ot set the y component of the data curve.
Method:
dataY()
dataY(data)
dataY(data, callback)
Where:
-
data: represents theycomponent of thedatacurve. It can be one of the following options:- An array of numbers.
- A function that returns an array of numbers. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the data as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
dataY()nordraw()should be called from within the data function, because that will cause a circular dependency and the program will stop.
When the data is defined as a function, we say that is dynamically generated.
If the data is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the base will not update.
Note: The
areadata generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
ycomponent of thedatacurve if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the data is defined as a function, this method will return an array of numbers.
Base x:
This method lets you get ot set the x component of the base curve.
Method:
baseX()
baseX(base)
baseX(base, callback)
Where:
-
base: represents thexcomponent of thebasecurve. It can be one of the following options:- An array of numbers.
- A function that returns an array of numbers. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the
baseas a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
baseX()nordraw()should be called from within thebasefunction, because that will cause a circular dependency and the program will stop.
When the base is defined as a function, we say that is dynamically generated.
If the base is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the data will not update.
Note: The
areadata generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
xcomponent of thebasecurve if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the base is defined as a function, this method will return an array of numbers.
Base y:
This method lets you get ot set the y component of the base curve.
Method:
baseX()
baseX(base)
baseX(base, callback)
Where:
-
base: represents theycomponent of thebasecurve. It can be one of the following options:- An array of numbers.
- A function that returns an array of numbers. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the base is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the
baseas a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
baseY()nordraw()should be called from within thebasefunction, because that will cause a circular dependency and the program will stop.
When the base is defined as a function, we say that is dynamically generated.
If the base is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the base will not update.
Note: The
areadata generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
ycomponent of thebasecurve if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the base is defined as a function, this method will return an array of numbers.
Axis Used:
This method lets you set or get the axis used by the area.
Method:
axisUsed()
axisUsed(options)
axisUsed(options, callback)
Where:
-
options: is an object containing the following properties:-
x: a string representing thexaxis that the dataset is goin to use. It can be"primary"or"secondary". -
y: a string representing theyaxis that the dataset is goin to use. It can be"primary"or"secondary".
-
-
callback: is a function that is run after the axis used is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- An object representing the axis being used by the dataset if no arguments are pass.
- A reference to the dataset object from which the method is called upon.
Default Value:
The default values for the axis used property are as follow.
{
x : "primary",
y : "primary"
}
Polar:
This method lets you set or get the polar property of the area.
This property controls whether the data and base arrays must be interpreted in rectangular or polar coordinates system.
Method:
polar()
polar(option)
polar(option, callback)
Where:
-
option: is a boolean that determines whether thedataandbasemust be interpreted inpolarcoordinates. -
callback: is a function that is run after thepolarproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
If the polar property is set to true then:
-
dataXwill be interpreted as the radial coordinates of thedatacurve. -
dataYwill be interpreted as the angular coordinates of thedatacurve. -
baseXwill be interpreted as the radial coordinates of thebasecurve. -
baseYwill be interpreted as the angular coordinates of thebasecurve.
For example, consider the data arrays:
const r = linspace(1, 1, 200); //[1,1,1, ..., 1]
const theta = linspace(0, 2*Math.PI, 200); //[0, 0.042, ..., 6.283]
//Assign the data to the linechart
line_chart.dataX(r).dataY(theta);
//Don't forget to render the new state.
my_graph.draw();
polar(false) |
polar(true) |
|---|---|
![]() |
![]() |
When the polar property is set to false the area is shows a triengle but the data is correctly interpreted as a full circle of radius one if the polar property is set as true.
Returns:
- A boolean that represents the area
polarproperty value if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
The default value for the area polar property is false.
Area Properties
Color:
This method lets you set or get the color of the area.
Method:
color()
color(option)
color(option, callback)
Where:
-
option: is a string representing the color of the area. -
callback: is a function that is run after the areacoloris set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Returns:
- A string that represents the area
colorif no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
The default value for the area color is "#0043e0".
Opacity:
This method lets you set or get the opacity of the area.
Method:
opacity()
opacity(option)
opacity(option, callback)
Where:
-
opacity: is a number representing the opacity of the area. -
callback: is a function that is run after the areaopacityis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The opacity must be a number between 0 and 1.
Returns:
- A number that represents the area
opacityif no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
The default value for the area opacity is 0.3.
Heatmap
The heatmap dataset is used to represent two dimensional functions or scalar fields. The field values is represented as a color.
The datapoints in this dataset are represented by three arrays, two of which are used to determine the location of the datapoint, these are call the meshX and meshY arrays and the third stores the value itself, this is the data array.
These arrays have a specific structure: All three are two-dimensional matrices
const data = [
[1,2,3],
[4,5,6],
[7,8,9]
]
That is, and array that holds arrays of numbers. Each of the inner arrays can have different lengths as long that difference is present in meshX, meshY and data at the same location.
Note: The
meshX,meshYanddataarrays must have the same structure.
The datapoints are characterized with two indices (i, j) that represent the row and column of the matrices.
So the x coordinate of the datapoint is at meshX[i][j], the y coordinate at meshY[i][j] and its value is data[i][j].
The mesh, data and datapoint properties can be defined to be static or dynamic, meaning that its values can be static and never change or be computed on each draw and even be based on the values of other properties.
Example:
First, lets create a static heatmap graph.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisPosition("bottom-left")
.primaryGrid({
grid : {enable : false}
})
.secondaryGrid({
grid : {enable : false}
})
.containerSize({width : 400, height : 400})
.pointerMove();
//Creates the heatmap
const heat_map = my_graph.addDataset("heatmap");
//Creates the mesh
const x_positions = linspace(-4, 4, 100);
const y_positions = linspace(-4, 4, 100);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
//Creates the data
const values = [];
for(let i=0; i < x_mesh.length; i++){
values.push([]);
for(let j=0; j<x_mesh[i].length; j++){
const x = x_mesh[i][j];
const y = y_mesh[i][j];
const r = Math.hypot(x, y);
values[i].push(Math.cos(r));
}
}
//Add the data to the heatmap
heat_map.meshX(x_mesh).meshY(y_mesh).data(values);
//Finally draws the graph
my_graph.draw();
Result:
Note:
linspaceandmeshgridare utility functions, you can learn more about this functions on the extras section.
As shown, the heatmap position and values are static and remain unchanged.
Now let's make the mesh dynamic. To do that, we need to define each mesh component as a generator function.
The mesh generator function has the form:
generator()
generator(dataset)
generator(dataset, graph)
Where:
-
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
These two optional arguments always holds the dataset and graph latest state at the moment of the call.
Note: Unlike the callback that most methods accept, the generator function runs on every draw.
The mesh generator function must return the mesh values.
We will keep the last script up until the data is add to the heatmap.
//Add the data to the heatmap
heat_map.meshX((dataset, graph)=>{
//This function will run on every draw
//Inside this function, dataset makes reference to heat_map and
//graph to my_graph
const domain = graph.axisDomain();
const x_positions = linspace(domain.x.start+1, domain.x.end-1, 100);
const y_positions = linspace(domain.y.start+1, domain.y.end-1, 100);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
//The return value must be the x mesh component.
return x_mesh;
})
.meshY((dataset, graph)=>{
const domain = graph.axisDomain();
const x_positions = linspace(domain.x.start+1, domain.x.end-1, 100);
const y_positions = linspace(domain.y.start+1, domain.y.end-1, 100);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
//This time we return the y mesh component.
return y_mesh;
})
.data(values);
//Finally draws the graph
my_graph.draw();
Result:
Warning: You may only read static properties and data from dynamic mesh generators, this is because dynamic properties depend on the
xandymesh values, so calling those from a data generator will create a circular dependency and the program will stop.
Warning: You must not call the
meshX()otdraw()methods from thexmesh generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
meshY()otdraw()methods from theymesh generator because that will create a circular dependency and the program will stop.
Note: It is safe to call the
meshX()method from within theymesh generator and vice versa.
Now in this case, the mesh moves along with the axis and appears always at the center of the graph, but the heatmap values are still static.
To change that, the data values must be defined as a generator function as well. This function will be call once for each datapoint position and must return a value for that datapoint.
The heatmap data generator can accept some optional parameters that must be call in the next order:
generator(x, y, i, j, meshX, meshY, dataset, graph)
Where:
-
x: is thexposition of the datapoint. -
y: is theyposition of the datapoint. -
i: is theiindex corresponding to thexandyvalues. -
j: is thejindex corresponding to thexandyvalues. -
meshX: is the completemeshXarray. -
meshY: is the completemeshYarray. -
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
Note: All the arguments of the data generator function are optional.
Note: The data generator operates in a similar way of the array methods (map, forEach, etc).
In our example, instead of defining the data statically, we will use a generator:
heat_map.data((x, y)=>{
//This function will run on every draw
//In this case we only need the x and y positions.
const r = Math.hypot(x,y);
//Return the data value at each datapoint
return Math.cos(2*r);
})
//Finally draws the graph
my_graph.draw();
Result:
Warning: You may only read static properties from dynamic data generators, this is because dynamic properties depend on the mesh values, so calling those from a data generator will create a circular dependency and the program will stop.
Warning: You must not call the
data()ordraw()methods from the data generator because that will create a circular dependency and the program will stop.
As shown, the data moves along with the axis and mesh.
Finally, some properties can be dynamic too. The heatmap property generator function in a similar manner than the data generator.
The function will be called once for each datapoint and must return a suitable property value.
The heatmap property generator can accept some optional parameters that must be call in the next order:
generator(value, x, y, i, j, data, meshX, meshY, dataset, graph)
Where:
-
value: is the value of the datapoint. -
x: is thexposition of the datapoint. -
y: is theyposition of the datapoint. -
i: is theiindex corresponding to thexandyvalues. -
j: is thejindex corresponding to thexandyvalues. -
data: is the completedataarray. -
meshX: is the completemeshXarray. -
meshY: is the completemeshYarray. -
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
Note: All the arguments of the data generator function are optional.
Note: The data generator operates in a similar way of the array methodds (map, forEach, etc).
In the last script we can add:
heat_map.opacity((value, x, y)=>{
//This function will run on each draw
//In this case we only need the datapoint value
const opacity_map = mapping({ from:[-1,1], to:[0,1] });
//The generator function must return a suitable value.
return opacity_map.map(value);
});
//Draw the graph after any change
my_graph.draw();
Result:
Warning: You must not call the
draw()method from the property generator because that will create a circular dependency and the program will stop.
Note:
mappingis a utility function, you can learn more about this functions on the extras section.
ID:
This method lets you set or get the id string of the dataset.
Method:
id()
id(option)
id(option, callback)
Where:
-
option: is a string representing theidof the dataset. -
callback: is a function that is run after theidis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
idis used to identify the dataset, so the string must be unique among other datasets.
By default, the id is generated automatically to guarantee its uniqueness, so in the majority of cases it is best to leave it as it is.
Returns:
- A string representing the
idof the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Index:
This method lets you set or get the index of the dataset.
The index is used to determine the order in which the datasets will be drawn. The dataset with the lower index value will be drawn first and appear to be behind other datasets.
Method:
index()
index(option)
index(option, callback)
Where:
-
option: is a number representing theindexvalue of the dataset. -
callback: is a function that is run after theindexis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The draw order of datasets with the same
indexvalue will be indeterminate.
Returns:
- A number that represents the
indexof the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
Enable:
This method lets you set or get the enable status of the heatmap.
Method:
enable()
enable(option)
enable(option, callback)
Where:
-
option: is a boolean that determines whether theheatmapmust be enabled. -
callback: is a function that is run after the heatmapenableproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A bolean representing the
enablestatus of theheatmap. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enableproperty of theheatmapistrue.
Dataset Type:
This method returns the heatmap dataset type.
Method:
datasetType()
Returns:
- This method always return the string
"heatmap".
This is usefull when you read the dataset from the graph2D getDatasets() method or the restoreGraph() function.
Heatmap Data
The heatmap uses a set of three two-dimensional matrices, two of which are used to determine the location of the datapoint, these are call the meshX and meshY arrays and the third stores the value itself, this is the data array.
Each matrix must be defined independently.
Mesh X:
This method lets you set or get the heatmap meshX array.
Method:
meshX()
meshX(mesh)
meshX(mesh, callback)
Where:
-
mesh: represents thexpositions of theheatmapdatapoints. It can be one of the following options:- A two-dimensional matrix.
- A function that returns a two-dimensional matrix. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the mesh is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the mesh as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
meshX()nordraw()should be called from within the mesh function, because that will cause a circular dependency and the program will stop.
When the mesh is defined as a function, we say that is dynamically generated.
If the mesh is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the mesh function, it will not be detected and the data will not update.
Note: The
heatmapmesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
xcoordinate of the datapoints if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the mesh is defined as a function, this method will return a two-dimensional matrix of numbers.
Mesh Y:
This method lets you set or get the heatmap meshY array.
Method:
meshY()
meshY(mesh)
meshY(mesh, callback)
Where:
-
mesh: represents theypositions of theheatmapdatapoints. It can be one of the following options:- A two-dimensional matrix.
- A function that returns a two-dimensional matrix. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the mesh is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the mesh as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
meshY()nordraw()should be called from within the mesh function, because that will cause a circular dependency and the program will stop.
When the mesh is defined as a function, we say that is dynamically generated.
If the mesh is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the mesh function, it will not be detected and the data will not update.
Note: The
heatmapmesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
ycoordinate of the datapoints if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the mesh is defined as a function, this method will return a two-dimensional matrix of numbers.
Data:
This method lets you set or get the heatmap data array.
Method:
data()
data(data)
data(data, callback)
Where:
-
data: represents the values of theheatmapdatapoints. It can be one of the following options:- A two-dimensional matrix.
- A function that returns a two-dimensional matrix. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the data as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
data()nordraw()should be called from within the mesh function, because that will cause a circular dependency and the program will stop.
When the data is defined as a function, we say that is dynamically generated.
If the data is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the data will not update.
Note: The
heatmapdata generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the value of the datapoints if no arguments are pass.
- A reference to the dataset object from which the method is called upon.
Note: Even when the data is defined as a function, this method will return a two-dimensional matrix of numbers.
Axis Used:
This method lets you set or get the axis used by the heatmap.
Method:
axisUsed()
axisUsed(options)
axisUsed(options, callback)
Where:
-
options: is an object containing the following properties:-
x: a string representing thexaxis that the dataset is goin to use. It can be"primary"or"secondary". -
y: a string representing theyaxis that the dataset is goin to use. It can be"primary"or"secondary".
-
-
callback: is a function that is run after the axis used is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- An object representing the axis being used by the dataset if no arguments are pass.
- A reference to the dataset object from which the method is called upon.
Default Value:
The default values for the axis used property are as follow.
{
x : "primary",
y : "primary"
}
Heatmap Properties
Color:
This method lets you set or get the color of the heatmap.
Method:
color()
color(option)
color(option, callback)
Where:
-
option: represents thecolorat each datapoint. It can be one of the following options:- A string with the name of any predefined color map. The predefined color maps are:
-
"viridis". -
"plasma". -
"magma". -
"magnet". -
"inv_magnet". -
"fairy". -
"inv_fairy". -
"swamp". -
"inv_swamp". -
"fire". -
"royal". -
"hsv".
-
- A two-dimensional matrix of strings, each representing the
colorat each datapoint. - A generator function that returns a string representing the
colorat each datapoint.
- A string with the name of any predefined color map. The predefined color maps are:
-
callback: is a function that is run after thecoloris set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorstring can be in the format "#rrggbb" or be any of the standard color names.
Note: If the
coloris defined as a marix, its dimensions must be the same as the data arrays.
Note: The
heatmapproperty generator functions are discussed in deep here.
If the color is defined as one of the predefined color maps, it will be considered dynamic. In this case, the color map range will be calculated automatically on each draw to accommodate the data.
This may be misleading if the data range changes constantly.
Returns:
- A two-dimensional matrix of strings representing the
colorat each datapoint if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be a matrix even if the
coloris defined as a function.
Default Value:
- The default value for
heatmapcoloris"viridis".
Opacity:
This method lets you set or get the opacity of the heatmap.
Method:
opacity()
opacity(option)
opacity(option, callback)
Where:
-
option: represents theopacityof theheatmap. It can be one of the following options:- A number that represents the
opacityof all datapoints. - A two-dimensional matrix of numbers, each representing the
opacityat each datapoint. - A generator function that returns a number representing the
opacityat each datapoint.
- A number that represents the
-
callback: is a function that is run after theopacityis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
opacityvalues must be a number between 0 and 1.
Note: If the
opacityis defined as a matrix, its dimensions must be the same as the data arrays.
Note: The
heatmapproperty generator functions are discussed in deep here.
Returns:
- A number or a two-dimensional matrix of numbers representing the
opacityof theheatmapif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be a matrix even if the
opacityis defined as a function.
Default Value:
- The default value for
heatmapopacityis1.
Smooth:
This method lets you set or get the smooth property of the heatmap.
If set, the heatmap will perform a pixel-wise bi-linear interpolation of the datapoint properties, resulting in a clearer, less pixelated image.
The accuracy of the final image will depend on the density of datapoints.
Warning: This is a complex computation, so it may harm the responsivity of the application. If you plan to use this feature, is best to avoid contant redraws.
Method:
smooth()
smooth(option)
smooth(option, callback)
Where:
-
option: is a boolean that determines whether theheatmapshould besmooth. -
callback: is a function that is run after thesmoothproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Example:
smooth(false) |
smooth(true) |
|---|---|
![]() |
![]() |
Returns:
- A boolean that represents the
smoothproperty if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for
smoothproperty isfalse.
Vector Field:
The vectorfield is used to create, as the name implies, two-dimensional vector field graphs. It can calso be used to draw single vectors or arrows.
The datapoints in this dataset are represented by four arrays, two of which are used to determine the location of the datapoint, these are call the meshX and meshY arrays and the other two stores the data values, these are the dataX and dataY arrays.
These arrays have a specific structure: All four are two-dimensional matrices
const data = [
[1,2,3],
[4,5,6],
[7,8,9]
]
That is, and array that holds arrays of numbers. Each of the inner arrays can have different lengths as long that difference is present in meshX, meshY, dataX and dataY at the same location.
Note: The
meshX,meshY,dataXanddataYarrays must have the same structure.
The datapoints are characterized with two indices (i, j) that represent the row and column of the matrices.
So the x coordinate of the datapoint is at meshX[i][j], the y coordinate at meshY[i][j], the x component of the vector is at dataX[i][j] and the y component at dataY[i][j].
The mesh, data and datapoint properties can be defined to be static or dynamic, meaning that its values can be static and never change or be computed on each draw and even be based on the values of other properties.
Example:
First, lest create a static vectorfield graph.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisDomain({
x : {start : -3, end : 3},
y : {start : -3, end : 3}
})
.containerSize({width:400, height:400})
.pointerMove();
//Creates the vectorfield
const vector_field = my_graph.addDataset("vectorfield");
//Creates the mesh
const x_positions = linspace(-3, 3, 23);
const y_positions = linspace(-3, 3, 23);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
//Creates the data
const x_values = [];
const y_values = [];
for(let i=0; i < x_mesh.length; i++){
x_values.push([]);
y_values.push([]);
for(let j=0; j < x_mesh[i].length; j++){
const x = x_mesh[i][j];
const y = y_mesh[i][j];
x_values[i].push(Math.sin(x + y));
y_values[i].push(Math.cos(x - y));
}
}
//Add the data to the vectorfield
vector_field.meshX(x_mesh)
.meshY(y_mesh)
.dataX(x_values)
.dataY(y_values);
//Finally draws the graph
my_graph.draw();
Result:
Note:
linspaceandmeshgridare utility functions, you can learn more about this functions on the extras section.
As shown, the vectorfield position and values are static and remain unchanged.
Now let's make the mesh dynamic. To do that, we need to define each mesh component as a generator function.
The mesh generator function has the form:
generator()
generator(dataset)
generator(dataset, graph)
Where:
-
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
These two optional arguments always holds the dataset and graph latest state at the moment of the call.
Note: Unlike the callback that most methods accept, the generator function runs on every draw.
The mesh generator function must return the mesh values.
We will keep the last script up until the data is add to the vectorfield.
//Add the data to the vectorfield
vector_field.meshX((dataset, graph)=>{
//This function will run on every draw
//Inside this function, dataset makes reference to vector_field and
//graph to my_graph
const domain = graph.axisDomain();
const x_positions = linspace(domain.x.start, domain.x.end, 25);
const y_positions = linspace(domain.y.start, domain.y.end, 25);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
//The return value must be the x mesh component.
return x_mesh;
})
meshY((dataset, graph)=>{
const domain = graph.axisDomain();
const x_positions = linspace(domain.x.start, domain.x.end, 25);
const y_positions = linspace(domain.y.start, domain.y.end, 25);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
//This time we return the y mesh component.
return y_mesh;
})
.dataX(x_values)
.dataY(y_values)
//Finally draws the graph
my_graph.draw();
As shown, the mesX and meshY generator function are very similar, so we can condense them into a single function.
//Creates the mesh generator function
function getMesh(graph){
const domain = graph.axisDomain();
const x_positions = linspace(domain.x.start, domain.x.end, 23);
const y_positions = linspace(domain.y.start, domain.y.end, 23);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
return {x_mesh, y_mesh};
}
//Add the data to the vectorfield
vector_field.meshX((dataset, graph)=>getMesh(graph).x_mesh)
meshY((dataset, graph)=>getMesh(graph).y_mesh)
.dataX(x_values)
.dataY(y_values)
//Finally draws the graph
my_graph.draw();
Result:
Warning: You may only read static properties and data from dynamic mesh generators, this is because dynamic properties depend on the
xandymesh values, so calling those from a data generator will create a circular dependency and the program will stop.
Warning: You must not call the
meshX()otdraw()methods from thexmesh generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
meshY()otdraw()methods from theymesh generator because that will create a circular dependency and the program will stop.
Note: It is safe to call the
meshX()method from within theymesh generator and vice versa.
Now in this case, the mesh moves along with the axis and appears always at the center of the graph, but the heatmap values are still static.
To change that, the data values must be defined as a generator function as well. This function will be call once for each datapoint position and must return a value for that datapoint.
The vectorfield data generator can accept some optional parameters that must be call in the next order:
generator(x, y, i, j, meshX, meshY, dataset, graph)
Where:
-
x: is thexposition of the datapoint. -
y: is theyposition of the datapoint. -
i: is theiindex corresponding to thexandyvalues. -
j: is thejindex corresponding to thexandyvalues. -
meshX: is the completemeshXarray. -
meshY: is the completemeshYarray. -
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
Note: All the arguments of the data generator function are optional.
Note: The data generator operates in a similar way of the array methods (map, forEach, etc).
In our example, instead of defining the data statically, we will use a generator:
//vector_field.dataX((x,y)=>{
//This function will run on every draw
//In this case we only need the x and y positions.
const x_value = Math.sin(x + y);
//The generator must return the x component of the vector
return x_value;
})
.dataY((x,y)=>{
const y_value = Math.cos(x - y);
//In this case the return value will be the y component of the vector
return y_value;
})
//Finally draws the graph
my_graph.draw();
Result:
Warning: You may only read static properties from dynamic data generators, this is because dynamic properties depend on the mesh values, so calling those from a data generator will create a circular dependency and the program will stop.
Warning: You must not call the
dataX()ordraw()methods from the x data generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
dataY()ordraw()methods from the y data generator because that will create a circular dependency and the program will stop.
As shown, the data now moves along with the axis and mesh.
But there is a catch, the vectors appear to change size and rotate as the graph moves. This is because the mesh positions remain constant relative to the graph rect, but no to the axis.
To fix that we need to make shure the mesh values always land on the same position relative to the axis.
The getMesh will be replaced by:
//Creates the mesh generator function
function getMesh(graph){
const domain = graph.axisDomain();
const delta = 0.25;
const x_positions = [];
const y_positions = [];
let x_current = Math.floor(domain.x.start);
while(x_current < Math.ceil(domain.x.end + delta)){
x_positions.push(x_current);
x_current += delta;
}
let y_current = Math.floor(domain.y.start);
while(y_current < Math.ceil(domain.y.end + delta)){
y_positions.push(y_current);
y_current += delta;
}
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
return {x_mesh, y_mesh};
}
Result:
Finally, some properties can be dynamic too. The vectorfield property generator function in a similar manner than the data generator.
The function will be called once for each datapoint and must return a suitable property value.
The vectorfield property generator can accept some optional parameters that must be call in the next order:
generator(vectorX, vectorY, x, y, i, j, dataX, dataY, meshX, meshY, dataset, graph)
Where:
-
vectorX: is the value of the datapointxcomponent. -
vectorY: is the value of the datapointycomponent. -
x: is thexposition of the datapoint. -
y: is theyposition of the datapoint. -
i: is theiindex corresponding to thexandyvalues. -
j: is thejindex corresponding to thexandyvalues. -
dataX: is the completedataXarray. -
dataY: is the completedataYarray. -
meshX: is the completemeshXarray. -
meshY: is the completemeshYarray. -
dataset: is a reference to the dataset from which the function is call upon. -
graph: is a reference of the graph that the dataset is bound to.
Note: All the arguments of the data generator function are optional.
Note: The data generator operates in a similar way of the array methodds (map, forEach, etc).
In the last script we can add:
//Create a colormap´
const color = colorMap({
from : -Math.PI,
to : Math.PI,
type : "royal"
});
vector_field.color((x_value, y_value)=>{
//In this case we can only need the x and y vector values;
const angle = Math.atan2(y, x);
//The return value will be assigned to the corresponding datapoint
return color(angle);
});
my_graph.draw();
Note:
colorMapis a utility function, you can learn more about this functions on the extras section.
ID:
This method lets you set or get the id string of the dataset.
Method:
id()
id(option)
id(option, callback)
Where:
-
option: is a string representing theidof the dataset. -
callback: is a function that is run after theidis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
idis used to identify the dataset, so the string must be unique among other datasets.
By default, the id is generated automatically to guarantee its uniqueness, so in the majority of cases it is best to leave it as it is.
Returns:
- A string representing the
idof the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Index:
This method lets you set or get the index of the dataset.
The index is used to determine the order in which the datasets will be drawn. The dataset with the lower index value will be drawn first and appear to be behind other datasets.
Method:
index()
index(option)
index(option, callback)
Where:
-
option: is a number representing theindexvalue of the dataset. -
callback: is a function that is run after theindexis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The draw order of datasets with the same
indexvalue will be indeterminate.
Returns:
- A number that represents the
indexof the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
Enable:
This method lets you set or get the enable status of the vectorfield.
Method:
enable()
enable(option)
enable(option, callback)
Where:
-
option: is a boolean that determines whether theheatmapmust be enabled. -
callback: is a function that is run after the heatmapenableproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A bolean representing the
enablestatus of thevectorfield. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enableproperty of thevectorfieldistrue.
Dataset Type:
This method returns the vectorfield dataset type.
Method:
datasetType()
Returns:
- This method always return the string
"vectorfield".
This is usefull when you read the dataset from the graph2D getDatasets() method or the restoreGraph() function.
Vector Field Data
The vectorfield uses a set of four two-dimensional matrices, two of which are used to determine the location of the datapoint, these are call the meshX and meshY arrays and the last two stores the data values. these are the dataX and dataY arrays.
Each matrix must be defined independently.
Mesh X:
This method lets you set or get the vectorfield meshX array.
Method:
meshX()
meshX(mesh)
meshX(mesh, callback)
Where:
-
mesh: represents thexpositions of thevectorfielddatapoints. It can be one of the following options:- A two-dimensional matrix.
- A function that returns a two-dimensional matrix. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the mesh is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the mesh as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
meshX()nordraw()should be called from within the mesh function, because that will cause a circular dependency and the program will stop.
When the mesh is defined as a function, we say that is dynamically generated.
If the mesh is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the mesh function, it will not be detected and the data will not update.
Note: The
vectorfieldmesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
xcoordinate of the datapoints if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the mesh is defined as a function, this method will return a two-dimensional matrix of numbers.
Mesh Y:
This method lets you set or get the vectorfield meshY array.
Method:
meshY()
meshY(mesh)
meshY(mesh, callback)
Where:
-
mesh: represents theypositions of thevectorfielddatapoints. It can be one of the following options:- A two-dimensional matrix.
- A function that returns a two-dimensional matrix. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the mesh is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the mesh as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
meshY()nordraw()should be called from within the mesh function, because that will cause a circular dependency and the program will stop.
When the mesh is defined as a function, we say that is dynamically generated.
If the mesh is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the mesh function, it will not be detected and the data will not update.
Note: The
vectorfieldmesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
ycoordinate of the datapoints if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Note: Even when the mesh is defined as a function, this method will return a two-dimensional matrix of numbers.
Data X:
This method lets you set or get the vectorfield dataX array.
Method:
dataX()
dataX(data)
dataX(data, callback)
Where:
-
data: represents the values of thevectorfielddatapoints. It can be one of the following options:- A two-dimensional matrix.
- A function that returns a two-dimensional matrix. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the data as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
dataX()nordraw()should be called from within the mesh function, because that will cause a circular dependency and the program will stop.
When the data is defined as a function, we say that is dynamically generated.
If the data is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the data will not update.
Note: The
vectorfielddata generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the value of the datapoints if no arguments are pass.
- A reference to the dataset object from which the method is called upon.
Note: Even when the data is defined as a function, this method will return a two-dimensional matrix of numbers.
Data Y:
This method lets you set or get the vectorfield dataY array.
Method:
dataY()
dataY(data)
dataY(data, callback)
Where:
-
data: represents the values of thevectorfielddatapoints. It can be one of the following options:- A two-dimensional matrix.
- A function that returns a two-dimensional matrix. This function can accept two optional parameters in the following order:
- An object that represents the dataset from which the method is called upon.
- An object that represents the graph object that the dataset is bound to.
-
callback: is a function that is run after the data is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: Unlike the callback, If you define the data as a function, that function will be executed on each subsequent draw and the arguments will hold the latest state of the dataset and graph at the moment of the call.
Warning: Neither
dataY()nordraw()should be called from within the mesh function, because that will cause a circular dependency and the program will stop.
When the data is defined as a function, we say that is dynamically generated.
If the data is dynamically generated, its values are updated on each draw. It is important to note that the draw() method only updates the graph if a change is made to it or any of its assets, so if you change an external value, even if that value is used inside the data function, it will not be detected and the data will not update.
Note: The
vectorfielddata generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the value of the datapoints if no arguments are pass.
- A reference to the dataset object from which the method is called upon.
Note: Even when the data is defined as a function, this method will return a two-dimensional matrix of numbers.
Axis Used:
This method lets you set or get the axis used by the vectorfield.
Method:
axisUsed()
axisUsed(options)
axisUsed(options, callback)
Where:
-
options: is an object containing the following properties:-
x: a string representing thexaxis that the dataset is goin to use. It can be"primary"or"secondary". -
y: a string representing theyaxis that the dataset is goin to use. It can be"primary"or"secondary".
-
-
callback: is a function that is run after the axis used is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- An object representing the axis being used by the dataset if no arguments are pass.
- A reference to the dataset object from which the method is called upon.
Default Value:
The default values for the axis used property are as follow.
{
x : "primary",
y : "primary"
}
Vector Field Properties
Color:
This methd lets you set or get the color of the vectorfield.
Method:
color()
color(option)
color(option, callback)
Where:
-
color: represent thecolorof thevectorfield. It can be one of the following options:- A string that represents the
colorof all datapoints. - A two-dimensional matrix of strings, each representing the
colorat each datapoint. - A generator function that returns a string representing the
colorat each datapoint.
- A string that represents the
-
callback: is a function that is run after thecoloris set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: If the
coloris defined as a marix, its dimensions must be the same as the data arrays.
Note: The
vectorfieldproperty generator functions are discussed in deep here.
Returns:
- A string or a two-dimensional matrix of strings representing the
colorat each datapoint if no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be a matrix even if the
coloris defined as a function.
Default Value:
- The default value for
vectorfieldcoloris"#303030".
Opacity:
This method lets you set or get the opacity of the vectorfield.
Method:
opacity()
opacity(option)
opacity(option, callback)
Where:
-
option: represents theopacityof thevectorfield. It can be one of the following options:- A number that represents the
opacityof all datapoints. - A two-dimensional matrix of numbers, each representing the
opacityat each datapoint. - A generator function that returns a number representing the
opacityat each datapoint.
- A number that represents the
-
callback: is a function that is run after theopacityis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
opacityvalues must be a number between 0 and 1.
Note: If the
opacityis defined as a matrix, its dimensions must be the same as the data arrays.
Note: The
vectorfieldproperty generator functions are discussed in deep here.
Returns:
- A number or a two-dimensional matrix of numbers representing the
opacityof thevectorfieldif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be a matrix even if the
opacityis defined as a function.
Default Value:
- The default value for
vectorfieldopacityis1.
Style:
This method lets you set or get the style of the vectorfield.
Method:
style()
style(option)
style(option, callback)
Where:
-
option: represents thestyleof thevectorfield. It can be one of the following options:- A string that represents the
styleof all datapoints. - A two-dimensional matrix of strings, each representing the
styleat each datapoint. - A generator function that returns a string representing the
styleat each datapoint.
- A string that represents the
-
callback: is a function that is run after thestyleis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
styleis defined as an array, its length must be the same as the data arrays.
Note: The
vectorfieldproperty generator functions are discussed in deep here.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- A string or a two-dimensional matrix of strings representing the
styleof thevectorfieldif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be a matrix even if the
styleis defined as a function.
Default Value:
- The default value for
vectorfieldopacityis1.
Width:
This method lets you set or get the line width of the vectorfield.
Method:
width()
width(option)
width(option, callback)
Where:
-
option: represents the linewidthof thevectorfield. It can be one of the following options:- A number that represents the line
widthof all datapoints. - A two-dimensional matrix of numbers, each representing the line
widthat each datapoint. - A generator function that returns a number representing the line
widthat each datapoint.
- A number that represents the line
-
callback: is a function that is run after thewidthis set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
widthvalues must be positive integer.
Note: If the
widthis defined as a matrix, its dimensions must be the same as the data arrays.
Note: The
vectorfieldproperty generator functions are discussed in deep here.
Returns:
- A number or a two-dimensional matrix of numbers representing the
widthof thevectorfieldif no argument is pass. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be a matrix even if the
widthis defined as a function.
Default Value:
- The default value for
vectorfieldwidthis1.
Normalize:
This method lets you set or get the vectorfield normalize property.
If set, all vectors length will be linearly scale to ensure no one is bigger than maxLength.
Note: This doesn't affect the
dataXnordataYvalues, only its visual representation.
This facilitates the visualization at the expense of data accuracy.
Method:
normalize()
normalize(option)
normalize(option, callback)
Where:
-
option: is a boolean that determines whether thenormalizeproperty should be enable. -
callback: is a function that is run after thenormalizeproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Example:
normalize(false) |
normalize(true) |
|---|---|
![]() |
![]() |
Returns:
- A boolean that represents the
normalizeproperty if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for
vectorfieldnormalizeistrue.
Max Lenght
This method lets you set or get the maxLenght property.
This property represents the max lenght in pixels that any given vector can have.
Method:
maxLength()
maxLength(option)
maxLength(option, callback)
Where:
-
option: is a number that represents themaxLengthproperty value. -
callback: is a function that is run after thenormalizeproperty is set but before the next render, this callback accept two optional arguments that represents the dataset object from which the method is called upon and the graph object that is bound to, in that order.
Note: This is only relevant when the
normalizeproperty is set totrue.
Returns:
- A number that represents the
maxLengthproperty if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for
vectorfieldmaxLengthis20.
Colorbar
The colorbar is a visual color scale used primarily with the heatmap datasets, but can be used by itself.
It displays a horizontal or vertical bar with a color gradient in it and marks with the values that those colors represent.
For example, consider the following graph.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisDomain({
x : {start : -3, end : 3},
y : {start : -3, end : 3}
})
.axisPosition("bottom-left")
.containerSize({width:450, height:400});
//Creates a heatmap
const heat_map = my_graph.addDataset("heatmap");
function getMesh(graph){
const domain = graph.axisDomain();
const x_positions = linspace(domain.x.start, domain.x.end, 100);
const y_positions = linspace(domain.y.start, domain.y.end, 100);
const [x_mesh, y_mesh] = meshgrid(x_positions, y_positions);
return {x_mesh, y_mesh};
}
function peaks(x, y){
return 3*(1-x)**2 * Math.exp(-(x**2)-(y+1)**2) - 10*(x/5-x**3-y**5) * Math.exp(-(x**2)-(y**2)) - 1/3*Math.exp(-((x+1)**2) - (y**2));
}
//Add the data
heat_map.meshX((dataset, graph)=>getMesh(graph).x_mesh)
.meshY((dataset, graph)=>getMesh(graph).y_mesh)
.data((x,y)=>peaks(x,y))
//Finally, draws the graph.
my_graph.draw();
Result:
To add a color bar to this graph, it's as simple as adding the next code before drawing the graph.
//Creates the colorbar
const color_bar = my_graph.addColorbar();
//Binds the colorbar to the heatmap previously created.
color_bar.data(heat_map.id());
//Because in the default configuration, the margins are
//changed in order to accommodate the colorbar, and we don't want the
//graph to be distorted.
my_graph.aspectRatio({anchor:0}).draw();
Result:
A title can be easily be added.
color_bar.title({text : "Peaks Function"});
my_graph.aspectRatio({anchor:0}).draw();
Result:
You can change the colorbar position, orientation, font, etc.
It can also be used by itself, without the need to bind it to a heatmap, by setting the colors, position and labels by manually.
ID:
This method lets you set or get the id string of the colorbar.
Method:
id()
id(option)
id(option, callback)
Where:
-
option: is a string representing theidof thecolorbar. -
callback: is a function that is run after theidis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
idis used to identify the colorbar, so the string must be unique among other colorbars.
By default, the id is generated automatically to guarantee its uniqueness, so in the majority of cases it is best to leave it as it is.
Returns:
- A string representing the
idof thecolorbarif no arguments are pass. - A reference to the
colorbarobject from which the method is called upon.
Enable:
This method lets you set or get the enable status of the colorbar.
Method:
enable()
enable(option)
enable(option, callback)
Where:
-
option: is a boolean that determines whether thecolorbarmust be enabled. -
callback: is a function that is run after the colorbarenableproperty is set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A bolean representing the
enablestatus of thecolorbar. - A reference to the colorbar object from which the method is called upon.
Default Value:
- The default values for the
enableproperty of thecolorbaristrue.
Data:
This method lets you set or get the colorbar data.
Method:
data()
data(options)
data(options, callback)
Where:
-
options: represent the data of thecolorbar. It can be one of the following options:- The id string of a
heatmapdataset. - An array of objects, each of those objects represent one mark in the color bar and must have the following properties:
-
color: a string representing the color of the mark. -
label: a string with the label of the mark. -
position: a number represinting the position of the mark in thecolorbar.
-
- The id string of a
-
callback: is a function that is run after thedatais set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
positionproperty must be a number between 0 and 1, being 0 the start of the color bar, and 1 the end.
Note: The
colorstring can be in the format"#rrggbb"or be any of the standard color names.
In case of using an id string, this must belong to a heatmap dataset, and the colorbar will use the entire range of data.
Returns:
- An object with the
color,labelandpositionof each mark in thecolorbarif no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Value:
- There is no default value, if the data is not set, the
colorbarwill be a blank rectangle.
Position:
This method lets you set or get the colorbar position.
Method:
position()
position(option)
position(option, callback)
Where:
-
option: represents thecolobarposition. It can be one of the following options:- A string with one of the predefined positions:
-
"x-start"will position thecolorbarat the left. -
"x-end"will position thecolorbarat the right. -
"y-start"will position thecolorbarat the bottom. -
"y-end"will position thecolorbarat the top.
-
- An object with the following properties to position the
colorbarmanually:-
x: a number with thecolorbarxcoordinate. -
y: a number with thecolorbarycoordinate. -
orientation: a string with thecolorbarorientation. It can be"vertial"or"horizontal".
-
- A string with one of the predefined positions:
-
callback: is a function that is run after thepositionis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: If the
positionis set to one of the predefined positions, the graphmarginswill be changed to accommodate thecolorbar. This will potentially change the axis aspect ratio.
Note: The
xandyvalues represent the coordinates of thecolorbartop left corner relative to the client rect.
Returns:
- A string representing the
colorbarpositionif no argument is pass. - A reference to the colorbar object from which the method is called upon.
Note: The returned string will be one of the predefined positions or
"floating"if was manually defined.
Default Value:
- The default values for the
colorbarpositionis"x-end".
Ticks:
This method lets you set or get the colorbar ticks properties.
Method:
ticks()
ticks(options)
ticks(options, callback)
Where:
-
options: is an object with the folowing properties:-
density: represents the number of ticks in thecolorbar. It can be one of the following options:- A number representing the number of ticks.
- An array of strings, the length of the array will be the number of ticks and the strings the label of each one.
- An array of objects, each object with the following properties:
-
label: a string representing the label of the tick. -
position: a number representing the position of the tick.
-
-
color: a string representing the color of the tick. -
opacity: a number representing the opacity of the tick. -
style: a string representing the style of the tick. -
width: a number representing the line width of the tick.
-
-
callback: is a function that is run after theticksvalues are set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
densityproperty has no effect if thedatais defined manually.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: The
opacitymust be a number between 0 and 1.
Note: The
widthmust be a positive integer.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- An object with the
ticksproperties if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
The default values for the properties of the ticks method are as follow:
{
density : 6,
color : "#000000",
opacity : 1,
style : "solid",
width : 1
}
Reverse:
This method lets you set or get the colorbar reverse property.
By default, the colorbar shows the values in ascending order starting from the bottom. If the reverse property is set to true, the values will be shown in descending order.
Method:
reverse()
reverse(option)
reverse(option, callback)
Where:
-
option: is a boolean that determines whether thecolorbarshouldreversethe data. -
callback: is a function that is run after thereverseproperty is set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A bollean that represents the ``colorbar
reverse` property if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
- The default values for the
reverseproperty isfalse.
Width:
This method lets you set or get the width of the colorbar.
This value affects only the width of the colorbar itself, and not the tick labels or title.
Method:
width()
width(option)
width(option, callback)
Where:
-
option: is a number that represents the width of thecolorbar. -
callback: is a function that is run after thewidthis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The width is represented in pixels, so it must be a positive integer.
Note: If the
colorbaris inhorizontalposition, this values will make reference to the colorbar height instead
Returns:
- A number that represents the
colorbarwidthif no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
- The default value for the
widthproperty is20.
Size:
This method lets you set or get the size of the colorbar.
This value affects the colorbar height if is in vertical orientation, and the width if is horizontal.
Method:
size()
size(option)
size(option, callback)
Where:
-
option: is a number representing thecolorbarsize. -
callback: is a function that is run after thesizeis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
sizemust be a number between 0 and 1.
If the colorbar is in vertical orientation, the size value is relative to the client rect height, being 1 equal to 100% of the client rect height.
If is in horizontal orientation, the value is relative to the cient rect width.
Returns:
- A number that represents the
colorbarsizeif no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
- The default value for the
sizeproperty is0.93.
Opacity:
This method lets you set or get the opacity of the colorbar.
This value only affects the bar itself and not other elements.
Method:
opacity()
opacity(option)
opacity(option, callback)
Where:
-
option: is a number that represents thecolorbaropacity. -
callback: is a function that is run after theopacityis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
opacitymust be a number between 0 and 1.
Returns:
- A number that represents the
colorbaropacityif no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
- The default value for the
opacityproperty is1.
Border:
This method lets you set or get the border properties of the colorbar.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
border()
border(options)
border(options, callback)
Where:
-
options: is an object with the following properties:-
color: a string representing the color of the border. -
opacity: a number representing the opacity of the border. -
style: a string representing the style of the border. -
width: a number representing the line width of the border.
-
-
callback: is a function that is run after theborderproperties are set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: The
opacitymust be a number between 0 and 1.
Note: The
widthmust be a positive integer.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- An object with the
borderproperties if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
The default values for the properties of the border method are as follow:
{
color : "#000000",
opacity : 1,
style : "solid",
width : 1
}
Label:
This method lets you set or get the label properties of the colorbar.
The values set with this method will only affect the text on each tick and not the title.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
label()
label(options)
label(options, callback)
Where:
-
options: is an object containing the following properties:-
font: a string representing the font of thelabel. -
size: a string representing the size of thelabel. -
specifier: a string representing the font specifier of thelabel. -
color: a string representing the color of thelabel. -
opacity: a number between 0 and 1 representing the opacity of thelabel. -
position: a string representing the positioning of thelabel. It can be one of the following values:-
"start". -
"end".
-
-
-
callback: is a function that is run after thelabelis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
opacitymust be a number between 0 and 1
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
The position property has different meaning depending upon the colorbar orientation.
If the colorbar is vertical:
-
"start"will position the labels at the left of thecolorbar. -
"end"will position the labels at the right of thecolorbar.
If the colorbar is horizontal:
-
"start"will position the labels at the bottom of thecolorbar. -
"end"will position the labels at the top of thecolorbar.
Returns:
- An object with the
labelproperties if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
The default values for the properties of the label method are as follow:
{
color : "#000000",
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "10px",
specifier : "",
opacity : 1,
position : "end"
}
Title:
This method lets you set or get the title properties of the colorbar.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
title()
title(options)
title(options, callback)
Where:
-
options: is an object containing the following properties:-
text: a string representing the text of thetitle. -
font: a string representing the font of thetitle. -
size: a string representing the size of thetitle. -
specifier: a string representing the font specifier of thetitle. -
color: a string representing the color of thetitle. -
opacity: a number between 0 and 1 representing the opacity of thetitle. -
position: a string representing the positioning of thetitle. It can be one of the following values:-
"start". -
"end".
-
-
reverse: a boolean that determines whether the text should be reversed.
-
-
callback: is a function that is run after thetitleis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
opacitymust be a number between 0 and 1
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
The position property has different meaning depending upon the colorbar orientation.
If the colorbar is vertical:
-
"start"will position the title at the left of thecolorbar. -
"end"will position the title at the right of thecolorbar.
If the colorbar is horizontal:
-
"start"will position the title at the bottom of thecolorbar. -
"end"will position the title at the top of thecolorbar.
By default, the title text is read from bottom to top, the reverse property will reverse that orientation.
Note: The
reverseproperty is only releventa when thecolorbaris inverticalorientation.
Returns:
- An object with the
titleproperties if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
The default values for the properties of the title method are as follow:
{
text : "",
color : "#000000",
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "12px",
specifier : "",
opacity : 1,
position : "end",
reverse : false
}
Text:
This method let you set or get the shared text properties from the label and text.
The values set by this method will affect equally the colorbar labels and title.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
text()
text(options)
text(options, callback)
Where:
-
options: is an object containing the following properties:-
font: a string representing the font of bothtext. -
size: a string representing the size of bothtext. -
specifier: a string representing the font specifier of bothtext. -
color: a string representing the color of bothtext. -
opacity: a number between 0 and 1 representing the opacity of bothtext. -
position: a string representing the positioning of bothtext. It can be one of the following values:-
"start". -
"end".
-
-
-
callback: is a function that is run after thetextproperties are set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The
opacitymust be a number between 0 and 1
Note: The
fontvalue can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
sizevalue can be any valid css size including em, rem, etc.
Note: The
specifiercan be any combination of the following css font properties.
- font-style.
- font-variant.
- font-weight.
- font-stretch.
- line-height.
Some fonts may not support all
specifieroptions.
The position property has different meaning depending upon the colorbar orientation.
If the colorbar is vertical:
-
"start"will position the text at the left of thecolorbar. -
"end"will position the text at the right of thecolorbar.
If the colorbar is horizontal:
-
"start"will position the text at the bottom of thecolorbar. -
"end"will position the text at the top of thecolorbar.
Returns:
- An object with the
textproperties if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
The default values for the properties of the text method are as follows:
{
label : {
color : "#000000",
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "10px",
specifier : "",
opacity : 1,
position : "end"
},
title : {
color : "#000000",
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "12px",
specifier : "",
opacity : 1,
position : "end"
}
}
Unit:
This method lets you set or get the unit of the colorbar
The unit is a string that is appended to each label at the right.
Method:
unit()
unit(option)
unit(option, callback)
Where:
-
option: is a string representing thecolorbarunit. -
callback: is a function that is run after theunitis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A string representing the
colorbarunitif no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Value:
- The default value for the
unitproperty is"".
Metrics:
This method returns the metrics of the colorbar.
Method:
metrics()
Returns:
- An object with the following properties:
-
x: Thexcoordinate of thecolorbar. -
y: Theycoordinate of thecolorbar. -
width: Thewidthof thecolorbar. -
height: Theheightof thecolorbar.
-
Note: The
xandycoordinates make reference to the top left corner of the wholecolorbar, including the labels and title.
Note: The
xandycoordinates are relatives to theclient rect.
Note: The
widthandheightvalues make reference to the width and height of the wholecolorbar, including the labels and title.
Legend
The legends are little banners, render at the top of all other information. It shows a legend for each dataset and a little marker to help to identify that dataset.
Each dataset type have a distinct marker, and that marker uses the same styling as the represented dataset.
//Gets the container div element
const element = document.querrySelector("#my-graph");
//Creates and customize the graph object
const my_graph = graph2D(element);
my_graph.axisDomain({
x : {start : -12, end : 12},
y : {start : -0.3, end : 1.1}
})
.containerSize({width : 800, height : 300})
.pointerMove();
//Creates a simple linechart
const line_chart_1 = my_graph.addDataset("linechart");
const data_x_1 = linspace(-12, 12, 150);
const data_y_1 = data_x_1.map(x => Math.sin(x)/x);
line_chart_1.dataX(data_x_1).dataY(data_y_1);
//Now creates the legend
const legend = my_graph.addLegend();
//Adds the datasets to the legend
legend.data([
{dataset : line_chart_1.id(), text : "sin(x)/x"}
]);
//Finally draws the graph
my_graph.draw();
Result:
As shown, the legend appear at the top right corner of the graph and contain only the linechart mark and label.
We can add as many datasets as we need on the legend. For example:
//Create another linechart
const line_chart_2 = my_graph.addDataset("linechart");
const data_x_2 = linspace(1e-5, 12, 100);
const data_y_2 = data_x_2.map(x => 1/x);
line_chart_2.dataX(data_x_2).dataY(data_y_2).lineColor("#a000eb");
//And adds both datasets to the legend
legend.data([
{dataset : line_chart_1.id(), text : "sin(x)/x"},
{dataset : line_chart_2.id(), text : "1/x"}
]);
//Draws the graph
my_graph.draw();
Result
A disabled dataset will not appear in a legend even if is part of its data.
Note: A
linechartwill not appear in a legend if both the line and markers are disabled. The error bars are not shown in the legend.
Each dataset type has a unique marker.
| Type | Marker | Type | Marker |
|---|---|---|---|
"linechart" |
![]() |
"linechart" |
![]() |
"area" |
![]() |
"heatmap" |
![]() |
"vectorfield" |
![]() |
The marker takes the color and style from the corresponding dataset.
Note: If the dataset has a dynamic property, the marker will take the first value of that property.
ID:
This method lets you set or get the id string of the legend.
Method:
id()
id(option)
id(option, callback)
Where:
-
option: is a string representing theidof thelegend. -
callback: is a function that is run after theidis set but before the next render, this callback accept two optional arguments that represents the legend object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
idis used to identify the legend, so the string must be unique among other legends.
By default, the id is generated automatically to guarantee its uniqueness, so in the majority of cases it is best to leave it as it is.
Returns:
- A string representing the
idof thelegendif no arguments are pass. - A reference to the
legendobject from which the method is called upon.
Enable:
This method lets you set or get the enable status of the legend.
Method:
enable()
enable(option)
enable(option, callback)
Where:
-
option: is a boolean that determines whether thelegendmust be enabled. -
callback: is a function that is run after theenableproperty is set but before the next render, this callback accept two optional arguments that represents the legend object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A bolean representing the
enablestatus of thelegend. - A reference to the
legendobject from which the method is called upon.
Default Value:
- The default values for the
enableproperty of thelegendistrue.
Data:
This method lets you set or get the legend data.
Method:
data()
data(options)
data(options, callback)
Where:
-
options: is an array of object, each of those representig one entry in the legend. The objects have the following properties:-
dataset: is theidstring of the dataset. -
label: is a string representing the label of the dataset. -
text: an object with thetextproperties of the labels:-
font: a string representing the font of thelabel. -
size: a string representing the size of thelabel. -
specifier: a string representing the font specifier of thelabel. -
color: a string representing the color of thelabel. -
opacity: a number between 0 and 1 representing the opacity of thelabel.
-
-
-
callback: is a function that is run after thedatais set but before the next render, this callback accept two optional arguments that represents the legend object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- An array of object, with the
dataproperties of each dataset if no argument is pass. - A reference to the
legendobject from which the method is called upon.
Default Values:
The default value for the data is an empty array, but the entries do have default values.
{
dataset : "",
label : "",
text : {
color : "#000000",
opacity : 1,
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "12px",
specifier : ""
}
}
Columns:
This method lets you set or get the columns property of the legend.
All the dataset entries associated with the legend will be display in the amount of columns defined by this method.
Method:
columns()
columns(option)
columns(option, callback)
Where:
-
option: is a number with the amount ofcolumnsin thelegend. -
callback: is a function that is run after thecolumnsare set but before the next render, this callback accept two optional arguments that represents the legend object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A number representing the amount of
columnsin thelegendif no argument is pass. - A reference to the
legendobject from which the method is called upon.
Default Value:
- The default value for the
columnsproperty is1.
Position:
This method lets you set or get the legend position.
Method:
position()
position(option)
position(option, callback)
Where:
-
option: represents thelegendposition. It can be one of the following options:- A string with one of the predefined positions:
-
"top-right". -
"top-left". -
"bottom-right". -
"bottom-left". -
"center".
-
- An object with the following properties to position the
legendmanually:-
x: a number with thelegendxcoordinate. -
y: a number with thelegendycoordinate.
-
- A string with one of the predefined positions:
-
callback: is a function that is run after thepositionis set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
xandyvalues represent the coordinates of thelegendtop left corner relative to the graph rect.
Returns:
- A string representing the
legendpositionif no argument is pass. - A reference to the legend object from which the method is called upon.
Note: The returned string will be one of the predefined positions or
"floating"if was manually defined.
Default Value:
- The default values for the
legendpositionis"top-right".
Width:
This method lets you set or get the width property of the legend.
The width value only affects the size of the dataset marker and not the entire legend.
Method:
width()
width(option)
width(option, callback)
Where:
-
option: is a number representing thewidthin pixels of thelegendmarkers. -
callback: is a function that is run after thewidthis set but before the next render, this callback accept two optional arguments that represents the legend object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- A number representing the
widthof thelegendmarkers if no argument is pass. - A reference to the
legendobject from which the method is called upon.
Default Value:
- The default value for the
widthproperty is20.
Title:
This method lets you set or get the title of the legned.
The title will apear at the top of the legend.
Method:
title()
title(options)
title(options, callback)
Where:
-
options: is an object containing the following properties:-
text: a string representing the text of thetitle. -
font: a string representing the font of thetitle. -
size: a string representing the size of thetitle. -
specifier: a string representing the font specifier of thetitle. -
color: a string representing the color of thetitle. -
opacity: a number between 0 and 1 representing the opacity of thetitle. -
position: a string representing thepositionof thelegend. It can be one of the following options:-
"start". -
"center". -
"end".
-
-
-
callback: is a function that is run after thetitleis set but before the next render, this callback accept two optional arguments that represents the legend object from which the method is called upon and the graph object that is bound to, in that order.
Returns:
- An object with the value of the
titleproperties if no argument is pass. - A reference to the
legendobject from which the method is called upon.
Default Value:
The default values for the properties of the title method are as follow:
{
text : "",
color : "#000000",
font : "Arial, Helvetica Neue, Helvetica, sans-serif",
size : "12px",
specifier : "bold",
opacity : 1,
position : "start"
}
Background:
This method lets you set or get the background properties of the legend.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
background()
background(options)
background(options, callback)
Where:
-
options: is an object containing the following properties:-
color: a string representing thebackgroundcolor. -
opacity: a number representing thebackgroundopacity.
-
-
callback: is a function that is run after thebackgroundis set but before the next render, this callback accept two optional arguments that represents the legend object from which the method is called upon and the graph object that is bound to, in that order.
Note: The color can be in the format "#rrggbb" or be any of the standard color names.
Note: The opacity must be a number between 0 and 1.
Returns:
- An object with the
backgroundproperty values of thelegendif no argument is pass. - A reference to the
legendobject from which the method is called upon.
Default Value:
The default values for the properties of the background method are as follow:
{
color : "#ffffff",
opacity : 1
}
Border:
This method lets you set or get the border properties of the legend.
It is only necessary to define the values that you want to change and only that values will be updated, leaving the rest as they are.
Method:
border()
border(options)
border(options, callback)
Where:
-
options: is an object with the following properties:-
color: a string representing the color of the border. -
opacity: a number representing the opacity of the border. -
style: a string representing the style of the border. -
width: a number representing the line width of the border.
-
-
callback: is a function that is run after theborderproperties are set but before the next render, this callback accept two optional arguments that represents the colorbar object from which the method is called upon and the graph object that is bound to, in that order.
Note: The
colorcan be in the format "#rrggbb" or be any of the standard color names.
Note: The opacity must be a number between 0 and 1.
Note: The width must be a positive integer.
The style can be represented with one of the following options:
| Style | Result |
|---|---|
"solid" |
![]() |
"dot" |
![]() |
"dash" |
![]() |
"long-dash" |
![]() |
"dash-dot" |
![]() |
"dash-2dot" |
![]() |
Also, the string can be a list of space separated integers representing line and space length in pixel to form a custom pattern.
For example, the string "3 2 10 2 3" forms a pattern of 3 pixels line, 2 pixels space, 10 pixels line, etc.
Returns:
- An object with the
borderproperties if no argument is pass. - A reference to the
legendobject from which the method is called upon.
Default Values:
The default values for the properties of the border method are as follow:
{
color : "#000000",
opacity : 1,
style : "solid",
width : 1
}
Metrics:
This method returns the metrics of the legend.
Method:
metrics()
Returns:
- An object with the following properties:
-
x: Thexcoordinate of thelegend. -
y: Theycoordinate of thelegend. -
width: Thewidthof thelegend. -
height: Theheightof thelegend.
-
Note: The
xandycoordinates make reference to the top left corner of thelegend.
Note: The
xandycoordinates are relatives to thegraph rect.
Note: The
widthandheightvalues make reference to the width and height of thelegend.
Extras
This library includes some extra functionality to make the use of the graphs easier.
Restore Graph
This function allows you to recreate any graph saved with the graph2D save() method.
Function:
restoreGraph(options)
Where:
-
options: is an object containing the following properties:-
container: a div element. -
data: the object returned bysave().
-
The container is the div element that is going to host the new graph. It acts as the element argument when creating a graph using the graph2D() function.
Returns:
An object with the following properties:
-
graph: the newgraph2Dobject. -
linechart: An array with alllinechartobjects present in the graph. -
area: An array with allareaobjects present in the graph. -
heatmap: An array with allheatmapobjects present in the graph. -
vectorfield: An array with allvectorfieldobjects present in the graph. -
colorbar: An array with allcolorbarobjects present in the graph. -
legend: An array with alllegendobjects present in the graph.
All these objects are ready to go. So you can call the draw() method on the graph object to show the graph.
linspace:
This function creates an array of numbers with n elements from start to end (inclusive).
The resulting values will be equally (linearly) spaced, hence the name.
Function:
linspace(`start`, `end`, `n`)
Note:
nmust be a positive integer.
Note: The
startandendarguments can hold the same vale, the result will be an array withncopies of that value.
Returns:
- An array of numbers.
Examples:
linspace(1, 10, 10) //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
linspace(-6, 6, 9) //[-6, -4.5, -3, -1.5, 0, 1.5, 3, 4.5, 6]
linspace(10, 1, 10) //[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
linspace(1, 1, 5) //[1, 1, 1, 1, 1]
Meshgrid:
This function generates two matrices of numbers representing the rectangular two-dimensional domain given by the x and y arguments.
The values returned by this function are compatible with the meshX() and meshY() methods.
Function:
meshgrid(x, y)
Where:
-
x: represents thexdomain. It can be one of the following options:- An array of numbers, with each value representing one
xcoordinate. - An object with the properties:
-
start: a number representing thestartof thexdomain. -
end: a number representing theendof thexdomain. -
n: a number representing the number of times thexdomain will be divided into.
-
- An array of numbers, with each value representing one
-
y: represents theydomain. It can be one of the following options:- An array of numbers, with each value representing one
ycoordinate. - An object with the properties:
-
start: a number representing thestartof theydomain. -
end: a number representing theendof theydomain. -
n: a number representing the number of times theydomain will be divided into.
-
- An array of numbers, with each value representing one
Note: If the
xoryarguments are defined as an object, thelinspace()function will be used to generate the domain.
A two-dimensional matrix can be acces by two indices M[i][j].
For example:
M = [
[M11, M12, M13],
[M21, M22, M23],
[M31, M32, M33],
]
Where i represents the row and j the column of the value.
Returns:
- An array with two values, the first being the
meshXmatrix and the second themeshY.
Example:
const x = linspace(1, 5, 5); //[1, 2, 3, 4, 5]
const y = linspace(6, 10, 5); //[6, 7, 8, 9, 10]
const [x_mesh, y_mesh] = meshgrid(x, y);
The resulting values will be:
x_Mesh = [
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
]
y_Mesh = [
[10, 10, 10, 10, 10],
[9, 9, 9, 9, 9],
[8, 8, 8, 8, 8],
[7, 7, 7, 7, 7],
[6, 6, 6, 6, 6],
]
As shown, the meshX matrix contains the x array as its rows and the meshY contains the y array as its columns.
Note: The final dimension of
meshXandmeshYdepend directly on the lenght ofxandy.
Mappign:
This function lets you create a mapping object. These objects are primarily used to translate screen coordinates to inner graph coordinates.
This offers a way to pass values from one domain to another using some transformation.
Function:
mapping(options)
Where:
-
options: is an object that contains the following properties:-
from: an array of two numbers representing themappingdomain. -
to: an array of two numbers representing themappingrange. -
type: a string representing themappingtype. It can be one of the following options:-
"linear". -
"log". -
"sqr".
-
-
base: a number representing thebaseif using alogtransformation.
-
Returns:
A mapping object with the following properties:
-
map: a funtion that takes a number from thedomainand returns the correspondingrangevalue. -
invert: a function that takes a number from therangeand returns the correspondingdomainvalue. -
domain: an array of two numbes reprenting thedomain. -
range: an array of two numbes reprenting therange. -
type: a string representing themappingtype. -
base: a number representing themappingbase.
Example:
const linear_map = mapping({
from : [0, 1],
to : [0, 10]
});
console.log( linear_map.map(0) );
console.log( linear_map.map(1) );
console.log( linear_map.map(2) );
console.log( linear_map.invert(10) );
console.log( linear_map.invert(5) );
console.log( linear_map.invert(-5) );
Result:
0
10
20
1
0.5
-0.5
As shown, values outside the damain (and range) are extrapolated correctly.
Warning: When using the
logtype thedomainmust be strictly positive or negative and dont contain the zero.
Warning: When using the
strtype, thedomainmust be strictly positive or negative but in this case the zero is allow.
Default Values:
Only the type and base have default values, those values are:
{
type : "linear",
base : 10
}
Color Interpolator:
This function creates a simple color interpolator between two color.
This object functions similarly as the mapping object, but this time it maps a number to a color.
Note: Unlike the
mapping, the inverse function is not implemented.
Function:
colorInterpolator(options)
Where:
-
options: is an object with the following properties:-
from: an array of two numbers representing thedomain. -
to: an array of two color strings representing therange. -
space: the color space in which the interpolation will be made. Ir can be one of the following options:-
"rgb". -
"hsv". -
"xyx". -
"lab". -
"lch".
-
-
Warning: Interpolation of numbers outside the
domainwill be indeterminate.
Note: The colors defined in the
rangemust be in the format"#rrggbb". Standard color names are not supported.
Returns:
A interpolator object with the following properties:
-
map: a funtion that takes a number from thedomainand returns the corresponding color. -
domain: an array of two numbes reprenting thedomain. -
range: an array of two color strings reprenting therange. -
space: a string representing the color space used.
Example:
const interpolator = colorInterpolator({
from : [0, 1],
to : ["#000000", "#ffffff"]
});
console.log(interpolator.map(0));
console.log(interpolator.map(0.25));
console.log(interpolator.map(0.5));
console.log(interpolator.map(1));
Result:
#000000
#3b3b3b
#777777
#ffffff
The color space used determines the path follow by the interpolator.
For example, consider the interpolation between "#440154" and "#fde724":
| space | Interpolation |
|---|---|
"rgb" |
![]() |
"hsv" |
![]() |
"xyz" |
![]() |
"lab" |
![]() |
"lch" |
![]() |
Computation in the rgb space is less expensive than those made in lch space. In general, the complexity follows the last table, being the most 'expensive' spaces at the bottom.
Default Values:
Only the space have default value, this value are:
{
space : "lch",
}
Color Map:
This function creates a ready to use general purpose color interpolator.
Function:
colorMap(options)
Where:
-
options: is an object containing the following properties:-
from: a number indicating thedomainstart. -
to: a number indicating thedomainend. -
type: a string representing thecolorMaptype. It can be one of the following options:-
"viridis". -
"plasma". -
"magma". -
"magnet". -
"inv_magnet". -
"fairy". -
"inv_fairy". -
"swamp". -
"inv_swamp". -
"fire". -
"royal". -
"hsv".
-
-
Warning: Interpolation of numbers outside the
domainwill be indeterminate.
Returns:
- A function that takes a number from the domain and returns the corresponding color.
Example:
const color_map = colorMap({
from : 0,
to : 1,
type : "plasma"
});
console.log(color_map(0));
console.log(color_map(0.25));
console.log(color_map(0.5));
console.log(color_map(1));
Result:
#440154
#9e0059
#e24648
#fde724
The colormap types are organized in the following categories.
Sequential
| type | Result |
|---|---|
"viridis" |
![]() |
"plasma" |
![]() |
"magma" |
![]() |
Diverging
| type | Result |
|---|---|
"magnet" |
![]() |
"swamp" |
![]() |
"fairy" |
![]() |
"inv-magnet" |
![]() |
"inv-swamp" |
![]() |
"inv-fairy" |
![]() |
Cyclic
| type | Result |
|---|---|
"fire" |
![]() |
"royal" |
![]() |
"hsv" |
![]() |
License
MIT License
Copyright (c) 2023 Jhoselin Ramirez
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

























































































