 angular.module('InescApp').directive 'monthlyGraph', ['$filter', ($filter) ->
+  chart = undefined
+
+  buildOrUpdateGraph = (chart, svgElement, entity) ->
+    if chart
+      updateGraph(chart, svgElement, entity)
+    else
+      buildGraph(svgElement, entity)
+
   buildGraph = (svgElement, entity) ->
      nv.addGraph () ->
-         data = processData(entity)
          chart = nv.models.multiChart()
                .x((d,i) -> d.x)
                .color(['#E74C3C','#C0392b','#3498DB'])

         chart.yAxis1.showMaxMin(true)
             .tickFormat($filter('roundedCurrency'))

         d3.select(svgElement)
             .datum(data)
           .transition().duration(500).call(chart)

         nv.utils.windowResize(chart.update)

         chart

  processData = (entity) ->
    autorizado = entity.autorizado

    year: '='
  link: (scope, element, attributes) ->
    scope.$watch 'entity.autorizado', ->
      entity = scope.entity
      if entity? and entity.autorizado?
        buildOrUpdateGraph(chart, element[0], entity)
]

