Skip to content

Instantly share code, notes, and snippets.

@raghothams
Last active August 29, 2015 14:18
Show Gist options
  • Save raghothams/c64784835af67c083a4d to your computer and use it in GitHub Desktop.
Save raghothams/c64784835af67c083a4d to your computer and use it in GitHub Desktop.
AngularJS directive for C3js line / bar chart
var c3Chart = angular.module('c3Chart', []);
c3Chart.directive('c3LineBar', [function () {
return{
restrict : 'A',
require : 'ngModel',
link: function (scope,elem,attrs,ngModel) {
scope.$watch(attrs.ngModel,function(){
render(scope[attrs.ngModel], elem);
});
var render=function(data, elem){
if(data){
// apply defaults
data.color = data.color || 'blue';
data.chartType = data.chartType || 'bar';
var thresholdLine = {};
data.yThreshold = data.yThreshold || false;
if(data.yThreshold == true){
thresholdLine.threshold = 'line';
}
if(data.x.type == 'timeseries'){
data.xAxis = data.columns[0][0];
}
// generate chart
c3.generate({
bindto: elem[0],
color: {
pattern: [data.color]
},
data: {
x: data.xAxis,
columns: data.columns,
type: data.chartType,
types: thresholdLine
},
axis: {
x: {
type: data.x.type,
categories: data.x.categories,
tick: {
format: '%Y-%m-%d'
}
}
}
});
}
};
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment