       if showLegend then do ->
         n = if showNegativeLegend then 2*m else m
 
-        unless update
-          parent.select("div.legend")
-            .append("svg")
-              .attr("width", 100)
-              .attr("height", n * 15 + 15)
-            .append("g")
-              .attr("class", "content")
-              .attr("transform", "translate(0, 10)")
+        if update
+          parent.select("div.legend").select("svg").remove()          
+
+        parent.select("div.legend")
+          .append("svg")
+            .attr("width", 100)
+            .attr("height", n * 15 + 15)
+          .append("g")
+            .attr("class", "content")
+            .attr("transform", "translate(0, 10)")
 
         legend = parent.select("div.legend").select("g.content")
 

          selected = []
          parent.selectAll("input")
            .each (d) -> 
              if d3.select(this).property("checked")
                selected.push d.key

          fire "applyFilter", selected


      filterBtns.append("button")
        .attr("class", "btn btn-mini")
        .html("&times;")




    extents = (d.values.extent for d in data)
    extent = [ d3.min(extents, (d) -> d[0]), d3.max(extents, (d) -> d[1]) ]
    max = Math.max(-extent[0], extent[1])
    
    yscale.domain [0, max]

    timeExtents = (d.values.timeExtent for d in data)
    timeExtent = [ d3.min(timeExtents, (d) -> d[0]), d3.max(timeExtents, (d) -> d[1]) ]
    tscale.domain(timeExtent)

    numSteps = Math.max(1, interval.range.apply(this, timeExtent).length)
    stepWidth = Math.ceil(width / numSteps)


    unless update


      horizon = d3.select(this)

      ys = yscale.copy()
      
 

      canvas = horizon.select("canvas").node().getContext("2d")
      canvas.save()
