javascript - How to properly make D3 event listener? -
i have code draws bunch of hexagons need them appear white , other stuff when mouse on them. why isn't event listener working , need it?http://jsfiddle.net/rewbr/
var elementtags = ["google", 4, "wikipedia", "yahoo!", "cindy"]; var _s32 = (math.sqrt(3)/2); var = 75; var svgcontainer = d3.select("body") //create container .append("svg") .attr("width", 1000) .attr("height", 1000); var xdiff = [0, 200, 400, 600, 800, 1000, 1200, 1400]; var ydiff = [200, 200, 200, 200, 200, 200, 200];
for (index = 0; index < elementtags.length; index++) { var pointdata = [ [a+xdiff[index], 0+ydiff[index]], [a/2+xdiff[index], (a*_s32)+ydiff[index]], [-a/2+xdiff[index], (a*_s32)+ydiff[index]], [-a+xdiff[index], 0+ydiff[index]], [-a/2+xdiff[index], -(a*_s32)+ydiff[index]], [a/2+xdiff[index], -(a*_s32)+ydiff[index]], [a+xdiff[index], 0+ydiff[index]]];
var hexmouseover = function(){ console.log(this); this.style("fill", "white"); } var enterelements = svgcontainer.selectall("path.area") //draw element .data([pointdata]).enter().append("path") .style("fill", "#1d85e0") .style("stroke", "black") .attr("d", d3.svg.line()) .style("shape-rendering", "auto") .on("mouseover", hexmouseover);
} var addtext = svgcontainer.selectall("text").data(elementtags).enter().append("text");
var textelements = addtext .attr("x", function(d, i){ return xdiff[i];}) .attr("y", function(d, i){ return ydiff[i];}) .attr("font-family", "arial black") .attr("font-size", "20px") .attr("fill", "1a2e40") //.attr("font-variant", "small-caps") .style("text-anchor", "middle") .text(function(d, i){return d;});
you need select 'this' element d3 first:
.on("mouseover", function(){ console.log(this); console.log(d3.select(this)); d3.select(this).style("fill", "white");})
updated fiddle: http://jsfiddle.net/rewbr/3/
Comments
Post a Comment