       # Add relation to data_relations_visibles array if both nodes exist and are visibles
       if d.source and d.target and d.source.visible and d.target.visible
         @data_relations_visibles.push d
-        @linkedByIndex[d.source_id+','+d.target_id] = true
+        @addRelationToLinkedByIndex d.source_id, d.target_id
+
+    # Add linkindex to relations
+    @setLinkIndex()
 
     console.log 'current nodes', @data_nodes
     console.log 'current relations', @data_relations_visibles

    if relation.source and relation.target and relation.source.visible and relation.target.visible
      console.log 'addRelationVisible'
      @data_relations_visibles.push relation
      @linkedByIndex[relation.source_id+','+relation.target_id] = true

  # maybe we need to split removeVisibleRelationaData & removeRelationData
  removeRelationData: (relation) =>

    index = @data_relations_visibles.indexOf relation
    if index != -1
      @data_relations_visibles.splice index, 1

  addRelationToLinkedByIndex: (source, target) ->
    # count number of relations between 2 nodes
    @linkedByIndex[source+','+target] = ++@linkedByIndex[source+','+target] || 1

  # Add a linkindex property to relations
  # Based on https://github.com/zhanghuancs/D3.js-Node-MultiLinks-Node
  setLinkIndex: ->
    # Sort relations
    @data_relations_visibles.sort (a,b) ->
      if a.source_id > b.source_id
        return 1
      else if a.source_id < b.source_id
        return -1
      else 
        if a.target_id > b.target_id
          return 1
        else if a.target_id < b.target_id
          return -1
        else
          return 0
    # set linkindex attr
    @data_relations_visibles.forEach (relation, i) =>
      if i != 0 && @data_relations_visibles[i].source_id == @data_relations_visibles[i-1].source_id && @data_relations_visibles[i].target_id == @data_relations_visibles[i-1].target_id
        @data_relations_visibles[i].linkindex = @data_relations_visibles[i-1].linkindex + 1
      else
        @data_relations_visibles[i].linkindex = 1

  addNode: (node) ->
    console.log 'addNode', node
