       return links
 
     draw_nodes: =>
+      horizontal_spacing = D3.sankey.Node.horizontal_spacing
+      width = D3.sankey.Node.width
+      colors = d3.scale.category20()
       nodes = @svg.selectAll("g.node").
-        data(@module.nodes.models, (d) -> d.get('id')).
+        data(@node_list.models, (d) -> d.get('id')).
         enter().
         append("g").
         attr("class", "node").
         attr("data-id", (d) -> d.get('id'))
 
-      colors = d3.scale.category20()
-
       nodes.append("svg:rect").
-        attr("x", (d) => @x(@module.Node.horizontal_spacing * d.get('column'))).
+        attr("x", (d) => @x(horizontal_spacing * d.get('column'))).
         attr("y", (d) => @y(d.y_offset())).
         attr("fill", (datum, i) -> colors(i)).
         attr("stroke", (d, i) -> d3.rgb(colors(i)).darker(2)).
-        attr("width", (d) => @x @module.Node.width).
+        attr("width", (d) => @x width).
         attr("height", (d) => @y d.value()).
         on("mouseover", @node_mouseover).
         on("mouseout", @node_mouseout)
 
-      # And here we have the label
       nodes.append("svg:text").
         attr("class", "label").
         attr("x", (d) => @x d.x_offset()).


      # this one will be changed dynamically later on
      @y = d3.scale.linear().domain([0, 5000]).range([0, @height])
      # we need the scale somewhere else, too. This code is indeed ugly and should
      # be refactored ASAP, removing all the @module calls.
      @module.y = @y

      # This is the function that will take care of drawing the links once we've
      # set the base points


    ]

  # In this chart most positioning is calculated by us. The D3 sankey plugin is
  # cool but not flexible enough
  Node: class extends Backbone.Model
    @width: 25
    @horizontal_spacing: 280
    vertical_margin: 8

    initialize: =>
      @view = D3.sankey.view
      @right_links = []
      @left_links = []

    # vertical position of the top left corner of the node. Adds some margin
    # between nodes
    y_offset: =>
      offset = 0
      # since the value's going to be scaled, let's invert it to have always the
      # same margin in absolute (=pixel) values
      margin = @view.y.invert(@vertical_margin)
      for n in @siblings()
        break if n == this
        offset += n.value() + margin
      offset

    x_offset: => @get('column') * D3.sankey.Node.horizontal_spacing

    # center point of the node. We use it as link anchor point
    x_center: => @x_offset() + D3.sankey.Node.width / 2

    # The height of the node is the sum of the height of its link. Since links
    # are both inbound and outbound, let's use the max size. Ideally the values
    # should match
    value: =>
      _.max [
        _.inject(@left_links, ((memo, i) -> memo + i.value()), 0),
        _.inject(@right_links,((memo, i) -> memo + i.value()), 0)
      ]

    # returns an array of the other nodes that belong to the same column. This
    # is used by the +y_offset+ method to calculate the right node position
    siblings: =>
      items = _.groupBy(@view.node_list.models, (node) -> node.get('column'))
      items[@get 'column']

    label: => @get('label') || @get('id')

  Link: class extends Backbone.Model
    initialize: =>
      @view = D3.sankey.view
      @series = @view.series
      @left = @view.node_list.get @get('left')
      @right = @view.node_list.get @get('right')
      if @get('gquery')
        @gquery = new ChartSerie
          gquery_key: @get('gquery')
