angular-nvd3
AngularJS directive for NVD3 reusable charting library (based on D3). Easily customize your charts via JSON API.
Top Related Projects
A reusable charting library written in d3.js
Simple HTML5 Charts using the <canvas> tag
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
Redefined chart library built with React and D3
Quick Overview
Angular-nvd3 is a reusable AngularJS component for NVD3.js charts. It provides a simple way to integrate NVD3 charts into Angular applications, allowing developers to create interactive and responsive data visualizations with ease.
Pros
- Easy integration with AngularJS applications
- Supports a wide range of chart types from NVD3
- Reactive and responsive charts that update automatically when data changes
- Customizable chart options and styles
Cons
- Limited to AngularJS, not compatible with newer Angular versions (2+)
- Depends on both AngularJS and NVD3, which may increase project complexity
- May have performance issues with large datasets
- Not actively maintained (last commit was in 2017)
Code Examples
- Basic line chart:
$scope.options = {
chart: {
type: 'lineChart',
height: 450,
x: function(d){ return d.x; },
y: function(d){ return d.y; }
}
};
$scope.data = [
{ values: [{x: 1, y: 5}, {x: 2, y: 8}, {x: 3, y: 3}], key: 'Line 1' },
{ values: [{x: 1, y: 3}, {x: 2, y: 5}, {x: 3, y: 7}], key: 'Line 2' }
];
- Pie chart with custom colors:
$scope.options = {
chart: {
type: 'pieChart',
height: 500,
donut: true
},
color: ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd']
};
$scope.data = [
{ key: "One", y: 5 },
{ key: "Two", y: 2 },
{ key: "Three", y: 9 }
];
- Bar chart with custom tooltip:
$scope.options = {
chart: {
type: 'discreteBarChart',
height: 450,
tooltips: true,
tooltipContent: function(key, x, y, e, graph) {
return '<h3>' + key + '</h3>' +
'<p>' + y + ' at ' + x + '</p>';
}
}
};
$scope.data = [
{
key: "Cumulative Return",
values: [
{ "label" : "A" , "value" : -29.765957771107 },
{ "label" : "B" , "value" : 0 },
{ "label" : "C" , "value" : 32.807804682612 }
]
}
];
Getting Started
-
Install the package:
bower install angular-nvd3
-
Include the required files in your HTML:
<link rel="stylesheet" href="bower_components/nvd3/build/nv.d3.css"> <script src="bower_components/angular/angular.js"></script> <script src="bower_components/d3/d3.js"></script> <script src="bower_components/nvd3/build/nv.d3.js"></script> <script src="bower_components/angular-nvd3/dist/angular-nvd3.js"></script>
-
Add the module to your Angular app:
angular.module('myApp', ['nvd3']);
-
Use the directive in your HTML:
<nvd3 options="options" data="data"></nvd3>
Competitor Comparisons
A reusable charting library written in d3.js
Pros of nvd3
- More versatile and can be used with various frameworks, not limited to Angular
- Larger community and more frequent updates
- Offers a wider range of chart types and customization options
Cons of nvd3
- Steeper learning curve for beginners
- Requires more setup and configuration compared to angular-nvd3
- Less seamless integration with Angular-specific features
Code Comparison
nvd3:
nv.addGraph(function() {
var chart = nv.models.lineChart();
d3.select('#chart svg')
.datum(data)
.call(chart);
return chart;
});
angular-nvd3:
$scope.options = {
chart: { type: 'lineChart' }
};
$scope.data = [/* ... */];
Summary
nvd3 is a more flexible and powerful charting library that can be used across different frameworks, while angular-nvd3 provides a simpler, Angular-specific implementation. nvd3 offers more customization options but requires more setup, whereas angular-nvd3 integrates more seamlessly with Angular projects at the cost of some versatility.
Simple HTML5 Charts using the <canvas> tag
Pros of Chart.js
- Lightweight and fast, with a smaller file size
- Extensive documentation and active community support
- Built-in responsiveness and mobile-friendly design
Cons of Chart.js
- Limited to canvas-based rendering, which may affect customization options
- Fewer advanced chart types compared to NVD3
Code Comparison
Chart.js:
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3]
}]
}
});
angular-nvd3:
$scope.options = {
chart: {
type: 'discreteBarChart',
height: 450,
x: function(d){ return d.label; },
y: function(d){ return d.value; }
}
};
$scope.data = [
{key: "Cumulative Return", values: [
{ "label" : "A" , "value" : -29.765957771107 },
{ "label" : "B" , "value" : 0 }
]}
];
Both libraries offer easy-to-use APIs for creating charts, but Chart.js has a more straightforward setup process. angular-nvd3 requires more configuration but provides greater flexibility for complex visualizations.
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Pros of d3
- More flexible and powerful, allowing for complex custom visualizations
- Larger community and ecosystem with extensive documentation and examples
- Can be used with any framework or vanilla JavaScript
Cons of d3
- Steeper learning curve and more complex API
- Requires more code to create basic charts and graphs
- Less integration with Angular-specific features and lifecycle
Code Comparison
d3:
const svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 5);
angular-nvd3:
$scope.options = {
chart: {
type: 'discreteBarChart',
height: 450
}
};
$scope.data = [{
key: "Cumulative Return",
values: [
{ "label": "A", "value": -29.765957771107 },
{ "label": "B", "value": 0 }
]
}];
The d3 code demonstrates direct manipulation of SVG elements, while angular-nvd3 uses a declarative approach with configuration objects. d3 offers more control but requires more code, while angular-nvd3 simplifies chart creation at the cost of flexibility.
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
Pros of nivo
- Built with React, offering better integration with modern React applications
- Provides a wider variety of chart types and customization options
- Offers server-side rendering capabilities for improved performance
Cons of nivo
- Steeper learning curve due to more complex API and configuration options
- Larger bundle size, which may impact initial load times for web applications
Code Comparison
nivo example:
import { ResponsivePie } from '@nivo/pie'
const MyPieChart = ({ data }) => (
<ResponsivePie
data={data}
margin={{ top: 40, right: 80, bottom: 80, left: 80 }}
innerRadius={0.5}
padAngle={0.7}
cornerRadius={3}
/>
)
angular-nvd3 example:
<nvd3 options="options" data="data"></nvd3>
$scope.options = {
chart: {
type: 'pieChart',
height: 500,
x: function(d) { return d.key; },
y: function(d) { return d.y; }
}
};
Both libraries offer powerful charting capabilities, but nivo is more modern and React-focused, while angular-nvd3 is tailored for AngularJS applications. nivo provides more flexibility and chart types, but may require more setup and configuration. angular-nvd3 is simpler to use but has fewer customization options and is tied to older AngularJS versions.
Redefined chart library built with React and D3
Pros of recharts
- Built specifically for React, offering seamless integration and better performance
- More active development and larger community support
- Extensive documentation and examples
Cons of recharts
- Limited to React applications, less versatile than angular-nvd3
- Steeper learning curve for developers not familiar with React
Code Comparison
angular-nvd3:
angular.module('myApp', ['nvd3'])
.controller('myCtrl', function($scope){
$scope.options = { /* chart options */ };
$scope.data = [{ /* chart data */ }];
});
recharts:
import { LineChart, Line } from 'recharts';
const MyChart = () => (
<LineChart data={data}>
<Line type="monotone" dataKey="value" stroke="#8884d8" />
</LineChart>
);
Key Differences
- angular-nvd3 is designed for AngularJS, while recharts is built for React
- recharts uses a more declarative approach, typical of React components
- angular-nvd3 relies on D3.js, while recharts is a pure React implementation
Use Cases
- Choose angular-nvd3 for AngularJS projects or when working with existing D3.js charts
- Opt for recharts in React applications or when seeking a more modern, performant charting library
Community and Support
recharts has a larger and more active community, with more frequent updates and contributions. angular-nvd3, while still maintained, has less frequent updates and a smaller user base.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Angular-nvD3
This thing is designed to make it easier to work with nvd3.js re-usable charting library. This directive allows you to easily customize your charts via JSON API.
The key feature is that the original hierarchical structure of nvd3 models is completely preserved in directive JSON structure. This means that while you creating a complex chart that containing multiple elementary chart models (such as line
, bar
, axis
, ...), you can in turn customize the properties of each internal elementary models as well as the global charting properties the way you want. This can be done as usual, but it becomes quite easily to customize while applying JSON approach to.
Try it online.
How to use
Install
cdnjs
https://cdnjs.cloudflare.com/ajax/libs/angular-nvd3/1.0.9/angular-nvd3.min.js
bower
$ bower install angular-nvd3
An angular.js, D3.js and nvd3.js would be installed as a dependency automatically. If it won't for some reason, install it manually:
$ bower install angular
$ bower install d3
$ bower install nvd3
Add dependencies to the <head>
section of your main html:
<meta charset="utf-8"> <!-- it's important for d3.js -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/d3/d3.js"></script>
<script src="bower_components/nvd3/build/nv.d3.js"></script> <!-- or use another assembly -->
<script src="bower_components/angular-nvd3/dist/angular-nvd3.js"></script>
<link rel="stylesheet" href="bower_components/nvd3/build/nv.d3.css">
npm
$ npm install angular-nvd3
download
If you don't use bower or npm, you can manually download and unpack directive with the latest version (zip, tar.gz).
Basic usage
Inject nvd3
directive into angular module, set up some chart options and push some data to the controller:
angular.module('myApp', ['nvd3'])
.controller('myCtrl', function('$scope'){
$scope.options = { /* JSON data */ };
$scope.data = { /* JSON data */ }
})
and in html again you can use it like:
<div ng-app='myApp'>
<div ng-controller='myCtrl'>
<nvd3 options='options' data='data'></nvd3>
</div>
</div>
The chart would be displayed on the page.
Example
Let's create a simple Discrete Bar Chart.
Configure options:
$scope.options = {
chart: {
type: 'discreteBarChart',
height: 450,
margin : {
top: 20,
right: 20,
bottom: 60,
left: 55
},
x: function(d){ return d.label; },
y: function(d){ return d.value; },
showValues: true,
valueFormat: function(d){
return d3.format(',.4f')(d);
},
transitionDuration: 500,
xAxis: {
axisLabel: 'X Axis'
},
yAxis: {
axisLabel: 'Y Axis',
axisLabelDistance: 30
}
}
};
Push some data:
$scope.data = [{
key: "Cumulative Return",
values: [
{ "label" : "A" , "value" : -29.765957771107 },
{ "label" : "B" , "value" : 0 },
{ "label" : "C" , "value" : 32.807804682612 },
{ "label" : "D" , "value" : 196.45946739256 },
{ "label" : "E" , "value" : 0.19434030906893 },
{ "label" : "F" , "value" : -98.079782601442 },
{ "label" : "G" , "value" : -13.925743130903 },
{ "label" : "H" , "value" : -5.1387322875705 }
]
}];
See the result.
Read more docs.
Contribute
Test it using command:
$npm test
Then build using grunt (node.js must be installed):
$grunt
Release Notes
1.0.9
- add
focus
options - fix data update for sunburst chart
- Node.js/CommonJS support
1.0.8
- fixed zoom feature for delayed data loading
- fixed caption positioning
- fixed
updateWithOptions
andupdateWithData
api functions
1.0.7
- added
debounceImmediate
flag - added compatibility with nvd3 1.8.3
1.0.6
- merged with nvd3 1.8.2
- fixed travis
- fixed npm package dependencies
- fixed tests
- added
zoomend
event
1.0.5 (nvd3 v1.8.1)
- fixed
index.js
- fixed
onReady
attribute - added
getElement
api method
1.0.4
deepWatchData = true
by default- deleted
autorefresh
,deepWatchConfig
configs - added
deepWatchDataDepth = 2
config to specify watch depth level for data: 0 - by reference (cheap), 1 - by collection item (the middle), 2 - by value (expensive) - added
onReady
attribute - added
updateWithTimeout
,refreshWithTimeout
methods toapi
- fixed bugs
1.0.3
- Fixed width and height issues for IE: #16, #158, #200, #226.
- Fixed tooltip issue #172
- Set
refreshDataOnly = true
by default - Added
zoom & pan
functionality - Fixed tooltip content, subtitle and many other issues...
1.0.2
- Fixed
tooltip
#222 for interactive guideline. - Set
deepWatchData
tofalse
by default - Added
deepWatchOptions
anddeepWatchConfig
properties
1.0.1
- Add support for
Candlestick Chart
,OHLC Chart
,Sunburst Chart
,Pox Plot Chart
1.0.0-rc.2
- Add support of nvd3 1.8.1
- Fix issue with
stacked
parameter
1.0.0-rc
- Rename
utils
module to avoid conflicts - Fix nvd3 version reference in bower.json
- Remove usage of reserved word
class
- Fix multiple resize event listeners which were causing null pointer exceptions
- Change bower.json's main property to use regular instead of minified file
1.0.0-beta (nvd3 v1.7.1)
Under developing in master (1.x) branch
--
If you use the old nvd3 version (v1.1.15-beta), I recommend you to use an updated assembly (
nv.d3.js
andnv.d3.css
, you can find it in thelib
directory of this project) with some fixes rather than the last one installed via bower.
0.1.1 (stable for nvd3 v1.1.15-beta)
Under developing in 0.x branch
0.1.0
- added update method to global api, pull request
- fixed bug for
multiChart
- added getScope method to global api. (give an access to internal directive scope, for example, we can get chart object like:
$scope.api.getScope().chart
) - fixed multiple chart rendering under initializing (fixed multiple callback calls)
0.0.9
...
License
Licensed under the terms of the MIT License
Top Related Projects
A reusable charting library written in d3.js
Simple HTML5 Charts using the <canvas> tag
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
Redefined chart library built with React and D3
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot