score:3

Accepted answer

you are adding new axes every time and appending new path and text elements every time.

ok, here's a full solution...

  var margin = {
      top: 6,
      right: 80,
      bottom: 30,
      left: 30
    },
    width = 600 - 20 - margin.left - margin.right,
    height = 200 - margin.top - margin.bottom;

  var parsedate = d3.time.format("%d-%m-%y").parse;

  var x = d3.time.scale()
    .range([0, width]);

  var y = d3.scale.linear()
    .range([height, 0]);

  var color = d3.scale.category10();

  var xaxis = d3.svg.axis()
    .scale(x)
    .ticksize(-height)
    .tickpadding(10)
    .ticksubdivide(true)
    .orient("bottom");

  var yaxis = d3.svg.axis()
      .scale(y)
      .tickpadding(10)
      .ticksize(-width)
      .ticksubdivide(true)
      .ticks(5)
      .orient("left")
      .tickformat(d3.format(".0f"));

  var line = d3.svg.line()
    .interpolate("cardinal")
    .x(function (d) {
      return x(d.timestamp);
    })
    .y(function (d) {
      return y(d.temperature);
    });

  var svg = d3.select("#progresschart").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")

  svg.append("g")
    .attr("class", "y axis")
    .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", ".71em")
    .style("text-anchor", "end")
    .text("niveau");

  var render = function (newdata, t) {

    var data = fetchdata(newdata);

    color.domain(d3.keys(data[0]).filter(function (key) {
      return key !== "timestamp";
    }));

    data.foreach(function (d) {
      d.timestamp = parsedate(d.timestamp);
    });

    var cities = color.domain().map(function (name) {
      return {
        name: name,
        values: data.map(function (d) {
          return {
            timestamp: d.timestamp,
            temperature: +d[name]
          };
        })
      };
    });

    x.domain(d3.extent(data, function (d) {
      return d.timestamp;
    }));

    y.domain([
      d3.min(cities, function (c) {
        return d3.min(c.values, function (v) {
          return v.temperature;
        });
      }),
      d3.max(cities, function (c) {
        return d3.max(c.values, function (v) {
          return v.temperature;
        });
      })]);

    svg.selectall(".x.axis")
      .call(xaxis);

    svg.selectall(".y.axis")
      .transition().duration(t)
      .call(yaxis)

    var city = svg.selectall(".city")
          .data(cities),
        cityenter = city.enter().append("g")
          .attr("class", "city");

    cityenter
      .append("path")
      .attr("class", "line");

    city.select(".line")
      .transition().duration(t)
      .attr("d", function (d) {
        return line(d.values);
      })
      .style("stroke", function (d) {
        return color(d.name);
      });

    cityenter.append("text")
      .attr("x", 3)
      .attr("dy", ".35em");
    city.select("text")
      .text(function (d) {
        return d.name;
      })
      .transition().duration(t)
      .attr("transform", function (d) {
          var final = d.values[d.values.length - 1];
          return "translate(" + x(final.timestamp) + "," + y(final.temperature) + ")";
      });

    city.exit().remove();

  };

  var fetchdata = function (newdata) {
    if (!newdata) {
      return [{
        forventet: 8,
        nuværende: 1,
        timestamp: "12-4-2015"
      }, {
        forventet: 8,
        nuværende: 2,
        timestamp: "12-5-2015"
      }, {
        forventet: 8,
        nuværende: 7,
        timestamp: "12-6-2015"
      }]
    } else {
      return [{
        forventet: 2,
        nuværende: 3,
        timestamp: "12-4-2015"
      }, {
        forventet: 6,
        nuværende: 5,
        timestamp: "12-5-2015"
      }, {
        forventet: 4,
        nuværende: 7,
        timestamp: "12-6-2015"
      }]
    }
  };

  render(false, 0);

  settimeout(function () {
    render(true, 2000)
  }, 2000)
    .grid .tick {
      stroke: lightgrey;
      opacity: 0.7;
      shape-rendering: crispedges;
    }
    .grid path {
      stroke-width: 0;
    }
    .axis path {
      fill: none;
      stroke: #bbb;
      shape-rendering: crispedges;
    }
    .axis text {
      fill: #555;
    }
    .axis line {
      stroke: #e7e7e7;
      shape-rendering: crispedges;
    }
    .axis, .axis-label {
      font-size: 12px;
    }
    .line {
      fill: none;
      stroke-width: 1.5px;
    }
    .dot {
      /* consider the stroke-with the mouse detect radius? */
      stroke: transparent;
      stroke-width: 10px;
      cursor: pointer;
    }
    .dot:hover {
      stroke: rgba(68, 127, 255, 0.3);
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="progresschart"></div>

score:2

your variable city contains the enter selection computed by the data join.

var city = svg.selectall(".city")
    .data(cities)
    .enter()

this enter selection will, of course, have no function .exit() to return the elements to remove. the documentation of selection.enter() has a good example on how to combine the enter, update and exit selection:

var update_sel = svg.selectall("circle").data(data)
update_sel.attr(/* operate on old elements only */)
update_sel.enter().append("circle").attr(/* operate on new elements only */)
update_sel.attr(/* operate on old and new elements */)
update_sel.exit().remove() /* complete the enter-update-exit pattern */

for your code the following should work:

var city = svg.selectall(".city")
    .data(cities);

city.enter().append("g")
    .attr("class", "city");

Related Query

More Query from same tag