SVG Graphing
The jQuery SVG graphing extension (jquery.svggraph.js
) provides support for
drawing graphs with SVG.
The SVG graphing entry point is within the SVG wrapper object as svg.graph
,
which allows you to configure the graph and render it using SVG.
Many functions return the graph object so that further calls may be chained together.
var defs = svg.defs();
svg.linearGradient(defs, 'fadeBlue', [[0, 'lightblue'], [1, 'blue']]);
svg.linearGradient(defs, 'fadeRed', [[0, 'pink'], [1, 'red']]);
svg.linearGradient(defs, 'fadeGreen', [[0, 'lightgreen'], [1, 'green']]);
svg.graph.noDraw().title('Browser Usage').
addSeries('IE', [95.97, 91.80, 88.16, 86.64], 'lightblue', 'blue', 3).
addSeries('Netscape', [3.39, 2.83, 1.61, 0.00], 'pink', 'red', 3).
addSeries('Firefox', [0.00, 4.06, 8.13, 9.95], 'lightgreen', 'green', 3).
format('ivory', 'gray').
gridlines({stroke: 'gray', strokeDashArray: '2,2'}, 'gray').
status(setStatus);
svg.graph.noDraw().
xAxis.title('Year').scale(0, 3).
ticks(1, 0).labels(['2002', '2004', '2005', '2006']).end().
yAxis.title('Percentage').scale(-5, 105).ticks(10, 5).end().
legend.settings({fill: 'lightgoldenrodyellow', stroke: 'gray'}).
show(legendPos).area(legendArea[legendPos]).end().
series(0).format(fills[0][seriesFill]).end().
series(1).format(fills[1][seriesFill]).end().
series(2).format(fills[2][seriesFill]).end().
area(chartArea[legendPos]).
type(chartType, {explode: 2, explodeDist: 10}).redraw();
addSeries | area | chartArea | chartFormat | chartOptions | chartType | container | format | gridlines | noDraw | options | redraw | series | status | title | type
Signature | Returns | Comments |
---|---|---|
addSeries(name, values, fill, stroke, strokeWidth, settings) | graph object | Add a series of values to be plotted on the graph. The details are encapsulated in a
SVGGraphSeries object.name (string, optional) is the name of this series.values (number[]) is the values to be plotted.fill (string) is how the plotted values are filled.
The default is 'green'.stroke (string, optional) is the colour of the plotted lines.
The default is 'black'.strokeWidth (number, optional) is the width of the plotted lines.
The default is 1.settings (object, optional) is additional settings
(SVG attributes) for the plotted values.
|
area(left, top, right, bottom) | graph object or number[4] | Set the insets for the main chart 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.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).svg.graph.area(0.1, 0.1, 0.9, 0.8); If no parameters are provided, the function returns the current chart area.
Since 1.3.0 - previously you used chartArea . |
chartArea(left, top, right, bottom) | graph object or number[4] | Since 1.3.0 - deprecated in
favour of area . |
chartFormat(fill, stroke, settings) | graph object or format object | Since 1.3.0 - deprecated in
favour of format . |
chartOptions(options) | graph object or options object | Since 1.3.0 - deprecated in
favour of options . |
chartType(id, options) | graph object or string | Since 1.3.0 - deprecated in
favour of type . |
container(cont) | graph object or SVG element | Set the container, which should be a svg element,
for the graph elements. A default container is created automatically.cont (SVG element) is the new graph container.
If no parameters are provided, the function returns the current chart container.
Since 1.3.1. |
format(fill, stroke, settings) | graph object or format object | Set the background of the graph chart.fill (string) is how to fill the chart background.stroke (string, optional) is the colour of the outline.settings (object, optional) is additional formatting
(SVG attributes) for the chart background.
If no parameters are provided, the function returns the current chart format, with at least the fill attribute.
Since 1.3.0 - previously you used chartFormat . |
gridlines(xSettings, ySettings) | graph object or object[2] | Set the gridlines formatting for the graph chart.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.
|
noDraw() | graph object | Suppress drawing of the graph until redraw() is called.
Normally the graph is redrawn whenever anything changes, but this
function allows multiple changes to be made before redrawing.
|
options(options) | graph object or options object | Set additional options
for the particluar chart type.options (object) is the extra options.
If no parameters are provided, the function returns the current chart options.
Since 1.3.0 - previously you used chartOptions . |
redraw() | graph object | Redraw the entire graph with the current settings and values.
Normally the graph is redrawn whenever anything changes, but this
function signals a redraw after pausing it with noDraw() .
|
series(i) | SVGGraphSeries or SVGGraphSeries[] | Retrieve a single series or the current set of series to be plotted on the graph.i (number, optional) is the index of the required series.
If no parameters are provided, the function returns the list of all series.
Since 1.3.0 - added index . |
status(onstatus) | graph object | Set the callback function for status updates.
When the mouse hovers over a series, a call is made to this function
with the series name and the current value as parameters.
On mouse exit the function is called with blank and zero values.onstatus (function) is the callback function.
Since 1.4.3 - function no longer has to be global and now takes two parameters. |
title(value, offset, colour, settings) | graph object or object | Set the title of the graph.value (string) is the title.offset (number, optional) is the pixel offset from the top of the chart.colour (string, optional) is the fill colour for the title.settings (object, optional) is formatting
(SVG attributes) for the title.
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 . |
type(id, options) | graph object or string | Set the type of chart to be rendered.
See
$.svg.graphing.chartTypes()
for the list of available types.id (string) is the ID of the chart type.options (object, optional) is
additional settings for this chart type.
If no parameters are provided, the function returns the current chart type.
Since 1.3.0 - previously you used chartType . |
The following functions are intended for internal use by chart renderers.
They would be called from within the
drawGraph()
function via the graph
parameter.
_drawAxes | _drawAxis | _drawChartBackground | _drawGridlines | _drawLegend | _drawTitle | _getDims | _getPercentageAxis | _getTickOffsets | _getTotals | _showStatus
Signature | Returns | Comments |
---|---|---|
_drawAxes(noX) | null | Draw the axes in their standard configuration.noX (boolean, optional) is true to suppress the x-axes, false to draw it.
|
_drawAxis(axis, id, x1, y1, x2, y2) | null | Draw an axis and its tick marks.axis (
SVGGraphAxis) is the axis definition.id (string) is the identifier for the axis group element.x1 (number) is starting x-coodinate for the axis.y1 (number) is starting y-coodinate for the axis.x2 (number) is ending x-coodinate for the axis.y2 (number) is ending y-coodinate for the axis.
|
_drawChartBackground(noXGrid, noYGrid) | background element | Draw the chart background, including gridlines.noXGrid (boolean, optional) is true to suppress the x-gridlines,
false to draw them.noYGrid (boolean, optional) is true to suppress the y-gridlines,
false to draw them.
|
_drawGridlines(bg, axis, horiz, dims, format) | null | Draw one set of gridlines.bg (element) is the background group element.axis (
SVGGraphAxis) is the axis definition.horiz (boolean) is true if horizontal, false if vertical.dims (number[4]) is the left, top, width, height of the chart area.format (object) is additional settings for the gridlines.
|
_drawLegend() | null | Draw the chart legend based on its settings.
|
_drawTitle() | null | Draw the graph title - centred at the top.
|
_getDims(area) | number[4] | Calculate the actual dimensions of the given area.
Convert the chart area settings into actual
coordinates returned in an array: left, top, width, height.area (number[4], optional) is the area sizings
(left, top, right, bottom), defaults to main chart area.
|
_getPercentageAxis() | SVGGraphAxis | Retrieve the standard percentage axis (localised).
|
_getTickOffsets(axis, bottomRight) | number[2] | Calculate offsets based on axis and tick positions.
Returns the array of offset multipliers (-1..+1) for left/up and right/down strokes.axis (
SVGGraphAxis) is the axis definition.bottomRight (boolean) is true if this axis is appearing on
the bottom or right of the chart area, false if to the top or left.
|
_getTotals() | number[] | Retrieve the totals for each entry across all the series.
This can be used in charts that show relative proportions,
such as stacked columns or rows, and pie charts.
|
_showStatus() | object | Retrieve the SVG attributes for onmouseover and
onmouseout events that display the current series value status.
Add this to your SVG element depicting the series value.
|
The following variables are available for general use.
Name | Type | Comments |
---|---|---|
legend | SVGGraphLegend | The chart's legend.
|
xAxis | SVGGraphAxis | The chart's main x-axis.
|
x2Axis | SVGGraphAxis | The chart's secondary x-axis. Not yet implemented.
|
yAxis | SVGGraphAxis | The chart's main y-axis.
|
y2Axis | SVGGraphAxis | The chart's secondary y-axis. Not yet implemented.
|
The following variables are intended for internal use by chart renderers.
They would be used from within the
drawGraph()
function via the graph
parameter.
Name | Type | Comments |
---|---|---|
_area | number[4] | The sizing for the main chart area. Convert these into actual
dimensions with _getDims() .
|
_chartFormat | object | The format settings for the main chart area.
|
_chartCont | SVG element | The SVG element that contains all the charting elements.
|
_chartOptions | object | Additional options set for the current chart type.
|
_gridlines | object[2] | The format settings for the x- and y-gridlines.
|
_series | SVGGraphSeries[] | The series to be plotted.
|
_title | object | Details about the graph title.
|
_wrapper | SVGWrapper | The attached SVG wrapper.
Since 1.2.0 - previously you used _root . |
Similarly for these constants.
Name | Type | Comments |
---|---|---|
H | number | The index into the dimensions array (_getDims())
for the height.
|
W | number | The index into the dimensions array (_getDims())
for the width.
|
X | number | The index into the dimensions array (_getDims())
for the x-coordinate.
|
Y | number | The index into the dimensions array (_getDims())
for the y-coordinate.
|