在查看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>
答案 0 :(得分:2)
关键在于API文档,就在您引用的位之前:
这在以编程方式设置画笔范围后触发侦听器时非常有用。
在这个特定的例子中,调用brush.event
是必要的,因为扩展程序是以编程方式设置的(这就是实现捕捉的方式)。如果没有调用,则显示的画笔范围将不对应于为音阶设置的内容,因为范围将在画笔处理功能中调整并且从不更新。
但是,在您发布的特定位代码中,您实际上并不需要致电brush.event
- 它只在内部发布画笔处理函数(参见example with that first call removed)。
现在,转型。首先,让我们看看当我们删除它时会发生什么 - 请参阅example here。现在刷子卡住而不是逐渐移动到位。否则没有区别。
您引用的文档部分基本上说,为了使转换工作,需要处理的所有事情都已完成。从技术上讲,过渡允许所有适当的听众知道它正在移动画笔,以便更新链接的显示(例如,考虑this example中的图表)。
就您的应用而言,听起来好像其他东西干扰了画笔,即(重新)在转换开始后设置状态。
更新:过渡期间调度的事件实际上并未触发您的处理程序,因为如果brushend
事件处理程序的第一行中出现这种情况,则会返回。您在调用brush.event
时正在执行的操作是安装处理函数,而不是处理单个事件 - all 之后调度的事件将被处理。