本文实例为大家分享了d3.js图形拖拽的具体代码,供大家参考,具体内容如下 !DOCTYPE htmlhtml lang="en"head meta charset="UTF-8" title拖拽操作/title/headbodyscript src = "d3.js"/scriptscript var width =2000, he
本文实例为大家分享了d3.js图形拖拽的具体代码,供大家参考,具体内容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>拖拽操作</title> </head> <body> <script src = "d3.js"></script> <script> var width =2000, height = 2000; var svg = d3.select("body").append("svg") .attr("width",width) .attr("height",height) var color = d3.scale.category20() //定义拖拽函数 var drag = d3.behavior.drag() .on("drag",dragmove) function dragmove(d){ d3.select(this) .attr("cx", d.cx = d3.event.x) .attr("cy", d.cy = d3.event.y) } //绘制圆形 var circles =[{cx:150,cy:200,r:30}, {cx:250,cy:200,r:30}] svg.selectAll("circle") .data(circles) .enter() .append("circle") .attr("cx",function(d){return d.cx}) .attr("cy",function(d){return d.cy}) .attr("r", function (d) { return d.r }) .attr("fill",color) .call(drag) </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。