d3.js矩形缩放不工作
这是我当前的代码d3.js矩形缩放不工作,d3.js,zooming,D3.js,Zooming,这是我当前的代码 <!DOCTYPE html> <meta charset="utf-8"> <title>Zoom by Rectangle</title> <script src="http://d3js.org/d3.v2.min.js?2.10.1"></script> <style> body { font-family: sans-serif; } .noselect { -webki
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoom by Rectangle</title>
<script src="http://d3js.org/d3.v2.min.js?2.10.1"></script>
<style>
body {
font-family: sans-serif;
}
.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
svg {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
rect {
fill: #ddd;
}
rect.zoom {
stroke: steelblue;
fill-opacity: 0.5;
}
.axis path, .axis line {
fill: none;
stroke: #fff;
}
</style>
<p>
<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.scale.linear().range([ 0, width ]);
var y = d3.scale.linear().range([ height, 0 ]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis().scale(x).orient("bottom");
var yAxis = d3.svg.axis().scale(y).orient("left");
var zoom = d3.behavior.zoom().x(x).y(y).on("zoom", refresh);
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)
.append("g")
.on(
"mousedown",
function() {
console.log("Mouse down");
var e = this;
var origin = d3.mouse(e);
var rect = svg.append("rect").attr("class", "zoom");
d3.select("body").classed("noselect", true);
origin[0] = Math.max(0, Math.min(width, origin[0]));
origin[1] = Math
.max(0, Math.min(height, origin[1]));
d3.select(window).on(
"mousemove.zoomRect",
function() {
console.log("Mouse moving");
var m = d3.mouse(e);
m[0] = Math.max(0, Math
.min(width, m[0]));
m[1] = Math.max(0, Math.min(height,
m[1]));
console.log(m[0]);
console.log(m[1]);
rect.attr("x",
Math.min(origin[0], m[0]))
.attr(
"y",
Math.min(origin[1],
m[1])).attr(
"width",
Math.abs(m[0]
- origin[0]))
.attr(
"height",
Math.abs(m[1]
- origin[1]));
}).on(
"mouseup.zoomRect",
function() {
console.log("Mouse up");
d3.select(window).on(
"mousemove.zoomRect", null).on(
"mouseup.zoomRect", null);
d3.select("body").classed(
"noselect", false);
var m = d3.mouse(e);
m[0] = Math.max(0, Math
.min(width, m[0]));
m[1] = Math.max(0, Math.min(height,
m[1]));
if (m[0] !== origin[0]
&& m[1] !== origin[1]) {
zoom.x(
x
.domain([
origin[0],
m[0] ].map(
x.invert)
.sort()))
.y(
y.domain([
origin[1],
m[1] ].map(
y.invert)
.sort()));
}
rect.remove();
refresh();
}, true);
d3.event.stopPropagation();
});
var data = [ {
"x" : 30,
"y" : 30,
"r" : 20,
"c" : "green",
"s" : "s1"
}, {
"x" : 70,
"y" : 70,
"r" : 20,
"c" : "purple",
"s" : "s2"
}, {
"x" : 110,
"y" : 100,
"r" : 20,
"c" : "red",
"s" : "s3"
} ];
var data1 = [ {
"x" : 30,
"y" : 30
}, {
"x" : 70,
"y" : 70
}, {
"x" : 90,
"y" :90
} ];
x.domain(d3.extent(data, function(d) {
return d.x;
})).nice();
y.domain(d3.extent(data, function(d) {
return d.y;
})).nice();
svg.append("rect").attr("width", width).attr("height", height);
svg.append("g").attr("class", "x axis").attr("transform",
"translate(0," + height + ")").call(xAxis).append("text").attr(
"class", "label").attr("x", width).attr("y", -6).style(
"text-anchor", "end").text("Yield");
svg.append("g").attr("class", "y axis").call(yAxis).append("text")
.attr("class", "label").attr("transform", "rotate(-90)").attr(
"y", 6).attr("dy", ".71em").style("text-anchor", "end")
.text("Skew")
var line = d3.svg.line().x(function(d) {
return x(d.x);
}).y(function(d) {
return y(d.y);
});
svg.append("path").datum(data1).attr("class", "line").attr("stroke",
"steelblue").attr("stroke-width", 1.5).attr("d", line).attr(
"fill", "none");
svg.selectAll(".dot").data(data).enter().append("circle").attr("class",
"dot").attr("r", 3.5).attr("cx", function(d) {
return x(d.x);
}).attr("cy", function(d) {
return y(d.y);
}).style("fill", function(d) {
return color(d.c);
});
var legend = svg.selectAll(".legend").data(color.domain()).enter()
.append("g").attr("class", "legend").attr("transform",
function(d, i) {
return "translate(0," + i * 20 + ")";
});
legend.append("rect").attr("x", width - 18).attr("width", 18).attr(
"height", 18).style("fill", color);
legend.append("text").attr("x", width - 24).attr("y", 9).attr("dy",
".35em").style("text-anchor", "end").text(function(d) {
return d;
});
function refresh() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
}
</script>
按矩形缩放
身体{
字体系列:无衬线;
}
noselect先生{
-webkit触摸标注:无;
-webkit用户选择:无;
-khtml用户选择:无;
-moz用户选择:无;
-ms用户选择:无;
用户选择:无;
}
svg{
字体:10px无衬线;
形状渲染:边缘清晰;
}
直肠{
填充:#ddd;
}
矩形缩放{
笔画:钢蓝;
填充不透明度:0.5;
}
.轴路径,.轴线{
填充:无;
冲程:#fff;
}
var保证金={
前20名,
右:20,,
底数:30,
左:40
},宽度=960-margin.left-margin.right,高度=500-margin.top
-边缘。底部;
var x=d3.scale.linear()范围([0,宽度]);
变量y=d3.scale.linear().range([height,0]);
var color=d3.scale.category10();
var xAxis=d3.svg.axis().scale(x.orient(“底部”);
var yAxis=d3.svg.axis().scale(y).orient(“左”);
var zoom=d3.behavior.zoom().x(x).y(y).on(“缩放”,刷新);
var svg=d3
.选择(“正文”)
.append(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”,
“翻译(“+margin.left+”,“+margin.top+”)
.呼叫(缩放)
.附加(“g”)
.在(
“鼠标镇”,
函数(){
console.log(“鼠标按下”);
var e=此;
var origin=d3.小鼠(e);
var rect=svg.append(“rect”).attr(“class”,“zoom”);
d3.选择(“主体”)。分类(“noselect”,真);
原点[0]=数学最大值(0,数学最小值(宽度,原点[0]);
原点[1]=数学
.max(0,数学最小值(高度,原点[1]);
d3.选择(窗口)。打开(
“mousemove.zoomRect”,
函数(){
log(“鼠标移动”);
var m=d3.小鼠(e);
m[0]=Math.max(0,Math
.min(宽度,m[0]);
m[1]=数学最大值(0,数学最小值(高度,
m[1]);
console.log(m[0]);
console.log(m[1]);
rect.attr(“x”,
Math.min(原点[0],m[0]))
艾特先生(
“y”,
Math.min(原点[1],
m[1]).attr(
“宽度”,
Math.abs(m[0]
-来源[0]))
艾特先生(
“高度”,
数学.abs(m[1]
-来源[1]);
}).在(
“mouseup.zoomRect”,
函数(){
console.log(“鼠标向上”);
d3.选择(窗口)。打开(
“mousemove.zoomRect”,空)。打开(
“mouseup.zoomRect”,空);
d3.选择(“主体”)。分类(
“noselect”,假);
var m=d3.小鼠(e);
m[0]=Math.max(0,Math
.min(宽度,m[0]);
m[1]=数学最大值(0,数学最小值(高度,
m[1]);
如果(m[0]!==原点[0]
&&m[1]!==原点[1]){
zoom.x(
x
.域名([
原点[0],
m[0]].map(
x、 倒置)
.sort())
.y(
y、 领域([
来源[1],
m[1]].map(
y、 倒置)
.sort());
}
rect.remove();
刷新();
},对);
d3.event.stopPropagation();
});
var数据=[{
“x”:30,
“y”:30,
“r”:20,
“c”:“绿色”,