【问题标题】:D3.js Enter/update/exit not adding elements as expectedD3.js 输入/更新/退出未按预期添加元素
【发布时间】:2015-09-23 19:20:50
【问题描述】:

我有一个基于this example 的日/小时热图。我正在尝试编写一个 updateHeatMap 方法,以便我可以使用新数据动态更新热图。

我正在使用几个教程中建议的进入/更新/退出方法,这是我注意到的行为。我打开页面,数据就在那里,太好了。我更新了数据,它的元素更少了,所以旧的元素消失了,这正是我想要的。我用新元素再次更新数据,但它们没有出现,这不是我想要的。

initHeatMap 函数只在加载时调用一次,updateHeatMap 函数在每次数据更新时调用。

我将原来的日/小时热图示例稍微更改为如下所示:

$scope.heatMapData = $http.get(...

$scope.initHeatMap = function() {

$scope.margin = { top: 50, right: 0, bottom: 100, left: 30 },
  width = 960 - margin.left - margin.right,
  height = 430 - margin.top - margin.bottom,
  gridSize = Math.floor(width / 24),
  legendElementWidth = gridSize*2,
  buckets = 9,
  colors = ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"], // alternatively colorbrewer.YlGnBu[9]
  days = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  times = ["1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a", "12a", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p", "12p"];



  $scope.colorScale = d3.scale.quantile()
      .domain([0, buckets - 1, d3.max($scope.heatMapData, function (d) { return d.value; })])
      .range(colors);

  var svg = d3.select("#heatmap").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 + ")");

  var dayLabels = svg.selectAll(".dayLabel")
      .data(days)
      .enter().append("text")
        .text(function (d) { return d; })
        .attr("x", 0)
        .attr("y", function (d, i) { return i * gridSize; })
        .style("text-anchor", "end")
        .attr("transform", "translate(-6," + gridSize / 1.5 + ")")
        .attr("class", function (d, i) { return ((i >= 0 && i <= 4) ? "dayLabel mono axis axis-workweek" : "dayLabel mono axis"); });

  var timeLabels = svg.selectAll(".timeLabel")
      .data(times)
      .enter().append("text")
        .text(function(d) { return d; })
        .attr("x", function(d, i) { return i * gridSize; })
        .attr("y", 0)
        .style("text-anchor", "middle")
        .attr("transform", "translate(" + gridSize / 2 + ", -6)")
        .attr("class", function(d, i) { return ((i >= 7 && i <= 16) ? "timeLabel mono axis axis-worktime" : "timeLabel mono axis"); });

  var heatMap = svg.selectAll(".hour")
      .data($scope.heatMapData)
      .enter().append("rect")
      .attr("x", function(d) { return (d.hour - 1) * gridSize; })
      .attr("y", function(d) { return (d.day - 1) * gridSize; })
      .attr("rx", 4)
      .attr("ry", 4)
      .attr("class", "hour bordered")
      .attr("width", gridSize)
      .attr("height", gridSize)
      .style("fill", colors[0]);

  heatMap.transition().duration(1000)
      .style("fill", function(d) { return $scope.colorScale(d.value); });

  heatMap.append("title").text(function(d) { return d.value; });

  var legend = svg.selectAll(".legend")
      .data([0].concat($scope.colorScale.quantiles()), function(d) { return d; })
      .enter().append("g")
      .attr("class", "legend");

  legend.append("rect")
    .attr("x", function(d, i) { return legendElementWidth * i; })
    .attr("y", height)
    .attr("width", legendElementWidth)
    .attr("height", gridSize / 2)
    .style("fill", function(d, i) { return colors[i]; });

  legend.append("text")
    .attr("class", "mono")
    .text(function(d) { return "≥ " + Math.round(d); })
    .attr("x", function(d, i) { return legendElementWidth * i; })
    .attr("y", height + gridSize);
  };

这里是更新方法:

$scope.rowSelected() = function(){
  $http.get(...).then(function(result){
    $scope.heatMapData = result.data;
    $scope.updateHeatMap();
  });
}

$scope.updateHeatMap = function(){
    var selection = d3.select("#heatmap");
    var heatMap = selection.selectAll(".hour").data($scope.heatMapData);

    heatMap.enter().append("rect")
      .attr("x", function(d) { return (d.hour - 1) * gridSize; })
      .attr("y", function(d) { return (d.day - 1) * gridSize; })
      .attr("rx", 4)
      .attr("ry", 4)
      .attr("class", "hour bordered")
      .attr("width", gridSize)
      .attr("height", gridSize)
      .style("fill", colors[0]);

   heatMap.transition().duration(1000)
          .style("fill", function(d) {return $scope.colorScale(d.value);});

   heatMap.exit().remove();
};

【问题讨论】:

    标签: javascript angularjs d3.js


    【解决方案1】:

    我解决了这个问题。在 initHeatMap 中,svg 元素附加了“g”,然后在该“g”中创建 heatMap 变量。

    var svg = d3.select("#heatmap").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 + ")");
    
    var heatMap = svg.selectAll(".hour")...
    

    所以当我更新热图时,我需要先选择“g”,然后再添加数据。

    这是解决方案:

    var heatMap = selection.selectAll('g').selectAll(".hour")...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多