A 2d function plotter powered by d3
Function Plot is a powerful library built on top of D3.js whose purpose is to render functions with little configuration (think of it as a little clone of Google's plotting utility: y = x * x
The library currently supports interactive line charts and scatterplots, whenever the graph scale is modified the function is evaluated again with the new bounds, result: infinite graphs!
NOTE: function-plot requires d3 v3: see this issue to keep track of the upgrade status
$ npm install --save function-plotvar d3 = window.d3
var functionPlot = require('function-plot');
functionPlot({
// options below
})All the available options are described in the homepage
var functionPlot = require('function-plot');
params, All the params are optional unless otherwise stated
options{Object}target{string|Object} the selector or DOM node of the parent element to render the graph totitle{string} If set the chart will have it as a title on the topxAxis{Object}type{string} (default:'linear') the scale of this axis, possible valueslinear|logdomain{number[]} initial ends of the axisinvert{boolean} (default:false) true to invert the values of this axislabel{string} (default:'') label to show near the axis
yAxis{Object}type{string} (default:'linear') the scale of this axis, possible valueslinear|logdomain{number[]} initial ends of the axisinvert{boolean} (default:false) true to invert the values of this axislabel{string} (default:'') label to show near the axis
disableZoom{boolean} true to disable drag and zoom on the graphgrid{boolean} true to show a gridtip{object} configuration passed tolib/tip, it's the helper shown on mouseover on the closest function to the current mose positionxLine{boolean} true to show a line parallel to the X axis on mouseoveryLine{boolean} true to show a line parallel to the Y axis on mouseoverrenderer{function} Function to be called to define custom rendering on mouseover, called with thexandf(x)of the function which is closest to the mouse position (args:x, y)
annotations{Object[]} An array defining parallel lines to the y-axis or the x-axisx{number} x-coordinate of the line parallel to the y-axisy{number} y-coordinate of the line parallel to the x-axistext{string} text shown next to the parallel line
data{array} required An array defining the functions to be renderedplugins{array} An array describing plugins to be run when the graph is initialized, check out the examples on the main page
An array of objects, each object contains info of a function to render and can have the following options
title{string} title of the functionskipTip{boolean=false} true to make the tip ignore this functionrange{number[]=[-Infinity, Infinity]} an array with two numbers, the function will only be evaluated with values that belong to this intervalnSamples{number} The number of values to be taken fromrangeto evaluate the function, note that if interval-arithmetic is used the function will be evaluated with intervals instead of single valuesgraphType{string='interval'} The type of graph to render, available values areinterval|polyline|scatterfnType{string='linear'} The type of function to render, available values arelinear|parametric|implicit|polar|points|vectorsampler{string='interval'} The sampler to take samples fromrange, available values areinterval|builtIn- NOTE:
builtInshould only be used whengraphTypeispolyline|scatter - NOTE: when math.js is included in the webpage it will be used instead of the bundled sampler
- NOTE:
Additional style related options
color{string} color of the function to renderattr{Object} additional attributes set on the svg node that represents this datumclosed{boolean=false} (only ifgraphType: 'polyline'orgraphType: 'scatter') True to close the path, for any segment of the closed area graphy0will be 0 andy1will bef(x)
When derivative {Object} is present on a datum
derivative.fn{string|Function} The derivative offnderivative.x0{number} The abscissa of the point which belongs to the curve represented byfnwhose tangent will be computed (i.e. the tangent line to the pointx0, fn(x0))derivative.updateOnMouseMove{boolean} True to compute the tangent line by evaluatingderivative.fnwith the current mouse position (i.e. letx0be the abscissa of the mouse position transformed to local coordinates, the tangent line to the pointx0, fn(x0))
When secants {Array} is present on a datum
secants[i].x0{number} The abscissa of the first pointsecants[i].x1{number} (optional ifupdateOnMouseMoveis set) The abscissa of the second pointsecants[i].updateOnMouseMove{boolean} (optional) True to update the secant line by evaluatingfnwith the current mouse position (x0is the fixed point andx1is computed dynamically based on the current mouse position)
fn{string|Function} the function that represents the curve, this function is evaluated with values which are insiderange
x{string|Function} the x-coordinate of a point to be sampled with a parameterty{string|Function} the y-coordinate of a point to be sampled with a parametertrange = [0, 2 * Math.PI]{Array} therangeproperty in parametric equations is used to determine the possible values oft, remember that the number of samples is set in the propertysamples
r{string|Function} a polar equation in terms ofthetarange = [-Math.PI, Math.PI]therangeproperty in polar equations is used to determine the possible values oftheta, remember that the number of samples is set in the propertysamples
fn{string|Function} a function which needs to be expressed in terms ofxandy
NOTE: implicit functions can only be rendered using interval-arithmetic
points{Array} an array of 2-number array which hold the coordinates of the points to render
NOTE: make sure your type of graph is either scatter or polyline
vector{Array} an 2-number array which has the ends of the vectoroffset{Array=[0, 0]} (optional) vector's offset
instance.id{string} a random generated id made out of letters and numbersinstance.linkedGraphs{array} array of function-plot instances linked to the events of this instance, i.e. when the zoom event is dispatched on this instance it's also dispatched on all the instances of this arrayinstance.meta{object}instance.meta.margin{object} graph's left,right,top,bottom marginsinstance.meta.width{number} width of the canvas (minus the margins)instance.meta.height{number} height of the canvas (minus the margins)instance.meta.xScale{d3.scale.linear} graph's x-scaleinstance.meta.yScale{d3.scale.linear} graph's y-scaleinstance.meta.xAxis{d3.svg.axis} graph's x-axisinstance.meta.yAxis{d3.svg.axis} graph's y-axis
instance.root{d3.selection}svgelement that holds the graph (canvas + title + axes)instance.canvas{d3.selection}g.canvaselement that holds the area where the graphs are plotted (clipped with a mask)
Events
An instance can subscribe to any of the following events by doing instance.on([eventName], callback),
events can be triggered by doing instance.emit([eventName][, params])
mouseoverfired whenever the mouse is over the canvasmousemovefired whenever the mouse is moved inside the canvas, callback params: a single object{x: number, y: number}(in canvas space coordinates)mouseoutfired whenever the mouse is moved outside the canvasbefore:drawfired before drawing all the graphsafter:drawfired after drawing all the graphszoom:scaleUpdatefired whenever the scale of another graph is updated, callback paramsxScale,yScale(x-scale and y-scale of another graph whose scales were updated)tip:updatefired whenever the tip position is updated, callback paramsx,y,index(in canvas space coordinates,indexis the index of the graph where the tip is on top of)evalfired whenever the sampler evaluates a function, callback paramsdata(an array of segment/points),index(the index of datum in thedataarray),isHelper(true if the data is created for a helper e.g. for the derivative/secant)
The following events are dispatched to all the linked graphs
all:mouseoversame asmouseoverbut it's dispatched in each linked graphall:mousemovesame asmousemovebut it's dispatched in each linked graphall:mouseoutsame asmouseoutbut it's dispatched in each linked graphall:zoom:scaleUpdatesame aszoom:scaleUpdatebut it's dispatched in each linked graphall:zoomfired whenever there's scaling/translation on the graph, dispatched on all the linked graphs
When the definite-integral plugin is included the instance will fire the following events
definite-integraldatum{object} The datum whose definite integral was computedi{number} The index of the datum in thedataarrayvalue{number} The value of the definite integrala{number} the left endpoint of the intervalb{number} the right endpoint of the interval
var y = functionPlot.eval.builtIn(datum, fnProperty, scope)Where datum is an object that has a function to be evaluated in the property fnProperty ,
to eval this function we need an x value which is sent through the scope
e.g.
var datum = {
fn: 'x^2'
}
var scope = {
x: 2
}
var y = functionPlot.eval.builtIn(datum, 'fn', scope)Every element of the data property sent to functionPlot is saved on instance.options.data,
if you want to get the evaluated values of all the elements here run
var instance = functionPlot( ... )
instance.options.data.forEach(function (datum) {
var datum = {
fn: 'x^2'
}
var scope = {
// a value for x
x: 2
}
var y = functionPlot.eval.builtIn(datum, 'fn', scope)
}Just call instance.programmaticZoom with the desired x and y domains
var instance = functionPlot( ... )
var xDomain = [-3, 3]
var yDomain = [-1.897, 1.897]
instance.programmaticZoom(xDomain, yDomain)Given the xDomain values you can compute the corresponding yDomain values to main
the aspect ratio between the axes
function computeYScale (width, height, xScale) {
var xDiff = xScale[1] - xScale[0]
var yDiff = height * xDiff / width
return [-yDiff / 2, yDiff / 2]
}
var width = 800
var height = 400
// desired xDomain values
var xScale = [-10, 10]
functionPlot({
width: width,
height: height,
xDomain: xScale,
yDomain: computeYScale(width, height, xScale),
target: '#demo',
data: [{
fn: 'x^2',
derivative: {
fn: '2x',
updateOnMouseMove: true
}
}]
})var instance = functionPlot({
target: '#complex-plane',
xLabel: 'real',
yLabel: 'imaginary'
})
// old format
var format = instance.meta.yAxis.tickFormat()
var imaginaryFormat = function (d) {
// new format = old format + ' i' for imaginary
return format(d) + ' i'
}
// update format
instance.meta.yAxis.tickFormat(imaginaryFormat)
// redraw the graph
instance.draw()Selectors (sass)
.function-plot {
.x.axis {
.tick {
line {
// grid's vertical lines
}
text {
// x axis labels
}
}
path.domain {
// d attribute defines the graph bounds
}
}
.y.axis {
.tick {
line {
// grid's horizontal lines
}
text {
// y axis labels
}
}
path.domain {
// d attribute defines the graph bounds
}
}
}After cloning the repo and running npm install
node site.js // generate the examples shown on index.html
npm startMain page: http://localhost:9966/site, development page: http://localhost:9966/site/playground.html
Deploy steps:
npm run dist(make sure to commit the dist files after this command)npm version major|minor|patchgit push origin masternpm run deploynpm publish
2015 MIT © Mauricio Poppe