JSPlot Documentation: Plotting data
Tutorial
API reference
Introduction to the graph class
This page explains how to plot data on a graph, using the JSPlot_Graph and JSPlot_DataSet classes. It assumes you have read the previous page, which explains how to display a canvas.
Data sets
Each set of data on a graph is a JSPlot_DataSet, created with three arguments: a title to show in the graph's legend, an object of styling options, and the data itself. The data is a list of rows, each of which is a list of numbers. For most plot styles, each row contains an (x, y) pair.
A JSPlot_Graph takes a list of data sets, followed by an object of settings. When a graph shows several data sets, each is automatically given a different color. Set key to true to show a legend:
var temperatures_london = [[1, 8], [2, 9], [3, 12], [4, 15], [5, 18], [6, 21],
[7, 23], [8, 23], [9, 20], [10, 16], [11, 11], [12, 8]];
var temperatures_oslo = [[1, -1], [2, 0], [3, 4], [4, 10], [5, 16], [6, 20],
[7, 22], [8, 21], [9, 16], [10, 9], [11, 4], [12, 0]];
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("London", {}, temperatures_london),
new JSPlot_DataSet("Oslo", {}, temperatures_oslo)
], {
"key": true,
"keyPosition": "bm"
})
}, {});
canvas.renderToCanvas($("#tutorial_data_sets")[0]);
The keyPosition setting places the legend: "bm" means bottom-middle. A data set with a blank title is left out of the legend. The styling options, such as colors and line styles, are covered on the next page, Graph plotting styles.
Titles, labels and axis ranges
The graph's title setting writes a title above it. Each axis is configured by passing an object of settings for it, using the keys x1_axis (the bottom axis) and y1_axis (the left axis). The most commonly used axis settings are:
- label – the text to write alongside the axis.
- min and max – the range of the axis. If they are not set, the axis scales itself automatically to fit the data.
- log – set to true for a logarithmic axis.
var data = [];
for (var n = 1; n < 20; n++) data.push([n, Math.pow(2, n)]);
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("", {"plotStyle": "points"}, data)
], {
"title": "Powers of two",
"x1_axis": {
"label": "Exponent n",
"min": 0,
"max": 20
},
"y1_axis": {
"label": "Two to the power n",
"log": true
}
})
}, {});
canvas.renderToCanvas($("#tutorial_titles_and_axes")[0]);
The full list of axis settings is in the JSPlot_Axis API reference. It includes settings to control the placement of tick marks, to reverse an axis, and to rotate its labels.
Plotting against a second axis
A graph has a second horizontal axis along its top (x2), and a second vertical axis along its right-hand side (y2). These are useful for plotting two quantities with different units on the same graph. To plot a data set against a second axis, set axis1 (the axis for the first column of data) or axis2 (the axis for the second column) in its styling options:
var rainfall = [[1, 55], [2, 41], [3, 42], [4, 44], [5, 49], [6, 45],
[7, 45], [8, 50], [9, 49], [10, 69], [11, 59], [12, 55]];
var temperature = [[1, 8], [2, 9], [3, 12], [4, 15], [5, 18], [6, 21],
[7, 23], [8, 23], [9, 20], [10, 16], [11, 11], [12, 8]];
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("Rainfall", {"plotStyle": "boxes"}, rainfall),
new JSPlot_DataSet("Temperature", {"axis2": "y2", "plotStyle": "linespoints"}, temperature)
], {
"key": true,
"keyPosition": "above",
"x1_axis": {"label": "Month"},
"y1_axis": {"label": "Rainfall / mm", "min": 0, "max": 80},
"y2_axis": {"label": "Temperature / degrees C", "min": 0, "max": 25}
})
}, {});
canvas.renderToCanvas($("#tutorial_second_axis")[0]);
Dates and times
To plot data against time, set an axis's dataType to "timestamp". The data should then contain Unix timestamps – the number of seconds since 1 January 1970 (UTC) – which are labelled as dates and times along the axis:
// One reading per day through January 2026
var start = Date.UTC(2026, 0, 1) / 1000;
var data = [];
for (var day = 0; day < 31; day++) {
data.push([start + day * 86400, 10 + 5 * Math.sin(day / 3)]);
}
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("Daily readings", {"plotStyle": "linespoints"}, data)
], {
"key": true,
"x1_axis": {
"dataType": "timestamp"
},
"y1_axis": {
"min": 0,
"max": 20
}
})
}, {});
canvas.renderToCanvas($("#tutorial_dates_and_times")[0]);
Interactive graphs
Graphs can respond to the user. Set the graph's interactiveMode to "pan", and turn on scrollEnabled for an axis, to let the user drag the graph along that axis. Turning on zoomEnabled as well lets them zoom in and out with the mouse wheel, or by pinching on a touchscreen. Try dragging this graph sideways:
var data = [];
for (var x = 0; x <= 100; x += 0.5) data.push([x, Math.sin(x) * Math.exp(-x / 30)]);
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("Damped oscillation", {}, data)
], {
"key": true,
"interactiveMode": "pan",
"x1_axis": {
"min": 0,
"max": 20,
"scrollEnabled": true,
"zoomEnabled": true
}
})
}, {});
canvas.renderToCanvas($("#tutorial_interactive_graphs")[0]);
The scrollMin and scrollMax axis settings limit how far the user can scroll. On three-dimensional graphs, an interactiveMode of "rotate" lets the user rotate the graph by dragging it.
Annotations
Text labels and arrows can be drawn over a graph using the JSPlot_Label_Text and JSPlot_Label_Arrow classes. Their positions are given in the graph's own coordinates, measured along the axes you pass them in the axis_x, axis_y and axis_z settings. All three must be set, even on two-dimensional graphs, so the graph needs to be created first and the annotations added to it afterwards:
var data = [];
for (var x = 0; x <= 10; x += 0.1) data.push([x, x * x * Math.exp(-x)]);
var graph = new JSPlot_Graph([
new JSPlot_DataSet("", {}, data)
], {});
graph.configure({
"annotations": [
new JSPlot_Label_Text({
"position": [5, 0.5],
"text": "Peak at x = 2",
"fontSize": 16,
"h_align": "left",
"axis_x": graph.axes["x1"],
"axis_y": graph.axes["y1"],
"axis_z": graph.axes["z1"]
}),
new JSPlot_Label_Arrow({
"origin": [4.9, 0.5],
"target": [2.1, 0.54],
"strokeLineWidth": 2,
"axis_x": graph.axes["x1"],
"axis_y": graph.axes["y1"],
"axis_z": graph.axes["z1"]
})
]
});
var canvas = new JSPlot_Canvas({"my_graph": graph}, {});
canvas.renderToCanvas($("#tutorial_annotations")[0]);
For more examples of graphs, see the demos.