javascript - Creating SVG elements dynamically -
i trying create circles in d3js dynamically, reason fails. requirement show 2 small circles on rectangle when hover rectangle. on mouse out event circles should hide. tried creating rect
object , passing function, in function group of rectangle , try append circles
it.
can tell me error is?
function drawcircle(rect, side){ var g = (rect.parentnode); // var g = d3.select(aaa.parentnode); // console.log(g.attr('id')); g.append('rect') .attr('x', 10) .attr('y', 10) .attr('width', 500) .attr('height', 500) .style('fill', 'red') ; var x = 0, y = 0, r =0; var rectheight = parsefloat(rect.attr('height')); var rectwidth = parsefloat(rect.attr('width')); var rectx = parsefloat(rect.attr('x')); var recty = parsefloat(rect.attr('y')); y = recty + rectheight/2; r = rectheight/8; if(side == 'left'){ x = rectx; }else{ x = rectx + rectwidth; } g.append('circle') .attr('cx', x) .attr('cy', y) .attr('r', r) .style('visibility', 'hidden') .on('mouseover', function(){ g .selectall('circle') .style('visibility', 'visible') ; }) .on('mouseout', function () { g .selectall('circle') .style('visibility', 'hidden') }); rect .on('mouseover', function(){ // alert('hover'); g .selectall('circle') .style('visibility', 'visible') ; }) .on('mouseout', function(){ g .selectall('circle') .style('visibility', 'hidden') }) } var rectangle = { width : 50, height:50, x:100, y:100 }; var svg = d3.select('svg'); var g = svg.append('g').attr('id', 'group'); console.log(g.attr('id')); var rect = g.append('rect') .attr('id', 'aaa') .attr('x', rectangle.x) .attr('y', rectangle.y) .attr('width', rectangle.width) .attr('height', rectangle.height) .style('fill', 'blue') .on('mouseover', function(){ drawcircle(this, 'right'); drawcircle(this, 'left'); }) ;
you want this?
[0][0] gives underlying dom element if have d3 selection. removed big red test rect stopped working.
function drawcircle(rect, side){ var g = d3.select(rect[0][0].parentnode); var x = 0, y = 0, r =0; var rectheight = parsefloat(rect.attr('height')); var rectwidth = parsefloat(rect.attr('width')); var rectx = parsefloat(rect.attr('x')); var recty = parsefloat(rect.attr('y')); y = recty + rectheight/2; r = rectheight/8; if(side == 'left'){ x = rectx; }else{ x = rectx + rectwidth; } g.append('circle') .attr('cx', x) .attr('cy', y) .attr('r', r) .style('visibility', 'hidden') .on('mouseover', function(){ g .selectall('circle') .style('visibility', 'visible') ; }) .on('mouseout', function () { g .selectall('circle') .style('visibility', 'hidden') }); rect .on('mouseover', function(){ g .selectall('circle') .style('visibility', 'visible') ; }) .on('mouseout', function(){ g .selectall('circle') .style('visibility', 'hidden') }) } var rectangle = { width : 50, height:50, x:100, y:100 }; var svg = d3.select('svg'); var g = svg.append('g').attr('id', 'group'); console.log(g.attr('id')); var rect = g.append('rect') .attr('id', 'aaa') .attr('x', rectangle.x) .attr('y', rectangle.y) .attr('width', rectangle.width) .attr('height', rectangle.height) .style('fill', 'blue') .on('mouseover', function(){ drawcircle(rect, 'right'); drawcircle(rect, 'left'); }) ;
Comments
Post a Comment