       {id: 'transport',   column: 0, row: 5},
       {id: 'loss',        column: 0, row: 6},
 
-      {id: 'el_production',      column: 1, row: 1},
-      {id: 'heating_production', column: 1, row: 3},
-
-      {id: 'coal',       column: 2, row: 0},
-      {id: 'nuclear',    column: 2, row: 1},
-      {id: 'gas',        column: 2, row: 2},
-      {id: 'oil',        column: 2, row: 3},
-      {id: 'biomass',    column: 2, row: 4},
-      {id: 'wind',       column: 2, row: 5},
-      {id: 'hydro',      column: 2, row: 6},
-      {id: 'solar',      column: 2, row: 7},
-      {id: 'waste',      column: 2, row: 8}
+      {id: 'el_prod',     column: 1, row: 1, label: "electricity production"},
+      {id: 'heat_prod',   column: 1, row: 3, label: "heating production"},
+
+      {id: 'coal',        column: 2, row: 0},
+      {id: 'nuclear',     column: 2, row: 1},
+      {id: 'gas',         column: 2, row: 2},
+      {id: 'oil',         column: 2, row: 3},
+      {id: 'biomass',     column: 2, row: 4},
+      {id: 'wind',        column: 2, row: 5},
+      {id: 'hydro',       column: 2, row: 6},
+      {id: 'solar',       column: 2, row: 7},
+      {id: 'waste',       column: 2, row: 8}
 
     ]
     links: [
-      {left: 'industry',      right: 'el_production', gquery: 'electricity_industry_in_mekko_of_final_demand'},
-      {left: 'other',         right: 'el_production', gquery: 'electricity_other_in_mekko_of_final_demand'},
-      {left: 'households',    right: 'el_production', gquery: 'electricity_households_in_mekko_of_final_demand'},
-      {left: 'agriculture',   right: 'el_production', gquery: 'electricity_agriculture_in_mekko_of_final_demand'},
-      {left: 'buildings',     right: 'el_production', gquery: 'electricity_buildings_in_mekko_of_final_demand'},
-      {left: 'transport',     right: 'el_production', gquery: 'electricity_transport_in_mekko_of_final_demand'},
-      {left: 'industry',      right: 'heating_production', gquery: 'hot_water_industry_in_mekko_of_final_demand'},
-      {left: 'other',         right: 'heating_production', gquery: 'hot_water_other_in_mekko_of_final_demand'},
-      {left: 'households',    right: 'heating_production', gquery: 'hot_water_households_in_mekko_of_final_demand'},
-      {left: 'agriculture',   right: 'heating_production', gquery: 'hot_water_agriculture_in_mekko_of_final_demand'},
-      {left: 'buildings',     right: 'heating_production', gquery: 'hot_water_buildings_in_mekko_of_final_demand'},
-      {left: 'transport',     right: 'heating_production', gquery: 'hot_water_transport_in_mekko_of_final_demand'},
-      {left: 'el_production', right: 'coal',       gquery: 'coal_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'nuclear',    gquery: 'nuclear_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'gas',        gquery: 'gas_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'oil',        gquery: 'oil_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'biomass', gquery: 'biomass_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'wind', gquery: 'wind_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'hydro', gquery: 'hydro_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'solar', gquery: 'solar_in_source_of_electricity_production'},
-      {left: 'el_production', right: 'waste', gquery: 'waste_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'coal',       gquery: 'coal_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'nuclear',    gquery: 'nuclear_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'gas',        gquery: 'gas_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'oil',        gquery: 'oil_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'biomass', gquery: 'biomass_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'wind', gquery: 'wind_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'hydro', gquery: 'hydro_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'solar', gquery: 'solar_in_source_of_electricity_production'},
-      {left: 'heating_production', right: 'waste', gquery: 'waste_in_source_of_electricity_production'}
+      {left: 'industry',    right: 'el_prod',   gquery: 'electricity_industry_in_mekko_of_final_demand'},
+      {left: 'other',       right: 'el_prod',   gquery: 'electricity_other_in_mekko_of_final_demand'},
+      {left: 'households',  right: 'el_prod',   gquery: 'electricity_households_in_mekko_of_final_demand'},
+      {left: 'agriculture', right: 'el_prod',   gquery: 'electricity_agriculture_in_mekko_of_final_demand'},
+      {left: 'buildings',   right: 'el_prod',   gquery: 'electricity_buildings_in_mekko_of_final_demand'},
+      {left: 'transport',   right: 'el_prod',   gquery: 'electricity_transport_in_mekko_of_final_demand'},
+      {left: 'industry',    right: 'heat_prod', gquery: 'hot_water_industry_in_mekko_of_final_demand'},
+      {left: 'other',       right: 'heat_prod', gquery: 'hot_water_other_in_mekko_of_final_demand'},
+      {left: 'households',  right: 'heat_prod', gquery: 'hot_water_households_in_mekko_of_final_demand'},
+      {left: 'agriculture', right: 'heat_prod', gquery: 'hot_water_agriculture_in_mekko_of_final_demand'},
+      {left: 'buildings',   right: 'heat_prod', gquery: 'hot_water_buildings_in_mekko_of_final_demand'},
+      {left: 'transport',   right: 'heat_prod', gquery: 'hot_water_transport_in_mekko_of_final_demand'},
+      {left: 'el_prod',     right: 'coal',      gquery: 'coal_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'nuclear',   gquery: 'nuclear_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'gas',       gquery: 'gas_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'oil',       gquery: 'oil_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'biomass',   gquery: 'biomass_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'wind',      gquery: 'wind_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'hydro',     gquery: 'hydro_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'solar',     gquery: 'solar_in_source_of_electricity_production'},
+      {left: 'el_prod',     right: 'waste',     gquery: 'waste_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'coal',      gquery: 'coal_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'nuclear',   gquery: 'nuclear_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'gas',       gquery: 'gas_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'oil',       gquery: 'oil_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'biomass',   gquery: 'biomass_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'wind',      gquery: 'wind_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'hydro',     gquery: 'hydro_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'solar',     gquery: 'solar_in_source_of_electricity_production'},
+      {left: 'heat_prod',   right: 'waste',     gquery: 'waste_in_source_of_electricity_production'}
     ]
 
   # Helper classes

        attr("x", (d) => @x d.x_offset()).
        attr("y", (d) => @y(d.y_center() + 5)).
        attr("dx", 5).
        text((d) -> d.get('id')).
        style("color", "black")

    refresh: =>
      @nodes.data(@module.nodes.models, (d) -> d.get('id')).
        transition().duration(500).
        attr("height", (d) => @y d.height()).
        attr("y", (d) => @y d.y_offset())

      @labels.data(@module.nodes.models, (d) -> d.get('id')).
        transition().duration(500).
        attr("y", (datum) => @y(datum.y_center() + 5))

      @links.data(@module.links.models, (d) -> d.cid).
        transition().duration(500).

  Node: class extends Backbone.Model
    @width: 170
    @horizontal_spacing: 400

    # vertical position of the node. Adds some margin between nodes
    #
    y_offset: =>
      offset = 0
      margin = 35
      for n in @siblings()
        break if n == this
        offset += n.value() + margin
      offset

    x_offset: => @get('column') * D3.sankey.Node.horizontal_spacing + 20
    x_center: => @x_offset() + D3.sankey.Node.width / 2
    y_center: => @y_offset() + @value() / 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)
      ])

    initialize: =>
      # shortcut to access the collection objects
      @module = D3.sankey
      @right_links = []
      @left_links = []

    # 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(@module.nodes.models, (node) -> node.get('column'))
      items[@get 'column']
    
    label: => @get('label') || @get('id')


  Link: class extends Backbone.Model
