d3渲染图表退出不是一个功能

时间:2015-08-13 12:31:22

标签: javascript d3.js charts

我正在努力确保我可以用D3更新我的图表数据,我有以下功能:

fetchData(scope.filters.title_id, scope.filters.competence_id, scope.filters.from, scope.filters.to).then(function (data) {

    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.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

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

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

    city.append("path")
        .transition()
        .attr("class", "line")
        .attr("d", function (d) {
            return line(d.values);
        })
        .style("stroke", function (d) {
            return color(d.name);
        });

    city.append("text")
        .datum(function (d) {
            return {name: d.name, value: d.values[d.values.length - 1]};
        })
        .attr("transform", function (d) {
            return "translate(" + x(d.value.timestamp) + "," + y(d.value.temperature) + ")";
        })
        .attr("x", 3)
        .attr("dy", ".35em")
        .text(function (d) {
            return d.name;
        });

    city.exit().remove();

})

然而,每当我运行它时,我都会得到city.exit is not a function

该图表只是以一种意想不到的方式重新渲染:

原始图表

enter image description here

重新渲染后

enter image description here

正如您所看到的,图表数据不会重新呈现,但旧数据不会被删除。

我做错了什么?

小提琴

fiddle

**小提琴随着我的进步和我得到的答案不断更新!

2 个答案:

答案 0 :(得分:2)

您的变量city包含数据连接计算的输入选择。

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

此输入选择当然没有函数.exit()来返回要删除的元素。 selection.enter()的文档就如何组合输入,更新和退出选择提供了一个很好的例子:

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 */

对于您的代码,以下内容应该有效:

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

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

答案 1 :(得分:2)

每次都要添加新轴,每次都要追加新的路径和文本元素。

好的,这是一个完整的解决方案......

  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>