Source: directives/graficos/chartLine.js

/**
 * @ngdoc directives
 * @name chartLine
 * @module s4c.directives.graficos.chartLine
 *
 * @description Componente para demonstração de gráficos no formato de Linhas
 *
 *
 */
(function() {
    'use strict';

    chartLineController.$inject = [
        '$scope',
        '$timeout',
        'Preset',
        'ChartService',
        'DashboardService'
    ];

    function chartLineController(
        $scope,
        $timeout,
        Preset,
        ChartService,
        DashboardService) {

        $scope.res = $scope.$root.res;
        $scope.canvas = {};
        $scope.canvas.id = Math.random().toString(36) + '_chart';
        
        /**
         * Método responsável pela montagem do gráfico
         * 
         * @method buildChartLine
         *
         * @param informacoes {Object}  Dados para montagem do gráfico
         *
         */         
        function buildChartLine(informacoes) {
            
            var ctx = document.getElementById($scope.canvas.id).getElementsByTagName("canvas");
            ChartService.appendChart(ctx, informacoes, 'line');
            Preset.salvar();
        }

        var modulos = Preset.obter().PresetModulos;
        $scope.presetId = Preset.obter().id;

        $timeout(function() {
            for (var index in modulos) {
                if ((modulos[index].template == 'app/components/modulos/modulo-chartLine.html' || (modulos[index].Face && modulos[index].Face.template == 'app/components/modulos/modulo-chartLine.html')) && !modulos[index].usado) {
                    
                    modulos[index].usado = true;
                    modulos[index].index = 1;
                    $scope.modulo = modulos[index];
                   
                    var filtro = modulos[index].Face.extras.filtro || {};
			        /**
			         * Faz o acesso ao backend que retornará com os dados da busca para serem exibidos no gráfico
			         * 
			         * @method getInformations
			         *
			         * @param filtro {Object}  Dados para filtrar a busca
			         *
			         */                        
                    DashboardService.getInformations(filtro).then(function (result) {
                        var informacoes = {
                            legendas: result.legendas,
                            dados: result.dados,
                            background: ChartService.createColorArray(result.dados.length),
                            titulo: modulos[index].Face.name
                        }
                        buildChartLine(informacoes);
                    });
                    break;
                }
            }
        }, 300);

        angular.extend($scope, {});
    }

    function s4cChartLine() {
        return {
            restrict: 'E',
            templateUrl: 'app/directives/graficos/chartLine.html',
            scope: {},
            controller: chartLineController
        };
    }

    angular.module('s4c.components.chartLine', [])
        .directive('s4cChartline', s4cChartLine);
})();