Skip to content

Instantly share code, notes, and snippets.

@lperrin
Created June 30, 2015 13:01
Show Gist options
  • Save lperrin/4cbc991de952adfa3c09 to your computer and use it in GitHub Desktop.
Save lperrin/4cbc991de952adfa3c09 to your computer and use it in GitHub Desktop.
var _ = require('underscore'),
moment = require('moment-timezone'),
Chart = require('chart.js');
ngModule.controller('analyticsChartCtrl', function ($scope, $element, analytics) {
var ctx = $element.find('#graph')[0].getContext('2d');
var oldChart = null;
$scope.analytics = analytics;
$scope.chart = new Chart(ctx);
$scope.$watch('analytics.data', drawChart);
$scope.$watch('data_direction', drawChart);
function drawChart() {
if (!analytics.data)
return;
var graphData = getGraphFromDirection($scope.data_direction).filter(removeDataFromTheFuture),
labels = _(graphData).pluck('indice'),
values = _(graphData).pluck('value');
var data = {
labels: labels,
datasets: [{
data: values,
fillColor: 'transparent',
strokeColor: '#D0D9E0',
pointColor: '#FFF',
datasetStrokeColor: '#fff',
pointStrokeColor: 'transparent',
pointHighlightFill: '#FFF',
pointHighlightStroke: 'transparent'
}]
};
if (oldChart)
oldChart.destroy();
oldChart = $scope.chart.Line(data, {
animation: false,
bezierCurve: false,
scaleShowGridLines: true,
scaleLineColor: 'transparent',
scaleGridLineColor: 'transparent',
scaleShowLabels: false,
tooltipTemplate: '<%= value %>',
tooltipCaretSize: 6,
tooltipFontSize: 14,
tooltipYPadding: 5,
tooltipFillColor: 'rgba(0,0,0,.4)',
pointDotRadius: 4,
pointDotStrokeWidth: 0,
pointHitDetectionRadius: 5,
datasetStrokeWidth: 4,
scaleFontSize: 14,
scaleFontColor: '#fff',
scaleFontFamily: 'Roboto',
responsive: true
});
}
function removeDataFromTheFuture(graph_element) {
return moment(graph_element.start).isBefore(new Date());
}
function getGraphFromDirection(data_direction) {
switch (data_direction) {
case 'inbound':
return analytics.data.graph_inbound;
case 'outbound':
return analytics.data.graph_outbound;
default:
return analytics.data.graph_inbound;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment