brush.event 在对画笔事件进行动画处理时会做什么

What does brush.event do when animating brush events?

本文关键字:处理 动画 什么 event 画笔 事件 brush      更新时间:2023-09-26

问题:

在查看 bl.ocks.org 上发布的画笔捕捉功能时,我对这段代码感到困惑:

var gBrush = svg.append("g")
        .attr("class", "brush")
        .call(brush)
        .call(brush.event);

需要明确的是,我了解.call(brush.event)的作用,但我不明白为什么它在这个特定的代码块中。 即,我看到在"brushended"事件处理程序中进行了相同的调用,但是为什么"brushend"事件中的转换取决于上面复制的调用?

<小时 />

先前的研究:

我查看了 API 文档,其中说了以下内容,但我必须承认我并不真正理解这种解释。

如果选定内容是过渡,则注册相应的补间,以便画笔在过渡过程中调度事件

我需要一些帮助来解析brush.event电话中的细节机制?

<小时 />

(我想知道的原因):

我想在 Meteor.js 应用程序中实现此功能。 事实证明,如果我注释掉这一行,滑块会捕捉到位,但动画会丢失。 但是,如果我保留该行,则画笔选择根本不显示。 我正在询问这个动画的机制,以便弄清楚如何让它在我的流星项目中工作

更新:事实证明,大气中名为"d3"的软件包已被弃用。 如果我使用名为 d3.js:d3 的包,一切都会很好。

<小时 />

完整代码:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis text {
  font: 11px sans-serif;
}
.axis path {
  display: none;
}
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
.grid-background {
  fill: #ddd;
}
.grid line,
.grid path {
  fill: none;
  stroke: #fff;
  shape-rendering: crispEdges;
}
.grid .minor.tick line {
  stroke-opacity: .5;
}
.brush .extent {
  stroke: #000;
  fill-opacity: .125;
  shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 200, right: 40, bottom: 200, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;
var x = d3.time.scale()
    .domain([new Date(2013, 7, 1), new Date(2013, 7, 15) - 1])
    .range([0, width]);
var brush = d3.svg.brush()
    .x(x)
    .extent([new Date(2013, 7, 2), new Date(2013, 7, 3)])
    .on("brushend", brushended);
var svg = d3.select("body").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("rect")
    .attr("class", "grid-background")
    .attr("width", width)
    .attr("height", height);
svg.append("g")
    .attr("class", "x grid")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.svg.axis()
        .scale(x)
        .orient("bottom")
        .ticks(d3.time.hours, 12)
        .tickSize(-height)
        .tickFormat(""))
  .selectAll(".tick")
    .classed("minor", function(d) { return d.getHours(); });
svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.svg.axis()
      .scale(x)
      .orient("bottom")
      .ticks(d3.time.days)
      .tickPadding(0))
  .selectAll("text")
    .attr("x", 6)
    .style("text-anchor", null);
var gBrush = svg.append("g")
    .attr("class", "brush")
    .call(brush)
    .call(brush.event);
gBrush.selectAll("rect")
    .attr("height", height);
function brushended() {
  if (!d3.event.sourceEvent) return; // only transition after input
  var extent0 = brush.extent(),
      extent1 = extent0.map(d3.time.day.round);
  // if empty when rounded, use floor & ceil instead
  if (extent1[0] >= extent1[1]) {
    extent1[0] = d3.time.day.floor(extent0[0]);
    extent1[1] = d3.time.day.ceil(extent0[1]);
  }
  d3.select(this).transition()
      .call(brush.extent(extent1))
      .call(brush.event);
}
</script>

密钥位于 API 文档中,就在您引用的位之前:

这对于在以编程方式设置画笔范围后触发侦听器非常有用。

在此特定示例中,调用 brush.event 是必需的,因为范围是以编程方式设置的(这就是捕捉的实现方式)。如果未调用,则显示的画笔范围将与为比例设置的范围不对应,因为范围将在画笔手柄函数中进行调整,并且永远不会更新。

但是,在您发布的特定代码中,您实际上不需要调用brush.event - 它仅在 brush 处理程序函数中是必需的(请参阅删除第一个调用的示例)。

现在,进入过渡。首先,让我们看看当我们删除它时会发生什么 - 请参阅此处的示例。画笔现在会折断,而不是逐渐移动到位。否则没有区别。

您引用的文档部分基本上是说,为了使过渡工作而需要处理的所有事情都已完成。从技术上讲,过渡让所有适当的侦听器知道它正在移动画笔,以便更新链接的显示(例如,考虑本例中的图表)。

就您的应用程序而言,听起来好像有其他东西干扰了画笔,即在转换开始后(重新)设置状态。

更新:在转换期间调度的事件实际上不会触发处理程序,因为如果在事件处理程序的第一行中出现这种情况brushend则会返回。调用brush.event时,您要做的是安装处理程序函数,而不是处理单个事件 - 之后调度的所有事件都将被处理。