0.3.3 • Published 11 years ago
simple-function-plot v0.3.3
simple-function-plot 
A simple 2d function plotter powered by d3 and used on http://blog.maurizzzio.com
Install
$ npm install --save simple-function-plotUsage
var simpleFunctionPlot = require('simple-function-plot');
var instance = simpleFunctionPlot({
// see the API below
})
d3.select(' selector of the parent container for the chart ')
.call(instance)Example
See public/index.js
'use strict';
var d3 = window.d3;
var simpleFunctionPlot = require('../');
var instance = simpleFunctionPlot({
data: [{
title: 'f(x)',
fn: function (x) {
return -x * x;
}
}, {
fn: function (x) {
return Math.sqrt(x);
},
graphOptions: {
type: 'scatter'
}
}, {
fn: function (x) {
return 1 / x;
},
graphOptions: {
limits: [0],
interpolate: 'linear'
}
}]
});
d3.select('#canvas')
.call(instance);Screenshot (for the example located at public/index.js):

API
var simpleFunctionPlot = require('simple-function-plot');simpleFunctionPlot(options)
params, All the params are optional unless otherwise stated
optionsoptions.title{string} If set the chart will have it as a title on the topoptions.domainX{array} domain of the linear scale (used in the x axis)options.domainY{array} domain of the linear scale (used in the y axis)options.labelX{string} x axis labeloptions.labelY{string} y axis labeloptions.tip{object} configuration passed tolib/tip, it's the helper shown on mouseover on the closest function to the current mose positionoptions.tip.xLine{boolean} true to show a line parallel to the X axis on mouseoveroptions.tip.yLine{boolean} true to show a line parallel to the Y axis on mouseoveroptions.tip.renderer{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)
options.data{array} required An array defining the functions to be renderedoptions.data[i].title{string} title of the functionoptions.data[i].skipTip{boolean} true to avoid this function from being a target of the tipoptions.data[i].fn{function} the function itself, called with an independent value defined inrange, itoptions.data[i].increment{number} the increment used in each iteration to reach the width of the chart i.e. this quantity is added k times to the x scale's min x value until it surpasses the x scale's max value, defaults to(max - min) / 100options.data[i].graphOptions{Object} options passed to the the files located inlib/type/, the most useful property of this object istypewhich is used to determine the type of graph to be rendered for a functionoptions.data[i].graphOptions.type{string} the type of graph to render for the function
Single Graph Options
Common options:
optionsoptions.limits{number[]} x values which make the function undefined, e.g. in1/xthe value 0 makes the function invalid
Depending on the type of graph:
line
optionsoptions.interpolate{string} passed tod3.svg.line().interpolate( ... )
Development
After cloning the repo and running npm install
npm startOpen localhost:5555 and that's it! Local development server powered beefy
TODO
- baselines (parallel to the X axis) http://metricsgraphicsjs.org/examples.htm
- annotations (parallel to the Y axis)
- axis labeling
License
2015 MIT © Mauricio Poppe
