【发布时间】: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