Created
August 24, 2015 16:11
-
-
Save jperelli/525d3ec0b2571c26ad63 to your computer and use it in GitHub Desktop.
angular jquery sparkline directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// modified from https://gist.github.com/pjsvis/6210002 | |
// Requires jQuery from http://jquery.com/ | |
// and jQuerySparklines from http://omnipotent.net/jquery.sparkline | |
// AngularJS directives for jquery sparkline | |
angular.module('sparkline', []); | |
angular.module('sparkline') | |
.directive('jqSparkline', [function () { | |
'use strict'; | |
return { | |
restrict: 'A', | |
require: 'ngModel', | |
link: function (scope, elem, attrs, ngModel) { | |
var opts={}; | |
//TODO: Use $eval to get the object | |
opts.type = attrs.type || 'line'; | |
opts.width = attrs.width || '50%'; | |
opts.spotColor = attrs.spotColor || '#0000f0'; | |
opts.minSpotColor = attrs.minSpotColor || '#0000f0'; | |
opts.maxSpotColor = attrs.maxSpotColor || '#0000f0'; | |
scope.$watch(attrs.ngModel, function () { | |
render(); | |
}); | |
scope.$watch(attrs.opts, function(){ | |
render(); | |
} | |
); | |
var render = function () { | |
var model; | |
if(attrs.opts) angular.extend(opts, angular.fromJson(attrs.opts)); | |
// Trim trailing comma if we are a string | |
angular.isString(ngModel.$viewValue) ? model = ngModel.$viewValue.replace(/(^,)|(,$)/g, "") : model = ngModel.$viewValue; | |
var data; | |
// Make sure we have an array of numbers | |
if (typeof model != 'undefined') { | |
angular.isArray(model) ? data = model : data = model.split(','); | |
$(elem).sparkline(data, opts); | |
} | |
}; | |
} | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment