n3.js is built on top of d3.js for creating super simple reusable visualizations using the reusable charts concept. Inspired by nvd3 for workflow in adding graphs and re-rendering graphs.
First in order to use n3.js
you will need to include the following:
<link rel="stylesheet" href="/css/n3.css" />
<script type="text/javascript" src="/javascripts/d3.v3.min.js" />
<script type="text/javascript" src="/javascripts/n3.min.js" />
To add a new chart use the addGraph
function:
var values = [];
var time = new Date().getTime() - 60000;
for (var i = 0; i < 60; i++) {
values.push({ x: new Date((time + (i * 1000))), y: Math.ceil(Math.random() * 100)});
}
var data = [{"key":"Stream0",values:values}];
n3.addGraph(function () {
var chart = new n3.graphing.linegraph();
d3.select("#container svg").datum(data).call(chart);
return chart;
});
There are many options that you can provide (and chain) when modifying a linegraph:
chart.pointRadius(5)
: gets or sets the radius of the circle on tooltipschart.transitionZero(true)
: when data is updated, this will transition lines to zero before displaying new datachart.transitionDuration(250)
: if transitionZero is enabled, this is the duration between transitions.chart.yDomain([min of data,max of data])
: default will use the min/max bounds, this provides specific boundschart.xDomain([min of data,max of data])
: similar toyDomain
except on the x-axischart.yScale(d3.scale.linear())
: scale of the y domain values (default linear)chart.xScale(d3.scale.linear())
: scale of the x domain values (default linear)chart.background('#313131')
: sets the background to the chartchart.margin({top:0,right:0,bottom:0,left:0})
: sets the margins of the chartchart.width()
: gets or sets the width of the chartchart.height()
: gets or sets the height of the chartchart.hideXAxis()
: gets or sets whether to show the x axischart.hideYAxis()
: gets or sets whether to show the y axis
Since the code is pretty much all there, if you find anything that needs to be customized, feel free to submit an issue, extend or create a new chart based on the one above.
This will automatically call the n3.dispatch.updateGraphs
function which is currently being
subscribed to automatically update when the window resizes. For responsive layouts, this will
ensure that the graph is displayed appropriately.
(The MIT License)
Copyright (c) 2012 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.