Skip to content

Instantly share code, notes, and snippets.

@DanisHack
Created February 17, 2014 08:54
Show Gist options
  • Save DanisHack/9047049 to your computer and use it in GitHub Desktop.
Save DanisHack/9047049 to your computer and use it in GitHub Desktop.
Angular directive to draw bar graph using D3
angular.module('d3', [])
.factory('d3Service', ['$document', '$window', '$q', '$rootScope',
function($document, $window, $q, $rootScope) {
var d = $q.defer(),
d3service = {
d3: function() { return d.promise; }
};
function onScriptLoad() {
// Load client in the browser
$rootScope.$apply(function() { d.resolve($window.d3); });
}
var scriptTag = $document[0].createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.async = true;
scriptTag.src = 'http://d3js.org/d3.v3.min.js';
scriptTag.onreadystatechange = function () {
if (this.readyState == 'complete') onScriptLoad();
}
scriptTag.onload = onScriptLoad;
var s = $document[0].getElementsByTagName('body')[0];
s.appendChild(scriptTag);
return d3service;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment