static.js.lib.angular-nvd3.js Maven / Gradle / Ivy
/**************************************************************************
* AngularJS-nvD3, v1.0.0-rc; MIT License; 29/06/2015 15:12
* http://krispo.github.io/angular-nvd3
**************************************************************************/
(function(){
'use strict';
angular.module('nvd3', [])
.directive('nvd3', ['nvd3Utils', function(nvd3Utils){
return {
restrict: 'AE',
scope: {
data: '=', //chart data, [required]
options: '=', //chart options, according to nvd3 core api, [required]
api: '=?', //directive global api, [optional]
events: '=?', //global events that directive would subscribe to, [optional]
config: '=?' //global directive configuration, [optional]
},
link: function(scope, element, attrs){
var defaultConfig = {
extended: false,
visible: true,
disabled: false,
autorefresh: true,
refreshDataOnly: false,
deepWatchData: true,
debounce: 10 // default 10ms, time silence to prevent refresh while multiple options changes at a time
};
//basic directive configuration
scope._config = angular.extend(defaultConfig, scope.config);
//directive global api
scope.api = {
// Fully refresh directive
refresh: function(){
scope.api.updateWithOptions(scope.options);
},
// Update chart layout (for example if container is resized)
update: function() {
scope.chart.update();
},
// Update chart with new options
updateWithOptions: function(options){
// Clearing
scope.api.clearElement();
// Exit if options are not yet bound
if (angular.isDefined(options) === false) return;
// Exit if chart is hidden
if (!scope._config.visible) return;
// Initialize chart with specific type
scope.chart = nv.models[options.chart.type]();
// Generate random chart ID
scope.chart.id = Math.random().toString(36).substr(2, 15);
angular.forEach(scope.chart, function(value, key){
if (key[0] === '_');
else if ([
'clearHighlights',
'highlightPoint',
'id',
'options',
'resizeHandler',
'state'
].indexOf(key) >= 0);
else if (key === 'dispatch') {
if (options.chart[key] === undefined || options.chart[key] === null) {
if (scope._config.extended) options.chart[key] = {};
}
configureEvents(scope.chart[key], options.chart[key]);
}
else if ([
'bars',
'bars1',
'bars2',
'boxplot',
'bullet',
'controls',
'discretebar',
'distX',
'distY',
'interactiveLayer',
'legend',
'lines',
'lines1',
'lines2',
'multibar',
'pie',
'scatter',
'sparkline',
'stack1',
'stack2',
'sunburst',
'tooltip',
'x2Axis',
'xAxis',
'y1Axis',
'y2Axis',
'y3Axis',
'y4Axis',
'yAxis',
'yAxis1',
'yAxis2'
].indexOf(key) >= 0 ||
// stacked is a component for stackedAreaChart, but a boolean for multiBarChart and multiBarHorizontalChart
(key === 'stacked' && options.chart.type === 'stackedAreaChart')) {
if (options.chart[key] === undefined || options.chart[key] === null) {
if (scope._config.extended) options.chart[key] = {};
}
configure(scope.chart[key], options.chart[key], options.chart.type);
}
//TODO: need to fix bug in nvd3
else if ((key === 'xTickFormat' || key === 'yTickFormat') && options.chart.type === 'lineWithFocusChart');
else if (options.chart[key] === undefined || options.chart[key] === null){
if (scope._config.extended) options.chart[key] = value();
}
else scope.chart[key](options.chart[key]);
});
// Update with data
scope.api.updateWithData(scope.data);
// Configure wrappers
if (options['title'] || scope._config.extended) configureWrapper('title');
if (options['subtitle'] || scope._config.extended) configureWrapper('subtitle');
if (options['caption'] || scope._config.extended) configureWrapper('caption');
// Configure styles
if (options['styles'] || scope._config.extended) configureStyles();
nv.addGraph(function() {
// Remove resize handler. Due to async execution should be placed here, not in the clearElement
if (scope.chart.resizeHandler) scope.chart.resizeHandler.clear();
// Update the chart when window resizes
scope.chart.resizeHandler = nv.utils.windowResize(function() { scope.chart.update && scope.chart.update(); });
return scope.chart;
}, options.chart['callback']);
},
// Update chart with new data
updateWithData: function (data){
if (data) {
// TODO this triggers one more refresh. Refactor it!
scope.options.chart.transitionDuration = +scope.options.chart.transitionDuration || 250;
// remove whole svg element with old data
d3.select(element[0]).select('svg').remove();
// Select the current element to add
© 2015 - 2025 Weber Informatics LLC | Privacy Policy