D3可缩放时间轴,如何获取实际极值?
D3 zoomable timeline, how to get actual extreme values?
我在 d3 中有一个可缩放的时间轴:
var zoom = d3.behavior.zoom().on("zoom", draw);
默认域名为1年,将呈现如下:
2015 年 1 月 20 日......2 月 2 日......2016 年 2 月 10 日
当我放大或缩小时,我想获得实际的可见限制日期。
例如当我放大时我有
2015 年 8 月 10 日 ....2015 年 8 月 30 日....2015 年 9 月 9 日
我会捕捉比例的第一个和最后一个值(8 月 10 日和 9 月 9 日)
我的代码:
svg.append("rect")
.attr("class", "pane")
.attr("width", width)
.attr("height", height)
.call(zoom)
.on("mouseup", function(){
//...here i need to catch 10 aug and 09 sep
});
我该怎么办?
x 比例域将 return 实际视口中的第一个和最后一个日期。
var zoom = d3.behavior.zoom().x(x).on("zoom", function(){
focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
var firstDate = x.domain()[0];
var lastDate = x.domain()[1];
});
希望对您有所帮助!
如果您 link 您的 zoom
到您的域:
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.on("zoom", zoomed);
然后 x.domain()
在缩放后自动神奇地设置:
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 + ")")
.call(zoom)
.on("mouseup", function(d){
console.log(x.domain());
});
示例:
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoom + Pan</title>
<style>
body {
position: relative;
width: 960px;
}
svg {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
rect {
fill: #ddd;
}
.axis path,
.axis line {
fill: none;
stroke: #fff;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([new Date(2012, 0, 1), new Date(2013, 0, 1)])
.range([0, width]);
var y = d3.scale.linear()
.domain([-height / 2, height / 2])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(-height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
.tickSize(-width);
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.on("zoom", zoomed);
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 + ")")
.call(zoom)
.on("mouseup", function(d){
console.log(x.domain());
})
svg.append("rect")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
function zoomed() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
}
</script>
</body>
我在 d3 中有一个可缩放的时间轴:
var zoom = d3.behavior.zoom().on("zoom", draw);
默认域名为1年,将呈现如下:
2015 年 1 月 20 日......2 月 2 日......2016 年 2 月 10 日
当我放大或缩小时,我想获得实际的可见限制日期。 例如当我放大时我有
2015 年 8 月 10 日 ....2015 年 8 月 30 日....2015 年 9 月 9 日
我会捕捉比例的第一个和最后一个值(8 月 10 日和 9 月 9 日) 我的代码:
svg.append("rect")
.attr("class", "pane")
.attr("width", width)
.attr("height", height)
.call(zoom)
.on("mouseup", function(){
//...here i need to catch 10 aug and 09 sep
});
我该怎么办?
x 比例域将 return 实际视口中的第一个和最后一个日期。
var zoom = d3.behavior.zoom().x(x).on("zoom", function(){
focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
var firstDate = x.domain()[0];
var lastDate = x.domain()[1];
});
希望对您有所帮助!
如果您 link 您的 zoom
到您的域:
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.on("zoom", zoomed);
然后 x.domain()
在缩放后自动神奇地设置:
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 + ")")
.call(zoom)
.on("mouseup", function(d){
console.log(x.domain());
});
示例:
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoom + Pan</title>
<style>
body {
position: relative;
width: 960px;
}
svg {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
rect {
fill: #ddd;
}
.axis path,
.axis line {
fill: none;
stroke: #fff;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([new Date(2012, 0, 1), new Date(2013, 0, 1)])
.range([0, width]);
var y = d3.scale.linear()
.domain([-height / 2, height / 2])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(-height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
.tickSize(-width);
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.on("zoom", zoomed);
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 + ")")
.call(zoom)
.on("mouseup", function(d){
console.log(x.domain());
})
svg.append("rect")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
function zoomed() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
}
</script>
</body>