Angularjs-nvd3-directives

directives for and

View the Project on GitHub cmaurer/angularjs-nvd3-directives

Creating and Configuring a Discrete Bar Chart

How to create a basic chart

Include angularjs-nvd3-directives.js in your HTML file.

<script src="dist/angularjs-nvd3-directives.js"></script>

Include other dependencies for nvd3.js and d3.js.

<script src="../build/components/d3/d3.js"></script>
<script src="../build/components/nvd3/nv.d3.js"></script>
<link rel="stylesheet" href="path/to/nv.d3.css"/>

In the Angular App, include nvd3ChartDirectives as a dependency.

var app = angular.module("nvd3TestApp", ['nvd3ChartDirectives']);

Create an Angular.js Controller, and assign json data to a scope variable.

 1 function ExampleCtrl($scope){
 2             $scope.exampleData = [
 3                 {
 4                     "key": "Series 1",
 5                     "values": [ [ 1025409600000 , 0] , [ 1028088000000 , -6.3382185140371] , [ 1030766400000 , -5.9507873460847] , [ 1033358400000 , -11.569146943813] , [ 1036040400000 , -5.4767332317425] , [ 1038632400000 , 0.50794682203014] , [ 1041310800000 , -5.5310285460542] , [ 1043989200000 , -5.7838296963382] , [ 1046408400000 , -7.3249341615649] , [ 1049086800000 , -6.7078630712489] , [ 1051675200000 , 0.44227126150934] , [ 1054353600000 , 7.2481659343222] , [ 1056945600000 , 9.2512381306992] ]
 6                 },
 7                 {
 8                     "key": "Series 2",
 9                     "values": [ [ 1025409600000 , 0] , [ 1028088000000 , 0] , [ 1030766400000 , 0] , [ 1033358400000 , 0] , [ 1036040400000 , 0] , [ 1038632400000 , 0] , [ 1041310800000 , 0] , [ 1043989200000 , 0] , [ 1046408400000 , 0] , [ 1049086800000 , 0] , [ 1051675200000 , 0] , [ 1054353600000 , 0] , [ 1056945600000 , 0] , [ 1059624000000 , 0] , [ 1062302400000 , 0] , [ 1064894400000 , 0] , [ 1067576400000 , 0] , [ 1070168400000 , 0] , [ 1072846800000 , 0] , [ 1075525200000 , -0.049184266875945] ]
10                 },
11                 {
12                     "key": "Series 3",
13                     "values": [ [ 1025409600000 , 0] , [ 1028088000000 , -6.3382185140371] , [ 1030766400000 , -5.9507873460847] , [ 1033358400000 , -11.569146943813] , [ 1036040400000 , -5.4767332317425] , [ 1038632400000 , 0.50794682203014] , [ 1041310800000 , -5.5310285460542] , [ 1043989200000 , -5.7838296963382] , [ 1046408400000 , -7.3249341615649] , [ 1049086800000 , -6.7078630712489] , [ 1051675200000 , 0.44227126150934] , [ 1054353600000 , 7.2481659343222] , [ 1056945600000 , 9.2512381306992] ]
14                 },
15                 {
16                     "key": "Series 4",
17                     "values": [ [ 1025409600000 , -7.0674410638835] , [ 1028088000000 , -14.663359292964] , [ 1030766400000 , -14.104393060540] , [ 1033358400000 , -23.114477037218] , [ 1036040400000 , -16.774256687841] , [ 1038632400000 , -11.902028464000] , [ 1041310800000 , -16.883038668422] , [ 1043989200000 , -19.104223676831] , [ 1046408400000 , -20.420523282736] , [ 1049086800000 , -19.660555051587] , [ 1051675200000 , -13.106911231646] , [ 1054353600000 , -8.2448460302143] , [ 1056945600000 , -7.0313058730976] ]
18                 }
19             ];
20 }

Include the chart directive in HTML. The data html attribute should point to the scope variable (exampleData). Other directive attributes should be the same as the public attributes associated with each chart.

<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="exampleData"
        id="exampleId"
        showXAxis="true"
        showYAxis="true"
        xAxisTickFormat="xAxisTickFormatFunction()"
        width="550"
        height="400">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

The following formatting function has been added to better display the x-axis data.

$scope.xAxisTickFormatFunction = function(){
    return function(d){
        return d3.time.format('%b')(new Date(d));
    }
}

Configuration Options

ID

Identifier for the chart. Utilized heavily by d3.js and nvd3.js when creating and updating charts. If there is more than one chart on a page, every chart should have a unique id. Datatype: String

Width

Controls the display width of the chart. Datatype: Number

Height

Controls the display height of the chart. Datatype: Number

Margin

Controls the external margin of the chart.

Datatype: Object, Number: {left:0,top:0,bottom:0,right:0}

Color

Controls the colors of the chart elements.

Datatype: Function

The function is the same as the d3.js color functions. Refer to d3.js Colors for d3.js color-specific documentation.

To use a configuration function, create a function on the $scope (i.e. $scope.colorFunction). The function can be named anything, as long as it does not conflict with an existing function name. To ‘connect’ the $scope function with the chart.color() function, add a color=”” attribute to the directive, with the value of the attribute being the name of the $scope function (i.e. scope=”colorFunction()”).

var colorCategory = d3.scale.category20b()
$scope.colorFunction = function() {
    return function(d, i) {
        return colorCategory(i);
    };
}
<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="exampleData"
        id="colorExample"
        width="550"
        height="300"
        xAxisTickFormat="xAxisTickFormatFunction()"
        showXAxis="true"
        showYAxis="true"
        color="colorFunction()">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

No Data

Defines the message displayed when data is not available.

Datatype: String

<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="noDataData"
        id="noDataExample"
        width="550"
        height="300"
        noData="No Data For You!">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

Show Values

Displays the data values on the chart.

Datatype: boolean - (true/false)

<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="reduceXTicksData"
        id="showValueExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="300"
        xAxisTickFormat="xAxisTickFormatFunction()"
        showValues="true">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

Value Format

Formats the value labels. Defaults to d3.format(‘,.2f’);

Datatype: Function

<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="reduceXTicksData"
        id="formatValueExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="300"
        xAxisTickFormat="xAxisTickFormatFunction()"
        showValues="true"
        valueFormat="valueFormatFunction()">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>
var format = d3.format(',.4f');
$scope.valueFormatFunction = function(){
	return function(d){
    	return format(d);
    }
}

Stagger Labels

<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="reduceXTicksData"
        id="staggerLablesExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="300"
        xAxisTickFormat="xAxisTickFormatFunction()"
        staggerLabels="true">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

X

Function that allows nvd3.js and d3.js to access x values from the ‘data’.

Datatype: Function

$scope.xFunction = function(){
	return function(d){
		return d[0];
	};
}
<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="exampleData"
        id="xExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="300"
        xAxisTickFormat="xAxisTickFormatFunction()"
        x="xFunction()">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

Y

Function that allows nvd3 and d3 to access y values from the ‘data’.

Datatype: Function

$scope.yFunction = function(){
	return function(d){
		return d[1];
	};
}
<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="exampleData"
        id="yExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="300"
        xAxisTickFormat="xAxisTickFormatFunction()"
        y="yFunction()">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

Force Y

List of numbers to Force into the Y scale (ie. 0, or a max / min, etc.). The numbers tell the d3.js the values to use in the scale, rather than d3.js determining the values.

Datatype: Array of Numbers (i.e. [0, 50]

<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="exampleData"
        id="forceyExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="300"
        xAxisTickFormat="xAxisTickFormatFunction()"
        forcey="[500]">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

Tooltips

Enables (true) or Disables (false) rendering of the tooltips.

Datatype: boolean - (true/false)

<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="exampleData"
        id="toolTipExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="350"
        xAxisTickFormat="xAxisTickFormatFunction()"
        tooltips="true">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>

Tooltip Content

Controls how the tooltips are displayed.

The Tooltips attribute must be included and set to true before tooltips will be rendered.

Datatype: Function

The function has the following signature function(key, x, y, e, graph), and should return a String.

$scope.toolTipContentFunction = function(){
	return function(key, x, y, e, graph) {
    	return  'Super New Tooltip' +
        	'<h1>' + key + '</h1>' +
            '<p>' +  y + ' at ' + x + '</p>'
	}
}
<div ng-controller="ExampleCtrl">
	<nvd3-discrete-bar-chart
    	data="exampleData"
        id="toolTipContentExample"
        showXAxis="true"
        showYAxis="true"
        width="550"
        height="350"
        xAxisTickFormat="xAxisTickFormatFunction()"
        tooltips="true"
        tooltipcontent="toolTipContentFunction()">
        	<svg></svg>
    </nvd3-discrete-bar-chart>
</div>