Created
December 6, 2011 09:20
-
-
Save sfrdmn/1437516 to your computer and use it in GitHub Desktop.
D3 Mouseover Example w/ trailing infobox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Mouse Over and Out with Moving Infobox</title> | |
| <script type="text/javascript" src="https://github.com/mbostock/d3/raw/fe671a70e236710412a514fa276e59f875f3c617/d3.js"></script> | |
| <link type="text/css" rel="stylesheet" href="style.css"> | |
| </head> | |
| <body> | |
| <!-- I've only commented changes relevant to this mouse follow stuff --> | |
| <!-- btw, this is what an html comment looks like --> | |
| <!-- here is our box which will move around. it is styled in the css/style.css file --> | |
| <div class="infobox"> | |
| <p>Here is where we will say something.</p> | |
| </div> | |
| <script type="text/javascript"> | |
| var w = 800; | |
| var h = 800; | |
| var data = []; | |
| for( var i = 0; i < 100; i++ ) { | |
| data[i] = Math.floor(Math.random()*100); | |
| } | |
| // this will be ran whenever we mouse over a circle | |
| var myMouseOverFunction = function() { | |
| var circle = d3.select(this); | |
| circle.attr("fill", "red" ); | |
| // show infobox div on mouseover. | |
| // block means sorta "render on the page" whereas none would mean "don't render at all" | |
| d3.select(".infobox").style("display", "block"); | |
| // add test to p tag in infobox | |
| d3.select("p").text("This circle has a radius of " + circle.attr("r") + " pixels."); | |
| } | |
| var myMouseOutFunction = function() { | |
| var circle = d3.select(this); | |
| circle.attr("fill", "steelblue" ); | |
| // display none removes element totally, whereas visibilty in last example just hid it | |
| d3.select(".infobox").style("display", "none"); | |
| } | |
| var myPositionFunction = function() { | |
| return Math.random() * w; | |
| } | |
| // this should be called when the mouse is moved | |
| // we will attach it to our svg area so that it detects mouse movement on our entire visualization | |
| // we are trying to get the infobox to move with the mouse | |
| var myMouseMoveFunction = function() { | |
| // save selection of infobox so that we can later change it's position | |
| var infobox = d3.select(".infobox"); | |
| // this returns x,y coordinates of the mouse in relation to our svg canvas | |
| var coord = d3.svg.mouse(this) | |
| // now we just position the infobox roughly where our mouse is | |
| infobox.style("left", coord[0] + 15 + "px" ); | |
| infobox.style("top", coord[1] + "px"); | |
| } | |
| var chart = d3.select("body") | |
| .append("svg:svg") | |
| .attr("width", w) | |
| .attr("height", h) | |
| .on('mousemove', myMouseMoveFunction); // here we attach our function which updates info box position | |
| // we attach it here, because we want it to work everywhere on our graphic | |
| chart.selectAll("circle").data(data) | |
| .enter().append("svg:circle") | |
| .attr("cx", myPositionFunction) | |
| .attr("cy", myPositionFunction) | |
| .attr("r", function(d) { | |
| return d; | |
| }) | |
| .attr("fill", "steelblue") | |
| .on("mouseover", myMouseOverFunction) | |
| .on("mouseout", myMouseOutFunction); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| html, body { | |
| margin: 0; | |
| } | |
| .infobox { | |
| position: absolute; | |
| width: 200px; | |
| padding: 10px; | |
| background-color: rgba(255,255,255,.75); | |
| } | |
| p { | |
| margin: 0; | |
| } | |
| ~ |
I think I solved my own problem by using
infobox.style("left", (d3.event.pageX) + 15 + "px" );
infobox.style("top", (d3.event.pageY) + "px");
Thanks again for the code!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Thanks for sharing your code. I am new to Javascript D3 and have a small question. If I add 10 lines of text (I left out '<' in the comment below):
...
body>
Add text here br>
Add text here br>
Add text here br>
Add text here br>
Add text here br>
Add text here br>
Add text here br>
Add text here br>
Add text here br>
Add text here br>
...
then the infobox no longer follows the mouse nicely - it seems to be too high. Do you have a way to solve this?
Cheers,
Toon