我如何在 d3 v3 中实现 brush.move

how do i implement brush.move in d3 v3

我正在尝试复制此示例中的画笔行为: https://bl.ocks.org/mbostock/87746f16b83cb9d5371394a001cbd772

我的主要重点是刷 select 整个 x 范围的 onclick。上面的例子做的完全一样,但是是在 d3v4 中实现的。在 d3v3 中,brush.move 和 .map() 函数不可用。我正在尝试 d3v3 相当于 brush.move。目前我的代码如下所示:

//brush slider display
                context.append("g")
                    .attr("class", "brush")
                    .call(brush)
                    .call(brush.event)
                    .selectAll("rect")
                    .attr("y", -6)
                    .attr("height", brushHeight + 7);

                context.append("g")
                    .call(brush)
                    .call(brush.event)
                    .selectAll(".overlay")
                    .on("mousedown touchstart", beforebrushed, true);

                /**
                 * beforeBrushed function
                 */
                function beforebrushed () {
                    logger.info("beforebrushed activated");
                    d3.event.stopImmediatePropagation();
                    d3.select(".brush").transition().call(brush.move, x.range());
                }

甚至没有调用 beforebrushed 函数。相反,这个错误是在我使用 brush.move 的行上抛出的。 TypeError: 无法读取未定义的 属性 'apply'

任何建议都会有很大帮助!谢谢

d3v3中没有brush.move,你应该用brush.event

...
.call(brush)
.call(brush.event);
...

我用 d3v3 重写了你提到的 blocks。请检查。很多代码不得不重写。如果您有任何问题,请随时提出。

var randomX = function() { return Math.random() * 10 },
    randomY = d3.random.normal(0.5, 0.12),
    data = d3.range(800).map(function() { return [randomX(), randomY()]; });

var svg = d3.select("svg"),
    margin = {top: 0, right: 50, bottom: 214, left: 10},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scale.linear()
  .range([0, width])
    .domain([0, 10]);
    
var defaultExtent = [3, 5];

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

var brush = d3.svg.brush()
 .x(x)
  .extent(defaultExtent)
 .on("brush", brushed)

var dot = g.append("g")
    .attr("fill-opacity", 0.2)
  .selectAll("circle")
  .data(data)
  .enter().append("circle")
    .attr("transform", function(d) { return "translate(" + x(d[0]) + "," + y(d[1]) + ")"; })
    .attr("r", 3.5);

var brushContainer = g.append("g")
    .attr("class", "brush")
    .call(brush)
    .call(brush.event);
    
brushContainer.selectAll("rect")
  .attr("height", height);
 
brushContainer.selectAll(".background")
  .on("mousedown", beforebrushed, true)
  .on("touchstart", beforebrushed, true);

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

function brushed() {
  var extent = brush.extent();

  dot.classed("selected", function(d) { return extent[0] <= d[0] && d[0] <= extent[1]; });
}

function beforebrushed() {
  d3.event.stopImmediatePropagation();
  
  brushContainer.transition()
    .duration(700)
     .call(brush.extent([0, 10]))
      .call(brush.event);
}
.selected {
  fill: red;
  fill-opacity: 1;
  stroke: brown;
}

.axis {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.extent {
  stroke: #fff;
  fill-opacity: .125;
  shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.5/d3.js"></script>
<svg width="960" height="306"></svg>