Skip to content

Instantly share code, notes, and snippets.

@wangshijun
Forked from pjsvis/gist:6210002
Created August 25, 2014 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangshijun/221bdea1970e7fd3967e to your computer and use it in GitHub Desktop.
Save wangshijun/221bdea1970e7fd3967e to your computer and use it in GitHub Desktop.
// 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';
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));
console.log(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
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