Skip to content

Instantly share code, notes, and snippets.

@nozelrosario
Forked from pjsvis/gist:6210002
Last active December 10, 2015 06:12
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 nozelrosario/52428f315c9520ec1247 to your computer and use it in GitHub Desktop.
Save nozelrosario/52428f315c9520ec1247 to your computer and use it in GitHub Desktop.
Angular directive for using JQuery Sparklines from http://omnipotent.net/jquery.sparkline.
// Requires jQuery from http://jquery.com/
// and jQuerySparklines from http://omnipotent.net/jquery.sparkline
// AngularJS directives for jquery sparkline
var jqSparklineDirectives = angular.module('jqSparklineDirectives', []);
jqSparklineDirectives.directive('jqSparkline', [function () {
'use strict';
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elem, attrs, ngModel) {
var compositeConfig = angular.fromJson(scope.$eval(attrs.sparklineConfig)); // convert to json
scope.$watch(attrs.ngModel, function () {
render();
});
scope.$watch(attrs.sparklineConfig, function () {
render();
});
var render = function () {
var model, isCompositeConfig = false, isCompositeModel = false;
// config can be one of foll :
// {type:'line', spotColoe: 'green', lineWidth: 5}
// [{type:'line', lineColor: 'green', lineWidth: 5} , {type:'line', lineColor: 'red', lineWidth: 5}]
if (angular.isArray(compositeConfig)) {
isCompositeConfig = true;
} else if (angular.isObject(compositeConfig)) {
isCompositeConfig = false;
}
//Setup Model : Model can be [1,2,3,4,5] or [[1,2,3,4,5],[6,7,8,9]]
// Trim trailing comma if we are a string
angular.isString(ngModel.$viewValue) ? model = ngModel.$viewValue.replace(/(^,)|(,$)/g, "") : model = ngModel.$viewValue;
// convert model to json
model = angular.fromJson(model);
// Validate data & render sparkline
var composite = false;
if (angular.isArray(model)) {
for (var j = 0; j < model.length; j++) {
if (angular.isArray(model[j])) {
if (j != 0 && isCompositeModel != true) {
alert("Data not in valid format!!");
break;
} else {
isCompositeModel = true;
$(elem).sparkline(model[j], (isCompositeConfig) ? reconcileConfig(compositeConfig[j], composite) : reconcileConfig(compositeConfig, composite));
composite = true;
}
} else {
if (j != 0 && isCompositeModel != false) {
alert("Data not in valid format!!");
break;
} else {
isCompositeModel = false;
}
}
}
}
// In case not composite, render once
if (!isCompositeModel) $(elem).sparkline(model, reconcileConfig(compositeConfig));
};
// can be used for validating & defaulting config options
var reconcileConfig = function (config, composite) {
if (!config) config = {};
config.type = config.type || 'line';
config.composite = composite;
return config;
};
}
}
} ]);
@nozelrosario
Copy link
Author

Can be used as follows :

Html :

And within your controller :

$scope.data= [ [180,180] , [130, 126, 150, 180] , [120, 106, 180, 110] ];
$scope.sparkLineOptions = [ { type: 'line', height: '150px', width: '200px',fillColor: false, normalRangeColor:'#4f4', normalRangeMin: 130, normalRangeMax: 160},
{ type: 'line', lineColor: 'red', fillColor: false },
{ type: 'line',lineColor: 'blue', fillColor: false } ];

you can also define a common config for all series :
$scope.sparkLineOptions = { type: 'line', height: '150px', width: '200px',fillColor: false, normalRangeColor:'#4f4', normalRangeMin: 130, normalRangeMax: 160} ;

@dicer2000
Copy link

Nozel,

I'm trying to get a sample Pen working of your directive. However, I can't figure out why its not working. Do you think you could check it out? See anything wrong?

http://codepen.io/dicer2000/pen/jWPwOv?editors=001

Thanks, Brett

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment