JSPlot Documentation: Graph plotting styles
Tutorial
- The plotting canvas
- Plotting data
- Graph plotting styles
- Plotting functions
- Vector graphics
API reference
Tour of the plot styles
Each data set on a graph is drawn in a plot style, such as points, lines or bars. This page shows how to choose a plot style, and how to change the colors, lines and points used to draw it. The full list of plot styles, and the columns of data that each one needs, is in the plot styles API reference.
Choosing a plot style
A data set's plot style is set by the plotStyle option in its styling settings, which is the second argument to JSPlot_DataSet. The default is "lines", which joins the data points with straight lines. "points" draws a symbol at each point, and "linespoints" does both:
var data_points = [], data_lines = [], data_linespoints = [];
for (var x = 1; x <= 10; x++) {
data_points.push([x, Math.sqrt(x)]);
data_lines.push([x, Math.sqrt(x) + 2]);
data_linespoints.push([x, Math.sqrt(x) + 4]);
}
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("points", {"plotStyle": "points"}, data_points),
new JSPlot_DataSet("lines", {"plotStyle": "lines"}, data_lines),
new JSPlot_DataSet("linespoints", {"plotStyle": "linespoints"}, data_linespoints)
], {
"key": true,
"keyPosition": "tl",
"x1_axis": {"min": 0, "max": 11}
})
}, {});
canvas.renderToCanvas($("#tutorial_choosing_a_style")[0]);
Colors, lines and points
Unless you choose otherwise, each data set is drawn in a different color, and each one drawn with points uses a different shape of point. The following styling options override these choices:
- color – the color to draw in, as a JSPlot_Color object made from red, green, blue and opacity values, each between 0 and 1.
- lineType – the style of line: 1 for solid, 2 for dashed, 3 for dotted, and so on up to 9.
- lineWidth – the width of lines.
- pointType – the shape of point, numbered from 0 to 19.
- pointSize – a scaling factor for the size of points.
var data_sets = [];
for (var i = 1; i <= 4; i++) {
var data = [];
for (var x = 0; x <= 10; x += 0.25) data.push([x, i + 0.4 * Math.sin(x)]);
data_sets.push(new JSPlot_DataSet("lineType " + i, {
"lineType": i,
"lineWidth": 2,
"color": new JSPlot_Color(0, 0, 0.6, 1)
}, data));
}
data_sets.push(new JSPlot_DataSet("pointType 5", {
"plotStyle": "points",
"pointType": 5,
"pointSize": 1.5,
"color": new JSPlot_Color(0.8, 0.3, 0, 1)
}, [[1, 5.5], [3, 5.5], [5, 5.5], [7, 5.5], [9, 5.5]]));
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph(data_sets, {
"key": true,
"keyPosition": "right",
"y1_axis": {"min": 0, "max": 6}
})
}, {});
canvas.renderToCanvas($("#tutorial_colors_lines_and_points")[0]);
The full lists of line types and point types are in the styling options API reference.
Error bars
Some plot styles need more than two columns of data. The yerrorbars style reads a third column, giving the size of the uncertainty in each y value, and draws an error bar of that size above and below each point. The yerrorrange style instead reads the lower and upper ends of each error bar from the third and fourth columns. There are matching styles for errors in x, and in both x and y:
// Each row is: x, y, uncertainty in y
var measurements = [[1, 2.1, 0.3], [2, 3.9, 0.4], [3, 6.2, 0.3], [4, 7.8, 0.6], [5, 10.1, 0.5]];
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("Measurements", {"plotStyle": "yerrorbars"}, measurements),
new JSPlot_DataSet("Model", {}, [[0, 0], [6, 12]])
], {
"key": true,
"keyPosition": "tl"
})
}, {});
canvas.renderToCanvas($("#tutorial_error_bars")[0]);
Bar charts
The boxes style draws a bar chart, with a bar for each data point. The bars are filled with the data set's fillColor, and outlined in its color. By default the bars fill the horizontal space available, and rise from zero. The graph's boxWidth setting gives all the bars a fixed width instead, and boxFrom sets the level the bars rise from:
var sales = [[2020, 12], [2021, 15], [2022, 9], [2023, 18], [2024, 21], [2025, 17]];
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("Sales", {
"plotStyle": "boxes",
"color": new JSPlot_Color(0, 0, 0, 1),
"fillColor": new JSPlot_Color(0.3, 0.5, 0.9, 1)
}, sales)
], {
"boxWidth": 0.6,
"key": true,
"keyPosition": "tl",
"x1_axis": {"label": "Year"},
"y1_axis": {"label": "Sales", "min": 0, "max": 25}
})
}, {});
canvas.renderToCanvas($("#tutorial_bar_charts")[0]);
Related styles include wboxes, which reads each bar's width from a third column of data; impulses, which draws a thin line from zero up to each point; and steps, fsteps and histeps, which join the points with a staircase.
Arrows
The arrows_head style draws an arrow for each row of data, from the point given in the first two columns to the point given in the next two. This is useful for drawing vector fields. arrows_nohead draws plain lines instead, and arrows_twohead puts a head on each end:
// A field of arrows circling the origin. Each row is: x0, y0, x1, y1
var arrows = [];
for (var x = -4; x <= 4; x++) {
for (var y = -4; y <= 4; y++) {
arrows.push([x, y, x - 0.15 * y, y + 0.15 * x]);
}
}
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("", {"plotStyle": "arrows_head"}, arrows)
], {
"aspect": 1,
"width": 500
})
}, {});
canvas.renderToCanvas($("#tutorial_arrows")[0]);
Three-dimensional plots
Set a graph's threeDimensional setting to true to plot data in three dimensions. Each row of data then needs a z value, after its x and y values. Setting interactiveMode to "rotate" lets the user turn the graph by dragging it:
// A helix. Each row is: x, y, z
var helix = [];
for (var t = 0; t <= 6 * Math.PI; t += 0.1) helix.push([Math.cos(t), Math.sin(t), t]);
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_DataSet("Helix", {"lineWidth": 2}, helix)
], {
"threeDimensional": true,
"interactiveMode": "rotate",
"key": true,
"width": 500
})
}, {});
canvas.renderToCanvas($("#tutorial_three_dimensional_plots")[0]);
The surface style draws a surface from data sampled on a regular grid. The easiest way to make such data is to evaluate a function, as described on the next page, Plotting functions.