SVG Plotting
The jQuery SVG plotting extension (jquery.svgplot.js
) provides support for
plotting numeric functions with SVG.
Since 1.3.0.
The SVG plotting entry point is within the SVG wrapper object as svg.plot
,
which allows you to configure the plot and render it using SVG.
Many functions return the plot object so that further calls may be chained together.
svg.plot.noDraw().title('Functions').
addFunction('sine', Math.sin, 'blue', 3).
addFunction('cosine', Math.cos, [-Math.PI, Math.PI], 20, 'red', 3).
addFunction('decaying', decay, 'green', 3).
areaFormat('ivory', 'gray').
gridlines({stroke: 'gray', strokeDashArray: '2,2'}, 'gray').
status(setStatus);
svg.plot.xAxis.scale(-1, 3.5).ticks(1, 0.2);
svg.plot.yAxis.scale(-1.5, 1.5).ticks(1, 0.2);
svg.plot.legend.settings({fill: 'lightgoldenrodyellow', stroke: 'gray'});
svg.plot.noDraw().
legend.show(true).area(legendAreas[plotLegend]).end().
xAxis.scale(plotZooms[plotZoom][0], plotZooms[plotZoom][1]).end().
yAxis.scale(plotZooms[plotZoom][2], plotZooms[plotZoom][3]).end().
chartArea(chartAreas[plotLegend]).equalXY(plotEqual).redraw();
addFunction | area | container | equalXY | format | functions | gridlines | noDraw | redraw | status | title
Signature | Returns | Comments |
---|---|---|
addFunction(name, fn, range, points, stroke, strokeWidth, settings) | SVGPlot object | Add a function to be shown on the plot. The details are encapsulated in a
SVGPlotFunction object.name (string, optional) is the name of this function.fn (function) is the function to be plotted.
It takes a single numeric value as a parameter and returns a numeric value.range (number[2], optional) is the minimum and maximum x-values to be plotted.
If not specified it defaults to the full range of the x-axis.points (number, optional) is the number of points within the range to plot.
More points produce a smoother curve, but generate a larger SVG document.
If not specified it defaults to 100.stroke (string, optional) is the colour of the plotted lines.
If not specified it defaults to black.strokeWidth (number, optional) is the width of the plotted lines.settings (object, optional) is additional
settings (SVG attributes) for the plotted values.
|
area(left, top, right, bottom) | SVGPlot object or number[4] | Set the insets for the main plot area.
If the parameter value is less than or equal to 1 it is taken as the proportion
of the width/height. If more than 1 it is the number of pixels.
The default is [0.1, 0.1, 0.8, 0.9].left (number) is the left position or
(number[4]) all the positions in an array.top (number) is the top position (omitted if left is an array).right (number) is the right position (omitted if left is an array).bottom (number) is the bottom position (omitted if left is an array).
If no parameters are provided, the function returns the current plot area.
|
container(cont) | SVGPlot object or SVG element | Set the container, which should be a svg element,
for the plot elements. A default container is created automatically.cont (SVG element) is the new plot container.
If no parameters are provided, the function returns the current plot container.
Since 1.3.1. |
equalXY(value) | SVGPlot object or boolean | Set whether the units along the x- and y-axes are of equal size.
value (boolean) is the new setting.
When true (default) the plot area may be reduced along one axis
to fit the axis and maintain a constant unit size.
When false the axes expand to fill the plot area available.
If no parameters are provided, the function returns the current equal X/Y setting.
|
format(fill, stroke, settings) | SVGPlot object or format object | Set the background of the plot area.fill (string) is how to fill the plot background (default 'none').stroke (string, optional) is the colour of the outline (default 'black').settings (object, optional) is additional
formatting (SVG attributes) for the plot background.
If no parameters are provided, the function returns the current area format as an object with at least fill and stroke attributes.
|
functions(i) | SVGPlotFunction or SVGPlotFunction[] | Retrieve a single function or the current set of functions to be plotted.i (number, optional) is the index of the required function
If no parameters are provided, the function returns the list of all functions.
|
gridlines(xSettings, ySettings) | SVGPlot object or object[2] | Set the gridlines formatting for the plot. The default is no gridlines.xSettings (string) is the colour of the gridlines along the x-axis, or
(object) formatting (SVG attributes) for the gridlines
along the x-axis, or null for none.ySettings (string) is the colour of the gridlines along the y-axis, or
(object) formatting (SVG attributes) for the gridlines
along the y-axis, or null for none.
If no parameters are provided, the function returns the current gridline format objects in an array.
|
noDraw() | SVGPlot object | Suppress drawing of the plot until redraw() is called.
Normally the plot is redrawn whenever anything changes, but this
function allows multiple changes to be made before redrawing.
|
redraw() | SVGPlot object | Redraw the entire plot with the current settings and values.
Normally the plot is redrawn whenever anything changes, but this
function signals a redraw after pausing it with noDraw() .
|
status(onstatus) | SVGPlot object | Set the callback function for status updates.
When the mouse hovers over a plot line, a call is made to
this function with name of the current series.
On mouse exit the function is called with a blank value.onstatus (function) is the callback function.
Since 1.4.3 - function no longer has to be global. |
title(value, offset, colour, settings) | SVGPlot object or object | Set the title of the plot.value (string) is the title (default '').offset (number, optional) is the pixel offset from the
top of the plot (default 25).colour (string, optional) is the fill colour for the title.settings (object, optional) is formatting (SVG attributes)
for the title (default textAnchor: 'middle').
If no parameters are provided, the function returns the current title settings as an object with value , offset ,
and settings attributes (colour appears in
settings as fill ).
Since 1.4.2 - added colour . |
The following variables are available for general use.
Name | Type | Comments |
---|---|---|
legend | SVGPlotLegend | The plot's legend.
|
xAxis | SVGPlotAxis | The plot's x-axis.
|
yAxis | SVGPlotAxis | The plot's y-axis.
|