JSPlot Documentation: Plotting functions
Tutorial
- The plotting canvas
- Plotting data
- Graph plotting styles
- Plotting functions
- Vector graphics
API reference
Introduction to the function evaluator class
As well as plotting lists of data, JSPlot can plot mathematical functions, written as Javascript functions. The JSPlot_FunctionEvaluator class evaluates functions at a series of points, and turns the results into a JSPlot_DataSet which can be plotted like any other.
Evaluating a function
A JSPlot_FunctionEvaluator is created with a title and styling options, exactly as for a JSPlot_DataSet, followed by a list of functions. Its evaluate_linear_raster method then evaluates the functions at evenly spaced values of x, and returns a data set. Its arguments are the lowest and highest values of x, the number of points to evaluate, and whether to re-evaluate the functions when the graph's range changes (see below):
var gaussian = function (x) {
return Math.exp(-x * x / 2);
};
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_FunctionEvaluator("sin(x)", {}, [Math.sin]).evaluate_linear_raster(-5, 5, 200, false),
new JSPlot_FunctionEvaluator("exp(-x^2 / 2)", {}, [gaussian]).evaluate_linear_raster(-5, 5, 200, false)
], {
"key": true,
"keyPosition": "below",
"y1_axis": {"min": -1.2, "max": 1.2}
})
}, {});
canvas.renderToCanvas($("#tutorial_function_evaluator")[0]);
Any Javascript function that takes a number and returns a number can be plotted, including the functions built into Javascript's Math object. Use enough points that the curve looks smooth.
Re-evaluating functions as the graph moves
If the last argument to evaluate_linear_raster is true, the functions are evaluated afresh whenever the range of the graph's horizontal axis is fixed – either because you have set its min and max, or because the user has scrolled the graph. Combined with an interactive graph, this lets the user scroll along a function forever. Try dragging this graph sideways:
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_FunctionEvaluator("sin(x) / x", {}, [
function (x) {
return (x === 0) ? 1 : Math.sin(x) / x;
}
]).evaluate_linear_raster(-20, 20, 400, true)
], {
"key": true,
"interactiveMode": "pan",
"x1_axis": {
"min": -20,
"max": 20,
"scrollEnabled": true,
"zoomEnabled": true
},
"y1_axis": {
"min": -0.4,
"max": 1.2
}
})
}, {});
canvas.renderToCanvas($("#tutorial_function_auto_update")[0]);
Functions which make several columns of data
The first column of the data set is the value of x, and each function in the list adds another column. Plot styles which need several columns, such as error bars, can therefore be drawn from several functions. Here, the first function gives the y value of each point, and the second gives the size of its error bar:
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_FunctionEvaluator("A growing uncertainty", {"plotStyle": "yerrorbars"}, [
function (x) {
return Math.sqrt(x);
},
function (x) {
return 0.05 * x;
}
]).evaluate_linear_raster(1, 10, 10, false)
], {
"key": true,
"keyPosition": "tl",
"x1_axis": {"min": 0, "max": 11}
})
}, {});
canvas.renderToCanvas($("#tutorial_function_columns")[0]);
Functions on logarithmic axes
On a logarithmic axis, evenly spaced points would be crowded together at one end of the axis. The evaluate_log_raster method takes the same arguments as evaluate_linear_raster, but spaces the points evenly in the logarithm of x:
// The response of a simple low-pass filter, as a function of frequency
var response = function (frequency) {
return 1 / Math.sqrt(1 + Math.pow(frequency / 1000, 2));
};
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_FunctionEvaluator("Low-pass filter", {}, [response]).evaluate_log_raster(1, 1e6, 200, false)
], {
"key": true,
"keyPosition": "bl",
"x1_axis": {"label": "Frequency / Hz", "log": true},
"y1_axis": {"label": "Gain", "log": true, "min": 0.001, "max": 2}
})
}, {});
canvas.renderToCanvas($("#tutorial_function_log_axes")[0]);
Surfaces
Functions of two variables, x and y, can be evaluated over a grid of points with the evaluate_over_grid method, and drawn as a surface on a three-dimensional graph. Its arguments are the range of x and the number of points along it, followed by the same for y. Drag the graph to rotate it:
var ripple = function (x, y) {
var r = Math.hypot(x, y);
return Math.cos(r) * Math.exp(-r / 4);
};
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_FunctionEvaluator("Ripple", {
"plotStyle": "surface",
"fillColor": new JSPlot_Color(0.2, 0.5, 0.9, 1),
"color": new JSPlot_Color(0, 0, 0, 0)
}, [ripple]).evaluate_over_grid(-10, 10, 50, -10, 10, 50)
], {
"threeDimensional": true,
"interactiveMode": "rotate",
"width": 500
})
}, {});
canvas.renderToCanvas($("#tutorial_function_surfaces")[0]);