【问题标题】:What does brush.event do when animating brush events?为画笔事件设置动画时,brush.event 做了什么?
【发布时间】:2015-04-04 07:01:09
【问题描述】:

问题:

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

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

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


先前的研究:

我查看了API docs,上面写着以下内容,但我必须承认我并不真正理解这个解释。

如果选择是一个过渡,则注册适当的补间,以便画笔在过渡过程中调度事件

我需要一些帮助来解析调用 brush.event 时的基本机制吗?


(我想知道的原因):

我想将此功能实现到 Meteor.js 应用程序中。事实证明,如果我注释掉这一行,滑块会卡入到位,但动画会丢失。如果我把线留在里面,画笔选择根本不会出现。我在询问这个动画的机制,以便弄清楚如何让它在我的流星项目中工作

更新:原来是package named "d3" on atmosphere is deprecated。如果我使用名为 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>

【问题讨论】:

    标签: javascript animation svg d3.js meteor


    【解决方案1】:

    关键就在您引用的那位之前的 API 文档中:

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

    在这个特定的例子中,调用brush.event 是必要的,因为范围是通过编程设置的(这就是捕捉的实现方式)。如果不调用它,则显示的画笔范围将与为比例设置的内容不对应,因为范围将在画笔处理函数中调整并且永远不会更新。

    但是,在您发布的特定代码中,您实际上不需要调用brush.event——它只需要在画笔处理函数内部(参见example with that first call removed) .

    现在,开始过渡。首先,让我们看看当我们删除它时会发生什么——参见example here。画笔现在会卡住,而不是逐渐移动到位。否则没有区别。

    您引用的文档部分基本上说,为了使过渡工作需要注意的一切都已完成。从技术上讲,过渡让所有合适的听众都知道它正在移动画笔,以便更新链接的显示(例如this example 中的图表)。

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

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

    【讨论】:

    • 感谢您的回答。尽管如此,仍然没有真正理解为什么需要为 Brush.event 设置动画。首先,我们已经进入了“刷过”的回调。其次,为什么这个事件调用在第一次被调用后会持续影响动画?
    • 我意识到我的问题并不完全清楚。我刚刚在上面编辑过。
    • 我仍然不完全确定您的意思,但我已经用更多解释更新了答案。如果有任何不清楚的地方,请告诉我。
    • 刷子事件中的这段代码不应该处理整个转换: d3.select(this).transition() .call(brush.extent(extent1)) .call(brush.event );
    • 确实如此。这就是逐渐将画笔移动到最近的一天的原因。
    猜你喜欢
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多