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.
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.
To install just type
npm install scigrapher
In a terminal on your project folder.
Each graph is controlled by a single object created by calling the graph2D
function.
graph2D(element)
graph2D(element, options)
Where
element
is a div element.options
is 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
graph2D
methods, 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
type
topolar
but 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:
datatype
is one of the data types available : "linechart", "area", "heatmap" or "vectorfield".options
is 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.
This method returns or changes the graph background color.
Method:
backgroundColor()
backgroundColor(option)
backgroundColor(option, callback)
Where:
option
is a string representing the background color in the format #rrggbb or any of the standard color names.callback
is 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)
This method returns or changes the graph background opacity.
Method:
backgroundOpacity()
backgroundOpacity(option)
backgroundOpacity(option, callback)
Where:
option
is a number between 0 and 1 representing the graph background opacity.callback
is 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
This method returns or change the current axis position.
Method:
axisPosition()
axisPosition(option)
axisPosition(option, callback)
Where:
option
is a string representing the axis position, the available options are:- "center"
- "bottom-left"
- "bottom-right"
- "top-left"
- "top-right"
callback
is 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"
.
This method returns or changes the current axis type.
Method:
axisType()
axisType(option)
axisType(option, callback)
Where:
option
is 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
x
axis and linear scale on they
axis. - "y-log": Base 10 logarithmic scale on the
y
axis and linear scale on thex
axis. - "log-log" Base 10 logarithmic scale on both axis.
callback
is 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"
.
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:
options
is an object representing the axis domain, this object has the properties:x
: An object containing the propertiesstart
andend
.- The property
start
is the value at the left most side of thex
axis. - The property
end
is the value at the right most side of thex
axis.
- The property
y
: An object containing the propertiesstart
andend
.- The property
start
is the value at the bottom of they
axis. - The property
end
is the value at the top of they
axis.
- The property
callback
is 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
end
value of they
axis 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
start
value be smaller than theend
value. 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
}
}
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:
options
is 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 wholex
axis.yAxis
: a string representing the color of the wholey
axis.base
: an object containing the following properties:x
: a string representing the color of thex
axisbase
.y
: a string representing the color of they
axisbase
.
tick
: an object containing the following properties:x
: a string representing the color of thex
axisticks
.y
: a string representing the color of they
axisticks
.
text
: an object containing the following properties:x
: a string representing the color of thex
axistext
.y
: a string representing the color of they
axistext
.
callback
is 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"
}
}
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:
options
is 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 wholex
axis.yAxis
: a number between 0 and 1 representing the opacity of the wholey
axis.base
: an object containing the following properties:x
: a number between 0 and 1 representing the opacity of thex
axisbase
.y
: a number between 0 and 1 representing the opacity of they
axisbase
.
tick
: an object containing the following properties:x
: a number between 0 and 1 representing the opacity of thex
axisticks
.y
: a number between 0 and 1 representing the opacity of they
axisticks
.
text
: an object containing the following properties:x
: a number between 0 and 1 representing the opacity of thex
axistext
.y
: a number between 0 and 1 representing the opacity of they
axistext
.
callback
is 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
}
}
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:
options
is an object containing the following properties:x
: a string representig thex
axis units:y
: a string representig they
axis units:
callback
is 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 : ""
}
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:
options
is an object containing the following properties:x
: an object containing the following properties:color
: a string representing thex
axis base color.opacity
: a number between 0 and 1 representing thex
axis base opacity.width
: an integer positive number representing the line width of thex
axis base.
y
: an object containing the following properties:color
: a string representing they
axis base color.opacity
: a number between 0 and 1 representing they
axis base opacity.width
: an integer positive number representing the line width of they
axis base.
callback
is 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
},
}
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 thex
axis ticks.opacity
: a number between 0 and 1 representing the opacity of thex
axis ticks.width
: a positive integer representing the line width of thex
axis ticks.size
: a number representing the size in pixels of thex
axis 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 thegraph
object 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 theticks
property 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 they
axis ticks.opacity
: a number between 0 and 1 representing the opacity of they
axis ticks.width
: a positive integer representing the line width of they
axis ticks.size
: a number representing the size in pixels of they
axis 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 thegraph
object 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 theticks
property is set to"auto"
controls the minimum space in pixels that the ticks can have between them, otherwise it has no effect.
callback
is 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
center
the tick size will be double of thesize
property. 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
},
}
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 propertiesx
: and object with the following properties:color
: a string representing the color of the text in thex
axis.opacity
: a number between 0 and 1 representing the opacity of the text in thex
axis.font
: a string representing the font of the text in thex
axis.size
: a string representing the size of the text in thex
axis.specifier
: a string representing the font specifier of the text in thex
axis.
y
: and object with the following properties:color
: a string representing the color of the text in they
axis.opacity
: a number between 0 and 1 representing the opacity of the text in they
axis.font
: a string representing the font of the text in they
axis.size
: a string representing the size of the text in they
axis.specifier
: a string representing the font specifier of the text in they
axis.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The
specifier
can 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
specifier
options.
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 : ""
}
}
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:
dynamic
is an object containing the following properties:x
: and object with the following properties:dynamic
: a boolean that determines whether thex
axis text changes position relative to the base.contained
: a boolean that determines whether thex
axis shifts to remain visible.
y
: and object with the following properties:dynamic
: a boolean that determines whether they
axis text changes position relative to the base.contained
: a boolean that determines whether they
axis shifts to remain visible.
callback
is 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
}
}
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:
overlap
is 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 thex
axis should be cleared.y
: a boolean representing whether a small area arround the text of they
axis should be cleared.
callback
is 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
priority
property.
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
}
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:
center
positioned axis cannot have secondary axis defined.
Method:
secondaryAxisEnable()
secondaryAxisEnable(options)
secondaryAxisEnable(options, callback)
Where:
options
is an object containing the following properties:x
: a boolean that determines whether the secondaryx
axis must be enable.y
: a boolean that determines whether the secondaryy
axis must be enable.
callback
is 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
position
is 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
}
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:
options
is an object representing the axis domain, this object has the properties:x
: An object containing the propertiesstart
andend
.- The property
start
is the value at the left most side of thex
axis. - The property
end
is the value at the right most side of thex
axis.
- The property
y
: An object containing the propertiesstart
andend
.- The property
start
is the value at the bottom of they
axis. - The property
end
is the value at the top of they
axis.
- The property
callback
is 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
end
value of they
axis 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
start
value be smaller than theend
value. 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
}
}
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:
options
is 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 wholex
axis.yAxis
: a string representing the color of the wholey
axis.base
: an object containing the following properties:x
: a string representing the color of thex
axisbase
.y
: a string representing the color of they
axisbase
.
tick
: an object containing the following properties:x
: a string representing the color of thex
axisticks
.y
: a string representing the color of they
axisticks
.
text
: an object containing the following properties:x
: a string representing the color of thex
axistext
.y
: a string representing the color of they
axistext
.
callback
is 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"
}
}
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:
options
is 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 wholex
axis.yAxis
: a number between 0 and 1 representing the opacity of the wholey
axis.base
: an object containing the following properties:x
: a number between 0 and 1 representing the opacity of thex
axisbase
.y
: a number between 0 and 1 representing the opacity of they
axisbase
.
tick
: an object containing the following properties:x
: a number between 0 and 1 representing the opacity of thex
axisticks
.y
: a number between 0 and 1 representing the opacity of they
axisticks
.
text
: an object containing the following properties:x
: a number between 0 and 1 representing the opacity of thex
axistext
.y
: a number between 0 and 1 representing the opacity of they
axistext
.
callback
is 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
}
}
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:
options
is an object containing the following properties:x
: a string representig thex
axis units:y
: a string representig they
axis units:
callback
is 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 : ""
}
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:
options
is an object containing the following properties:x
: an object containing the following properties:color
: a string representing thex
axis base color.opacity
: a number between 0 and 1 representing thex
axis base opacity.width
: an integer positive number representing the line width of thex
axis base.
y
: an object containing the following properties:color
: a string representing they
axis base color.opacity
: a number between 0 and 1 representing they
axis base opacity.width
: an integer positive number representing the line width of they
axis base.
callback
is 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
},
}
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 thex
axis ticks.opacity
: a number between 0 and 1 representing the opacity of thex
axis ticks.width
: a positive integer representing the line width of thex
axis ticks.size
: a number representing the size in pixels of thex
axis 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 thegraph
object 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 theticks
property 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 they
axis ticks.opacity
: a number between 0 and 1 representing the opacity of they
axis ticks.width
: a positive integer representing the line width of they
axis ticks.size
: a number representing the size in pixels of they
axis 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 thegraph
object 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 theticks
property is set to"auto"
controls the minimum space in pixels that the ticks can have between them, otherwise it has no effect.
callback
is 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
},
}
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 propertiesx
: and object with the following properties:color
: a string representing the color of the text in thex
axis.opacity
: a number between 0 and 1 representing the opacity of the text in thex
axis.font
: a string representing the font of the text in thex
axis.size
: a string representing the size of the text in thex
axis.specifier
: a string representing the font specifier of the text in thex
axis.
y
: and object with the following properties:color
: a string representing the color of the text in they
axis.opacity
: a number between 0 and 1 representing the opacity of the text in they
axis.font
: a string representing the font of the text in they
axis.size
: a string representing the size of the text in they
axis.specifier
: a string representing the font specifier of the text in they
axis.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value 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
specifier
can 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
specifier
options.
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 : ""
}
}
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:
options
is 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
.
callback
is 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"
}
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
polar
the 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
x
component of the primary grid is composed of concentric circles around the origin with radii equal to each tick in thex
direction. - The
y
component of the primary grid is composed of rays coming out of the origin with equal angle variation.
- The
-
Secondary
:- The
x
component of the secondary grid is composed of concentric circles arround the origin with raii equal to values in between the ticks in thex
direction. - The
y
component of the secondary grid is composed of rays coming out of the origin, with equal angle variation in between each ray of they
component 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.
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:
options
is an object containing the following properties:grid
: a string representing the color of both whole grids.primary:
a string representing the color of the wholeprimary
grid.secondary:
a string representing the color of the wholesecondary
grid.x
: an object with the following properties:primary
: a string representing the color of thex
component of theprimary
grid.secondary
: a string representing the color of thex
component of thesecondary
grid.
y
: an object with the following properties:primary
: a string representing the color of they
component of theprimary
grid.secondary
: a string representing the color of they
component of thesecondary
grid.
callback
is 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",
}
}
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:
options
is an object containing the following properties:grid
: a number representing the opacity of both whole grids.primary:
a number representing the opacity of the wholeprimary
grid.secondary:
a number representing the opacity of the wholesecondary
grid.x
: an object with the following properties:primary
: a number representing the opacity of thex
component of theprimary
grid.secondary
: a number representing the opacity of thex
component of thesecondary
grid.
y
: an object with the following properties:primary
: a number representing the opacity of they
component of theprimary
grid.secondary
: a number representing the opacity of they
component of thesecondary
grid.
callback
is 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,
}
}
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:
options
is an object containing the following properties:grid
: a string representing the style of both whole grids.primary:
a string representing the style of the wholeprimary
grid.secondary:
a string representing the style of the wholesecondary
grid.x
: an object with the following properties:primary
: a string representing the style of thex
component of theprimary
grid.secondary
: a string representing the style of thex
component of thesecondary
grid.
y
: an object with the following properties:primary
: a string representing the style of they
component of theprimary
grid.secondary
: a string representing the style of they
component of thesecondary
grid.
callback
is 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",
}
}
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:
options
is an object containing the following properties:grid
: a number representing the width of both whole grids.primary:
a number representing the width of the wholeprimary
grid.secondary:
a number representing the width of the wholesecondary
grid.x
: an object with the following properties:primary
: a number representing the width of thex
component of theprimary
grid.secondary
: a number representing the width of thex
component of thesecondary
grid.
y
: an object with the following properties:primary
: a number representing the width of they
component of theprimary
grid.secondary
: a number representing the width of they
component of thesecondary
grid.
callback
is 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,
}
}
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:
options
is 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 gridx
component should be enabled.color
: a string representing the color of the gridx
component.opacity
: a number representing the opacity of the gridx
component.width
: a number representing the line width of the gridx
component.style
: a string representing the line style of the gridx
component.
y
: an object with the properties:enable
: a boolean that determines whether the gridy
component should be enabled.color
: a string representing the color of the gridy
component.opacity
: a number representing the opacity of the gridy
component.width
: a number representing the line width of the gridy
component.style
: a string representing the line style of the gridy
component.
callback
is 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"
}
}
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:
options
is 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 thedensity
value 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 thedensity
value is set to"auto"
.
x
: an object with the properties:enable
: a boolean that determines whether the gridx
component should be enabled.color
: a string representing the color of the gridx
component.opacity
: a number representing the opacity of the gridx
component.width
: a number representing the line width of the gridx
component.style
: a string representing the line style of the gridx
component.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 thedensity
value 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 thedensity
value is set to"auto"
.
y
: an object with the properties:enable
: a boolean that determines whether the gridy
component should be enabled.color
: a string representing the color of the gridy
component.opacity
: a number representing the opacity of the gridy
component.width
: a number representing the line width of the gridy
component.style
: a string representing the line style of the gridy
component.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 thedensity
value 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 thedensity
value is set to"auto"
.
callback
is 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
minSpacing
andmaxDensity
are only relevant when thedensity
property 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
}
}
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:
option
is a number representing the number of main divisions in the angle grid if no arguments are pass.callback
is 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:
option
must be a positive integer, this property is only relevant when the axistype
is 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
.
Currently, the available labels are:
Title
: Located at the top of the graph.Subtitle
: Located below theTitle
.x Label
: Located beside thex
axis.y Label
: Located beside they
axis.Secondary x Label
: Located beside thex
secondary axis.Secondary y Label
: Located beside they
secondary 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.
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:
options
is an object containing the following properties:enable
: a boolean that determines whether thetitle
must 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"
.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The
specifier
can 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
specifier
options.
Returns:
undefined
if thetitle
is not defined yet and no arguments are pass.- An object representing the properties of the
title
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 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"
}
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:
options
is an object containing the following properties:enable
: a boolean that determines whether thesubtitle
must 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"
.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The
specifier
can 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
specifier
options.
Returns:
undefined
if thesubtitle
is not defined yet and no arguments are pass.- An object representing the properties of the
subtitle
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 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"
}
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:
options
is an object containing the following properties:enable
: a boolean that determines whether thex label
must 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"
.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
position
is not set tocenter
.
Note: The
specifier
can 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
specifier
options.
Returns:
undefined
if thex label
is not defined yet and no arguments are pass.- An object representing the properties of the
x label
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 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"
}
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:
options
is an object containing the following properties:enable
: a boolean that determines whether they label
must 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"
.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
position
is not set tocenter
.
Note: The
specifier
can 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
specifier
options.
Returns:
undefined
if they label
is not defined yet and no arguments are pass.- An object representing the properties of the
y label
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 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"
}
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:
options
is an object containing the following properties:enable
: a boolean that determines whether thesecondary x label
must 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"
.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
position
is not set tocenter
.
Note: This label will not render if the secodary
x
axis is not enabled.
Note: The
specifier
can 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
specifier
options.
Returns:
undefined
if thesecondary x label
is not defined yet and no arguments are pass.- An object representing the properties of the
secondary x label
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 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"
}
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:
options
is an object containing the following properties:enable
: a boolean that determines whether thesecondary y label
must 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"
.
callback
is 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
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The label will render only if the axis
position
is not set tocenter
.
Note: This label will not render if the secodary
y
axis is not enabled.
Note: The
specifier
can 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
specifier
options.
Returns:
undefined
if thesecondary y label
is not defined yet and no arguments are pass.- An object representing the properties of the
secondary y label
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 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"
}
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
pointerMove
andpointerZoom
are 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
.
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:
options
is 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 theprimary
axis.secondaryAxis
: a boolean that determines if the move affects thesecondary
axis.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
delay
property affects how often that calculations are made.
Note: The value of the
defaultCursor
,hoverCursor
,moveCursor
can be any of the standar cursor names.
Note: When disabled, the cursor will be set to
defaultCursor
.
Note: Unlike other methods, the
callback
function is contained inside theoptions
object.
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
}
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:
options
is 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 theprimary
axis.secondaryAxis
: a boolean that determines if the move affects thesecondary
axis.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 thetype
property is set todrag
or when using touch devices.rect
: an object representing the rectangle draw when usin thedrag
zoomtype
: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
delay
property affects how often that calculations are made.
Note: The value of the
defaultCursor
,hoverCursor
,moveCursor
can be any of the standar cursor names.
Note: When disabled, the cursor will be set to
defaultCursor
.
Note: Unlike other methods, the
callback
function is contained inside theoptions
object.
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
}
}
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:
options
is 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 theprimary
axis.secondaryAxis
: a boolean that determines if the move affects thesecondary
axis.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
}
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:
options
is 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 thex
axis as thesource
. - The string
"y"
to set they
axis as thesource
. - The string
"xSecondary"
to set the secondaryx
axis as thesource
. - The string
"ySecondary"
to set the secondaryy
axis as thesource
.
- The string
target
: a string representing the target axis. It can be one of the folloging options:- The string
"x"
to set thex
axis as thetarget
. - The string
"y"
to set they
axis as thetarget
. - The string
"xSecondary"
to set the secondaryx
axis as thetarget
. - The string
"ySecondary"
to set the secondaryy
axis as thetarget
.
- The string
anchor
: this property controls the point on thetarget
axis that will remain unchaged afther the transformation. It can be one of the following values:- The string
"start"
to fix thestart
value of thetarget
axis domain. - The string
"end"
to fix theend
value of thetarget
axis domain. - A number to fix that coordinate of the
target
axis domain.
- The string
callback
is 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
source
ortarget
properties are set to either secondary axis, and that axis is not enabled, the method will take no effect.
Note: The the
source
ortarget
properties 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"
}
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:
options
is an object containing the following properties:x
: an object whith the properties:start
: this property controls the margin at the left most side of thex
axis. 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 thex
axis. 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 they
axis. 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 they
axis. 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
callback
is 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
end
value of they
component 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",
},
}
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:
options
is 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.
callback
is 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"
}
}
}
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:
options
is 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.
callback
is 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
width
andheight
properties 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
}
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.
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.
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
pointerMove
andpointerZoom
methods are set on the second element of the array.
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
x
andy
coordinates of the top left corner and the width and height in pixels of the client rect.
Examples of the client 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
x
andy
coordinates of the top left corner and the width and height in pixels of the graph rect.
Examples of the graph rect:
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.
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 theoptions
arument of thegraph2D
function.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 theoptions
argument 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.
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.
This method lets you create a dataset.
Method:
addDataset(type)
addDataset(type, options)
addDataset(type, options, callback)
Where:
type
is a string indicating one of the available datasets. The posible values are:"linechart"
."area"
."heatmap"
."vectorfield"
.
options
is an object containing the options to change the default behavior and appearance of the dataset.callback
is 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
type
so it's convenient to assign this value to a variable for later use.
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 alllinechart
objects.area
: an array with allarea
objects.heatmap
: an array with allheatmap
objects.vectorfield
: an array with allvectorfield
objects.
This method removes a dataset form the graph.
Method:
removeDataset(id)
removeDataset(id, callback)
Where:
id
is the id string of the dataset you want to remove.callback
is 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.
This method lets you create a colorbar.
Method:
addColorbar()
addColorbar(options)
addColorbar(options, callback)
Where:
options
is an object containing the options to change the default behavior and appearance of the colorbar.callback
is 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.
This method returns an array containing all the colorbars associated with the graph.
Method:
getColorbars()
Returns:
- An array with all colorbars.
This method removes a colorbar form the graph.
Method:
removeColorbar(id)
removeColorbar(id, callback)
Where:
id
is the id string of the colorbar you want to remove.callback
is 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.
This method lets you create a colorbar.
Method:
addLegend()
addLegend(options)
addLegend(options, callback)
Where:
options
is an object containing the options to change the default behavior and appearance of the legend.callback
is 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.
This method returns an array containing all the legends associated with the graph.
Method:
getLegends()
Returns:
- An array with all legends.
This method removes a legend form the graph.
Method:
removeLegend(id)
removeLegend(id, callback)
Where:
id
is the id string of the legend you want to remove.callback
is 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.
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
lines
that can be draw connecting each datapoint. - The
markers
that can be draw on each datapoint. - The
error bars
that 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:
linspace
is 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
x
andy
data 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 thex
data generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
dataY()
ordraw()
method from they
data generator because that will create a circular dependency and the program will stop.
Note: It is safe to call the
dataX()
method from within they
data 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 thex
value of the datapoint.y
: is they
value of the datapoint.index
: is the index corresponding to thex
andy
values.arrayX
: is the complete arary ofx
data values.arrayY
: is the complete arary ofy
data 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:
colorMap
is a utility function, you can learn more about this functions on the extras section.
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 theid
of the dataset.callback
: is a function that is run after theid
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: The
id
is 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
id
of the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
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 theindex
value of the dataset.callback
: is a function that is run after theindex
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: The draw order of datasets with the same
index
value will be indeterminate.
Returns:
- A number that represents the
index
of the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
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.
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.
This method lets you get ot set the x
component of the data points.
Method:
dataX()
dataX(data)
dataX(data, callback)
Where:
data
: represents thex
component 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
linechart
data generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
x
component 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.
This method lets you get ot set the y
component of the data points.
Method:
dataY()
dataY(data)
dataY(data, callback)
Where:
data
: represents they
component 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
linechart
data generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
y
component 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.
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 thex
axis that the dataset is goin to use. It can be"primary"
or"secondary"
.y
: a string representing they
axis 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"
}
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 thedata
must be interpreted inpolar
coordinates.callback
: is a function that is run after thepolar
property 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:
dataX
will be interpreted as the radial coordinates.dataY
will 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
polar
property 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
.
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 lineenable
property 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
linechart
the marks and error bars must be disabled as well.
Returns:
- A bolean representing the
enable
status of the line. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enable
property of the line istrue
.
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
color
on each datapoint. - A generator function that returns a string representing the
color
at each datapoint.
- A string that represents the whole line
callback
: is a function that is run after the linecolor
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: The
color
can be in the format "#rrggbb" or be any of the standard color names.
Note: If the
color
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the line
color
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
color
is defined as a function.
Default Value:
- The default value for the line
color
is"#0043e0"
.
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
opacity
on each datapoint. - A generator function that returns a number representing the
opacity
at each datapoint.
- A number that represents the whole line
callback
: is a function that is run after the lineopacity
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: The
opacity
values must be a number between 0 and 1.
Note: If the
opacity
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
opacity
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
opacity
is defined as a function.
Default Value:
- The default value for the line
opacity
is1
.
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
style
on each datapoint. - A generator function that returns a string representing the
style
at each datapoint.
- A string that represents the whole line
callback
: is a function that is run after the linestyle
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
style
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property 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
style
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
style
is defined as a function.
Default Value:
- The default value for the line
style
is"solid"
.
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
width
on each datapoint. - A generator function that returns a number representing the
width
at each datapoint.
- A number that represents the whole line
callback
: is a function that is run after the linewidth
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: The
width
values must be a positive integer.
Note: If the
width
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
width
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
width
is defined as a function.
Default Value:
- The default value for the line
width
is1
.
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.
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 markersenable
property 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
linechart
the line and error bars must be disabled as well.
Returns:
- A bolean representing the
enable
status of the marker. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for the
enable
property of the marker isfalse
.
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
color
of all markers. - An array of strings, each representing the marker
color
at each datapoint. - A generator function that returns a string representing the
color
at each datapoint.
- A string that represents the
callback
: is a function that is run after the markercolor
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: The
color
can be in the format "#rrggbb" or be any of the standard color names.
Note: If the
color
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the markers
color
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
color
is defined as a function.
Default Value:
- The default value for the markers
color
is"#0043e0"
.
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
opacity
of all markers. - An array of numbers, each representing the marker
opacity
at each datapoint. - A generator function that returns a number representing the marker
opacity
at each datapoint.
- A number that represents the
callback
: is a function that is run after the markeropacity
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: The
opacity
values must be a number between 0 and 1.
Note: If the
opacity
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the marker
opacity
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
opacity
is defined as a function.
Default Value:
- The default value for the marker
opacity
is1
.
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
style
of all markers. - An array of strings, each representing the marker line
style
at each datapoint. - A generator function that returns a string representing the marker line
style
at each datapoint.
- A string that represents the
callback
: is a function that is run after the markerstyle
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 is only relevant wen the marker
filled
property is set tofalse
.
Note: If the
style
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property 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
style
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
style
is defined as a function.
Default Value:
- The default value for the marker line
style
is"solid"
.
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
width
of all markers. - An array of numbers, each representing the marker line
width
at each datapoint. - A generator function that returns a number representing the marker line
width
at each datapoint.
- A number that represents line
callback
: is a function that is run after the marker linewidth
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 is only relevant wen the marker
filled
property is set tofalse
.
Note: The
width
values must be a positive integer.
Note: If the
width
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the marker line
width
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
width
is defined as a function.
Default Value:
- The default value for the marker line
width
is1
.
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 markerfilled
property. 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
filled
at each datapoint. - A generator function that returns a boolean determining whether the marker should be
filled
at each datapoint.
- A boolean that determine whether all markers should be
callback
: is a function that is run after the markerfilled
property 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
filled
property is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A bolean or a string of boolean representing the marker
filled
property. - A reference to the dataset object from which the method is called upon.
Note: The returned value will be an array even if
filled
is defined as a function.
Default Value:
- The default values for the
enable
property of the line istrue
.
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
type
of all markers. - An array of strings representing the marker
type
at each datapoint. - A generator function that returns a string representing the marker
type
at each datapoint.
- A string representing the
callback
: is a function that is run after the markertype
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 marker
type
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property 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
type
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
type
is defined as a function.
Default Value:
- The default value for the marker
type
is"circle"
.
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
size
of all markers. - An array of numbers representing the marker
size
at each datapoint. - A generator function that returns a number representing the marker
size
at each datapoint.
- A number representing the
callback
: is a function that is run after the markersize
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: The marker
size
must be a positive number.
Note: If the marker
size
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property 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
size
is 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
size
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
size
is defined as a function.
Default Value:
- The defaul value for the marker
size
is1
.
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.
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 thex
error bars must be enabled.y
: a boolean that determines whether they
error bars must be enabled.
callback
: is a function that is run after the error barsenable
property 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
linechart
the line and markers must be disabled as well.
Returns:
- An object representing the
enable
property 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
}
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
type
of all error bars. - An array of strings representing the error bar
type
at each datapoint. - A generator function that returns a string representing the error bar
type
at each datapoint.
- A string representing the
callback
: is a function that is run after the error bartype
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 error bar
type
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property 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
type
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
type
is defined as a function.
Default Value:
- The default value for the error bar
type
is"tail-cross"
.
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 thedata
of the error barx
component. It can be one of the following options:- A number that represents the
x
data
of all error bars. - An array of numbers, each representing the errorbar
x
data
at each datapoint. - A generator function that returns a number representing the error bar
x
data
at each datapoint.
- A number that represents the
callback
: is a function that is run after the error bardata
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
data
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
data
of the error barx
component 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
data
is defined as a function.
Default Value:
- The default value for the error bar
x
data
is0
.
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 thedata
of the error bary
component. It can be one of the following options:- A number that represents the
y
data
of all error bars. - An array of numbers, each representing the errorbar
y
data
at each datapoint. - A generator function that returns a number representing the error bar
y
data
at each datapoint.
- A number that represents the
callback
: is a function that is run after the error bardata
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
data
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
data
of the error bary
component 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
data
is defined as a function.
Default Value:
- The default value for the error bar
y
data
is0
.
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 thecolor
of the error barx
component. It can be one of the following options:- A string that represents the
color
of all error bars. - An array of strings, each representing the error bar
color
at each datapoint. - A generator function that returns a string representing the error bar
color
at each datapoint.
- A string that represents the
callback
: is a function that is run after the error barcolor
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: The
color
can be in the format "#rrggbb" or be any of the standard color names.
Note: If the
color
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the
color
of the error barx
component 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
color
is defined as a function.
Default Value:
- The default value for the error bars
x
color
is"#666666"
.
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 thecolor
of the error bary
component. It can be one of the following options:- A string that represents the
color
of all error bars. - An array of strings, each representing the error bar
color
at each datapoint. - A generator function that returns a string representing the error bar
color
at each datapoint.
- A string that represents the
callback
: is a function that is run after the error barcolor
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: The
color
can be in the format "#rrggbb" or be any of the standard color names.
Note: If the
color
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A string or an array of strings representing the
color
of the error bary
component 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
color
is defined as a function.
Default Value:
- The default value for the error bars
y
color
is"#666666"
.
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 theopacity
of the error barx
component. It can be one of the following options:- A number that represents the
opacity
of all error bars. - An array of numbers, each representing the error bar
opacity
at each datapoint. - A generator function that returns a number representing the error bar
opacity
at each datapoint.
- A number that represents the
callback
: is a function that is run after the error baropacity
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: The
opacity
values must be a number between 0 and 1.
Note: If the
opacity
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
opacity
of the error barx
component 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
opacity
is defined as a function.
Default Value:
- The default value for the error bar
x
opacity
is1
.
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 theopacity
of the error bary
component. It can be one of the following options:- A number that represents the
opacity
of all error bars. - An array of numbers, each representing the error bar
opacity
at each datapoint. - A generator function that returns a number representing the error bar
opacity
at each datapoint.
- A number that represents the
callback
: is a function that is run after the error baropacity
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: The
opacity
values must be a number between 0 and 1.
Note: If the
opacity
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the
opacity
of the error bary
component 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
opacity
is defined as a function.
Default Value:
- The default value for the error bar
y
opacity
is1
.
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 linestyle
of the error barx
component. It can be one of the following options:- A string that represents the
style
of all error bars. - An array of strings, each representing the error bar line
style
at each datapoint. - A generator function that returns a string representing the error bar line
style
at each datapoint.
- A string that represents the
callback
: is a function that is run after the error barstyle
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
style
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property 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
style
of the error barx
component 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
style
is defined as a function.
Default Value:
- The default value for the error bar
x
linestyle
is"solid"
.
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 linestyle
of the error bary
component. It can be one of the following options:- A string that represents the
style
of all error bars. - An array of strings, each representing the error bar line
style
at each datapoint. - A generator function that returns a string representing the error bar line
style
at each datapoint.
- A string that represents the
callback
: is a function that is run after the error barstyle
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
style
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property 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
style
of the error bary
component 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
style
is defined as a function.
Default Value:
- The default value for the error bar
y
linestyle
is"solid"
.
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 linewidth
of the error barx
component. It can be one of the following options:- A number that represents line
width
of all error bars. - An array of numbers, each representing the error bar line
width
at each datapoint. - A generator function that returns a number representing the error bar line
width
at each datapoint.
- A number that represents line
callback
: is a function that is run after the error bar linewidth
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: The
width
values must be a positive integer.
Note: If the
width
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
width
of the error barx
component 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
width
is defined as a function.
Default Value:
- The default value for the error bar
x
linewidth
is1
.
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 linewidth
of the error bary
component. It can be one of the following options:- A number that represents line
width
of all error bars. - An array of numbers, each representing the error bar line
width
at each datapoint. - A generator function that returns a number representing the error bar line
width
at each datapoint.
- A number that represents line
callback
: is a function that is run after the error bar linewidth
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: The
width
values must be a positive integer.
Note: If the
width
is defined as an array, its length must be the same as the data arrays.
Note: The
linechart
property generator functions are discussed in deep here.
Returns:
- A number or an array of numbers representing the line
width
of the error bary
component 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
width
is defined as a function.
Default Value:
- The default value for the error bar
y
linewidth
is1
.
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:
linspace
is 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
base
to be a straight line, it can be any arbitrary curve.
Note: It is not necessary for the
data
andbase
curves 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 thex
data generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
dataY()
ordraw()
method from they
data generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
baseX()
ordraw()
method from thex
base generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
baseY()
ordraw()
method from they
base 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.
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 theid
of the dataset.callback
: is a function that is run after theid
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: The
id
is 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
id
of the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
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 theindex
value of the dataset.callback
: is a function that is run after theindex
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: The draw order of datasets with the same
index
value will be indeterminate.
Returns:
- A number that represents the
index
of the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
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 areaenable
property 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
enable
status of the area. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enable
property of the area istrue
.
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.
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.
This method lets you get ot set the x
component of the data
curve.
Method:
dataX()
dataX(data)
dataX(data, callback)
Where:
data
: represents thex
component of thedata
curve. 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
area
data generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
x
component of thedata
curve 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.
This method lets you get ot set the y
component of the data
curve.
Method:
dataY()
dataY(data)
dataY(data, callback)
Where:
data
: represents they
component of thedata
curve. 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
area
data generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
y
component of thedata
curve 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.
This method lets you get ot set the x
component of the base
curve.
Method:
baseX()
baseX(base)
baseX(base, callback)
Where:
base
: represents thex
component of thebase
curve. 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
base
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
baseX()
nordraw()
should be called from within thebase
function, 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
area
data generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
x
component of thebase
curve 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.
This method lets you get ot set the y
component of the base
curve.
Method:
baseX()
baseX(base)
baseX(base, callback)
Where:
base
: represents they
component of thebase
curve. 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
base
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
baseY()
nordraw()
should be called from within thebase
function, 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
area
data generator functions are discussed in deep here.
Returns:
- An array of numbers representing the
y
component of thebase
curve 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.
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 thex
axis that the dataset is goin to use. It can be"primary"
or"secondary"
.y
: a string representing they
axis 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"
}
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 thedata
andbase
must be interpreted inpolar
coordinates.callback
: is a function that is run after thepolar
property 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:
dataX
will be interpreted as the radial coordinates of thedata
curve.dataY
will be interpreted as the angular coordinates of thedata
curve.baseX
will be interpreted as the radial coordinates of thebase
curve.baseY
will be interpreted as the angular coordinates of thebase
curve.
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
polar
property 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
.
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 areacolor
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: The color can be in the format "#rrggbb" or be any of the standard color names.
Returns:
- A string that represents the area
color
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 color
is "#0043e0"
.
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 areaopacity
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: The opacity must be a number between 0 and 1.
Returns:
- A number that represents the area
opacity
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 opacity
is 0.3
.
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
,meshY
anddata
arrays 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:
linspace
andmeshgrid
are 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
x
andy
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
meshX()
otdraw()
methods from thex
mesh generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
meshY()
otdraw()
methods from they
mesh generator because that will create a circular dependency and the program will stop.
Note: It is safe to call the
meshX()
method from within they
mesh 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 thex
position of the datapoint.y
: is they
position of the datapoint.i
: is thei
index corresponding to thex
andy
values.j
: is thej
index corresponding to thex
andy
values.meshX
: is the completemeshX
array.meshY
: is the completemeshY
array.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 thex
position of the datapoint.y
: is they
position of the datapoint.i
: is thei
index corresponding to thex
andy
values.j
: is thej
index corresponding to thex
andy
values.data
: is the completedata
array.meshX
: is the completemeshX
array.meshY
: is the completemeshY
array.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:
mapping
is a utility function, you can learn more about this functions on the extras section.
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 theid
of the dataset.callback
: is a function that is run after theid
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: The
id
is 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
id
of the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
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 theindex
value of the dataset.callback
: is a function that is run after theindex
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: The draw order of datasets with the same
index
value will be indeterminate.
Returns:
- A number that represents the
index
of the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
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 theheatmap
must be enabled.callback
: is a function that is run after the heatmapenable
property 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
enable
status of theheatmap
. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enable
property of theheatmap
istrue
.
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.
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.
This method lets you set or get the heatmap
meshX
array.
Method:
meshX()
meshX(mesh)
meshX(mesh, callback)
Where:
mesh
: represents thex
positions of theheatmap
datapoints. 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
heatmap
mesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
x
coordinate 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.
This method lets you set or get the heatmap
meshY
array.
Method:
meshY()
meshY(mesh)
meshY(mesh, callback)
Where:
mesh
: represents they
positions of theheatmap
datapoints. 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
heatmap
mesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
y
coordinate 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.
This method lets you set or get the heatmap
data
array.
Method:
data()
data(data)
data(data, callback)
Where:
data
: represents the values of theheatmap
datapoints. 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
heatmap
data 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.
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 thex
axis that the dataset is goin to use. It can be"primary"
or"secondary"
.y
: a string representing they
axis 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"
}
This method lets you set or get the color
of the heatmap
.
Method:
color()
color(option)
color(option, callback)
Where:
option
: represents thecolor
at 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
color
at each datapoint. - A generator function that returns a string representing the
color
at 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 thecolor
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: The
color
string can be in the format "#rrggbb" or be any of the standard color names.
Note: If the
color
is defined as a marix, its dimensions must be the same as the data arrays.
Note: The
heatmap
property 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
color
at 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
color
is defined as a function.
Default Value:
- The default value for
heatmap
color
is"viridis"
.
This method lets you set or get the opacity
of the heatmap
.
Method:
opacity()
opacity(option)
opacity(option, callback)
Where:
option
: represents theopacity
of theheatmap
. It can be one of the following options:- A number that represents the
opacity
of all datapoints. - A two-dimensional matrix of numbers, each representing the
opacity
at each datapoint. - A generator function that returns a number representing the
opacity
at each datapoint.
- A number that represents the
callback
: is a function that is run after theopacity
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: The
opacity
values must be a number between 0 and 1.
Note: If the
opacity
is defined as a matrix, its dimensions must be the same as the data arrays.
Note: The
heatmap
property generator functions are discussed in deep here.
Returns:
- A number or a two-dimensional matrix of numbers representing the
opacity
of theheatmap
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
opacity
is defined as a function.
Default Value:
- The default value for
heatmap
opacity
is1
.
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 theheatmap
should besmooth
.callback
: is a function that is run after thesmooth
property 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
smooth
property if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for
smooth
property isfalse
.
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
,dataX
anddataY
arrays 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:
linspace
andmeshgrid
are 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
x
andy
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
meshX()
otdraw()
methods from thex
mesh generator because that will create a circular dependency and the program will stop.
Warning: You must not call the
meshY()
otdraw()
methods from they
mesh generator because that will create a circular dependency and the program will stop.
Note: It is safe to call the
meshX()
method from within they
mesh 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 thex
position of the datapoint.y
: is they
position of the datapoint.i
: is thei
index corresponding to thex
andy
values.j
: is thej
index corresponding to thex
andy
values.meshX
: is the completemeshX
array.meshY
: is the completemeshY
array.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 datapointx
component.vectorY
: is the value of the datapointy
component.x
: is thex
position of the datapoint.y
: is they
position of the datapoint.i
: is thei
index corresponding to thex
andy
values.j
: is thej
index corresponding to thex
andy
values.dataX
: is the completedataX
array.dataY
: is the completedataY
array.meshX
: is the completemeshX
array.meshY
: is the completemeshY
array.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:
colorMap
is a utility function, you can learn more about this functions on the extras section.
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 theid
of the dataset.callback
: is a function that is run after theid
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: The
id
is 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
id
of the dataset if no arguments are pass. - A reference to the dataset object from which the method is called upon.
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 theindex
value of the dataset.callback
: is a function that is run after theindex
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: The draw order of datasets with the same
index
value will be indeterminate.
Returns:
- A number that represents the
index
of the dataset if no aguments are pass. - A reference to the dataset object from which the method is called upon.
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 theheatmap
must be enabled.callback
: is a function that is run after the heatmapenable
property 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
enable
status of thevectorfield
. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default values for the
enable
property of thevectorfield
istrue
.
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.
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.
This method lets you set or get the vectorfield
meshX
array.
Method:
meshX()
meshX(mesh)
meshX(mesh, callback)
Where:
mesh
: represents thex
positions of thevectorfield
datapoints. 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
vectorfield
mesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
x
coordinate 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.
This method lets you set or get the vectorfield
meshY
array.
Method:
meshY()
meshY(mesh)
meshY(mesh, callback)
Where:
mesh
: represents they
positions of thevectorfield
datapoints. 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
vectorfield
mesh generator functions are discussed in deep here.
Returns:
- A two-dimensional matrix representing the
y
coordinate 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.
This method lets you set or get the vectorfield
dataX
array.
Method:
dataX()
dataX(data)
dataX(data, callback)
Where:
data
: represents the values of thevectorfield
datapoints. 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
vectorfield
data 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.
This method lets you set or get the vectorfield
dataY
array.
Method:
dataY()
dataY(data)
dataY(data, callback)
Where:
data
: represents the values of thevectorfield
datapoints. 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
vectorfield
data 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.
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 thex
axis that the dataset is goin to use. It can be"primary"
or"secondary"
.y
: a string representing they
axis 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"
}
This methd lets you set or get the color
of the vectorfield
.
Method:
color()
color(option)
color(option, callback)
Where:
color
: represent thecolor
of thevectorfield
. It can be one of the following options:- A string that represents the
color
of all datapoints. - A two-dimensional matrix of strings, each representing the
color
at each datapoint. - A generator function that returns a string representing the
color
at each datapoint.
- A string that represents the
callback
: is a function that is run after thecolor
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: The
color
can be in the format "#rrggbb" or be any of the standard color names.
Note: If the
color
is defined as a marix, its dimensions must be the same as the data arrays.
Note: The
vectorfield
property generator functions are discussed in deep here.
Returns:
- A string or a two-dimensional matrix of strings representing the
color
at 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
color
is defined as a function.
Default Value:
- The default value for
vectorfield
color
is"#303030"
.
This method lets you set or get the opacity
of the vectorfield
.
Method:
opacity()
opacity(option)
opacity(option, callback)
Where:
option
: represents theopacity
of thevectorfield
. It can be one of the following options:- A number that represents the
opacity
of all datapoints. - A two-dimensional matrix of numbers, each representing the
opacity
at each datapoint. - A generator function that returns a number representing the
opacity
at each datapoint.
- A number that represents the
callback
: is a function that is run after theopacity
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: The
opacity
values must be a number between 0 and 1.
Note: If the
opacity
is defined as a matrix, its dimensions must be the same as the data arrays.
Note: The
vectorfield
property generator functions are discussed in deep here.
Returns:
- A number or a two-dimensional matrix of numbers representing the
opacity
of thevectorfield
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
opacity
is defined as a function.
Default Value:
- The default value for
vectorfield
opacity
is1
.
This method lets you set or get the style
of the vectorfield
.
Method:
style()
style(option)
style(option, callback)
Where:
option
: represents thestyle
of thevectorfield
. It can be one of the following options:- A string that represents the
style
of all datapoints. - A two-dimensional matrix of strings, each representing the
style
at each datapoint. - A generator function that returns a string representing the
style
at each datapoint.
- A string that represents the
callback
: is a function that is run after thestyle
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
style
is defined as an array, its length must be the same as the data arrays.
Note: The
vectorfield
property 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
style
of thevectorfield
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
style
is defined as a function.
Default Value:
- The default value for
vectorfield
opacity
is1
.
This method lets you set or get the line width
of the vectorfield
.
Method:
width()
width(option)
width(option, callback)
Where:
option
: represents the linewidth
of thevectorfield
. It can be one of the following options:- A number that represents the line
width
of all datapoints. - A two-dimensional matrix of numbers, each representing the line
width
at each datapoint. - A generator function that returns a number representing the line
width
at each datapoint.
- A number that represents the line
callback
: is a function that is run after thewidth
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: The
width
values must be positive integer.
Note: If the
width
is defined as a matrix, its dimensions must be the same as the data arrays.
Note: The
vectorfield
property generator functions are discussed in deep here.
Returns:
- A number or a two-dimensional matrix of numbers representing the
width
of thevectorfield
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
width
is defined as a function.
Default Value:
- The default value for
vectorfield
width
is1
.
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
dataX
nordataY
values, 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 thenormalize
property should be enable.callback
: is a function that is run after thenormalize
property 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
normalize
property if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for
vectorfield
normalize
istrue
.
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 themaxLength
property value.callback
: is a function that is run after thenormalize
property 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
normalize
property is set totrue
.
Returns:
- A number that represents the
maxLength
property if no arguments are pass. - A reference to the dataset object from which the method is called upon.
Default Value:
- The default value for
vectorfield
maxLength
is20
.
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.
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 theid
of thecolorbar
.callback
: is a function that is run after theid
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.
Note: The
id
is 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
id
of thecolorbar
if no arguments are pass. - A reference to the
colorbar
object from which the method is called upon.
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 thecolorbar
must be enabled.callback
: is a function that is run after the colorbarenable
property 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
enable
status of thecolorbar
. - A reference to the colorbar object from which the method is called upon.
Default Value:
- The default values for the
enable
property of thecolorbar
istrue
.
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
heatmap
dataset. - 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 thedata
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.
Note: The
position
property must be a number between 0 and 1, being 0 the start of the color bar, and 1 the end.
Note: The
color
string 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
,label
andposition
of each mark in thecolorbar
if 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
colorbar
will be a blank rectangle.
This method lets you set or get the colorbar
position
.
Method:
position()
position(option)
position(option, callback)
Where:
option
: represents thecolobar
position
. It can be one of the following options:- A string with one of the predefined positions:
"x-start"
will position thecolorbar
at the left."x-end"
will position thecolorbar
at the right."y-start"
will position thecolorbar
at the bottom."y-end"
will position thecolorbar
at the top.
- An object with the following properties to position the
colorbar
manually:x
: a number with thecolorbar
x
coordinate.y
: a number with thecolorbar
y
coordinate.orientation
: a string with thecolorbar
orientation
. It can be"vertial"
or"horizontal"
.
- A string with one of the predefined positions:
callback
: is a function that is run after theposition
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.
Note: If the
position
is set to one of the predefined positions, the graphmargins
will be changed to accommodate thecolorbar
. This will potentially change the axis aspect ratio.
Note: The
x
andy
values represent the coordinates of thecolorbar
top left corner relative to the client rect.
Returns:
- A string representing the
colorbar
position
if 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
colorbar
position
is"x-end"
.
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 theticks
values 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
density
property has no effect if thedata
is defined manually.
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.
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
ticks
properties 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
}
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 thecolorbar
shouldreverse
the data.callback
: is a function that is run after thereverse
property 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
- A reference to the colorbar object from which the method is called upon.
Default Values:
- The default values for the
reverse
property isfalse
.
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 thewidth
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.
Note: The width is represented in pixels, so it must be a positive integer.
Note: If the
colorbar
is inhorizontal
position, this values will make reference to the colorbar height instead
Returns:
- A number that represents the
colorbar
width
if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
- The default value for the
width
property is20
.
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 thecolorbar
size
.callback
: is a function that is run after thesize
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.
Note: The
size
must 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
colorbar
size
if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
- The default value for the
size
property is0.93
.
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 thecolorbar
opacity
.callback
: is a function that is run after theopacity
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.
Note: The
opacity
must be a number between 0 and 1.
Returns:
- A number that represents the
colorbar
opacity
if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Values:
- The default value for the
opacity
property is1
.
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 theborder
properties 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
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
border
properties 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
}
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 thelabel
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.
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
Note: The
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The
specifier
can 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
specifier
options.
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
label
properties 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"
}
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 thetitle
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.
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
Note: The
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The
specifier
can 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
specifier
options.
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
reverse
property is only releventa when thecolorbar
is invertical
orientation.
Returns:
- An object with the
title
properties 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
}
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 thetext
properties 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
opacity
must be a number between 0 and 1
Note: The
font
value can be any font or stack of fonts available in the browser. Its advised to use safe web fonts.
Note: The
size
value can be any valid css size including em, rem, etc.
Note: The
specifier
can 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
specifier
options.
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
text
properties 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"
}
}
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 thecolorbar
unit
.callback
: is a function that is run after theunit
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 string representing the
colorbar
unit
if no argument is pass. - A reference to the colorbar object from which the method is called upon.
Default Value:
- The default value for the
unit
property is""
.
This method returns the metrics of the colorbar
.
Method:
metrics()
Returns:
- An object with the following properties:
x
: Thex
coordinate of thecolorbar
.y
: They
coordinate of thecolorbar
.width
: Thewidth
of thecolorbar
.height
: Theheight
of thecolorbar
.
Note: The
x
andy
coordinates make reference to the top left corner of the wholecolorbar
, including the labels and title.
Note: The
x
andy
coordinates are relatives to theclient rect
.
Note: The
width
andheight
values make reference to the width and height of the wholecolorbar
, including the labels and title.
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
linechart
will 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.
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 theid
of thelegend
.callback
: is a function that is run after theid
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.
Note: The
id
is 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
id
of thelegend
if no arguments are pass. - A reference to the
legend
object from which the method is called upon.
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 thelegend
must be enabled.callback
: is a function that is run after theenable
property 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
enable
status of thelegend
. - A reference to the
legend
object from which the method is called upon.
Default Value:
- The default values for the
enable
property of thelegend
istrue
.
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 theid
string of the dataset.label
: is a string representing the label of the dataset.text
: an object with thetext
properties 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 thedata
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:
- An array of object, with the
data
properties of each dataset if no argument is pass. - A reference to the
legend
object 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 : ""
}
}
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 ofcolumns
in thelegend
.callback
: is a function that is run after thecolumns
are 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
columns
in thelegend
if no argument is pass. - A reference to the
legend
object from which the method is called upon.
Default Value:
- The default value for the
columns
property is1
.
This method lets you set or get the legend
position
.
Method:
position()
position(option)
position(option, callback)
Where:
option
: represents thelegend
position
. 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
legend
manually:x
: a number with thelegend
x
coordinate.y
: a number with thelegend
y
coordinate.
- A string with one of the predefined positions:
callback
: is a function that is run after theposition
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.
Note: The
x
andy
values represent the coordinates of thelegend
top left corner relative to the graph rect.
Returns:
- A string representing the
legend
position
if 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
legend
position
is"top-right"
.
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 thewidth
in pixels of thelegend
markers.callback
: is a function that is run after thewidth
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 number representing the
width
of thelegend
markers if no argument is pass. - A reference to the
legend
object from which the method is called upon.
Default Value:
- The default value for the
width
property is20
.
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 theposition
of thelegend
. It can be one of the following options:"start"
."center"
."end"
.
callback
: is a function that is run after thetitle
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:
- An object with the value of the
title
properties if no argument is pass. - A reference to the
legend
object 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"
}
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 thebackground
color.opacity
: a number representing thebackground
opacity.
callback
: is a function that is run after thebackground
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.
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
background
property values of thelegend
if no argument is pass. - A reference to the
legend
object 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
}
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 theborder
properties 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 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
border
properties if no argument is pass. - A reference to the
legend
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
}
This method returns the metrics of the legend
.
Method:
metrics()
Returns:
- An object with the following properties:
x
: Thex
coordinate of thelegend
.y
: They
coordinate of thelegend
.width
: Thewidth
of thelegend
.height
: Theheight
of thelegend
.
Note: The
x
andy
coordinates make reference to the top left corner of thelegend
.
Note: The
x
andy
coordinates are relatives to thegraph rect
.
Note: The
width
andheight
values make reference to the width and height of thelegend
.
This library includes some extra functionality to make the use of the graphs easier.
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 newgraph2D
object.linechart
: An array with alllinechart
objects present in the graph.area
: An array with allarea
objects present in the graph.heatmap
: An array with allheatmap
objects present in the graph.vectorfield
: An array with allvectorfield
objects present in the graph.colorbar
: An array with allcolorbar
objects present in the graph.legend
: An array with alllegend
objects 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.
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:
n
must be a positive integer.
Note: The
start
andend
arguments can hold the same vale, the result will be an array withn
copies 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]
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 thex
domain. It can be one of the following options:- An array of numbers, with each value representing one
x
coordinate. - An object with the properties:
start
: a number representing thestart
of thex
domain.end
: a number representing theend
of thex
domain.n
: a number representing the number of times thex
domain will be divided into.
- An array of numbers, with each value representing one
y
: represents they
domain. It can be one of the following options:- An array of numbers, with each value representing one
y
coordinate. - An object with the properties:
start
: a number representing thestart
of they
domain.end
: a number representing theend
of they
domain.n
: a number representing the number of times they
domain will be divided into.
- An array of numbers, with each value representing one
Note: If the
x
ory
arguments 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
meshX
matrix 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
meshX
andmeshY
depend directly on the lenght ofx
andy
.
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 themapping
domain
.to
: an array of two numbers representing themapping
range
.type
: a string representing themapping
type
. It can be one of the following options:"linear"
."log"
."sqr"
.
base
: a number representing thebase
if using alog
transformation.
Returns:
A mapping
object with the following properties:
map
: a funtion that takes a number from thedomain
and returns the correspondingrange
value.invert
: a function that takes a number from therange
and returns the correspondingdomain
value.domain
: an array of two numbes reprenting thedomain
.range
: an array of two numbes reprenting therange
.type
: a string representing themapping
type
.base
: a number representing themapping
base
.
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
log
type thedomain
must be strictly positive or negative and dont contain the zero.
Warning: When using the
str
type, thedomain
must 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
}
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
domain
will be indeterminate.
Note: The colors defined in the
range
must 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 thedomain
and 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",
}
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 thedomain
start.to
: a number indicating thedomain
end.type
: a string representing thecolorMap
type. 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
domain
will 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.
type | Result |
---|---|
"viridis" |
|
"plasma" |
|
"magma" |
type | Result |
---|---|
"magnet" |
|
"swamp" |
|
"fairy" |
|
"inv-magnet" |
|
"inv-swamp" |
|
"inv-fairy" |
type | Result |
---|---|
"fire" |
|
"royal" |
|
"hsv" |
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.