d3 comes with varied support for interaction: event handlers and more specific tools for brushing, zooming...
d3's .on
function ties DOM-event handlers to selections.
var circles = svg.selectAll('circle')
.on('mouseover', function(d){
console.log(this);
console.log(d);
});
Let's try it on: code/d3-interaction/01_bitcoin_vis.html (or use your own!)
First, console.log elements that have been interacted with. Then, highlight them.
Useful DOM-events:
Mozilla: DOM-events
Sometimes very useful: pointer-events: none
(CSS)
Most common type of interaction: tooltips!
Not directly supported in d3, but easy to build yourself:
d3noob: Simple d3.js tooltips
Or use qTip2 or one of the other 75.000 tooltip libraries out there...
Transitions can be tweaked using:
var t = d3.transition();
// add initial delay:
t.delay(500);
// change duration:
t.duration(5000);
// add event handlers:
t.on("end", function(d){ ... });
Want to be really fancy? Change the easings on the transitions!
d3.transition().ease(d3.easeBounceInOut)
d3-ease
d3: Life of a transition
Let's try it on: code/d3-interaction/02_more_transitions.html
d3 has several specific behaviors supporting brushing, dragging, ...
Read up on d3-drag and d3.event, then use the basic d3-template from code/d3-basics/template.html to:
Draw some random circles on screen. Then make them draggable.
d3's zoom makes handling zoom+pan easier.
d3-zoom
Let's look at examples:
Mike Bostock: Pan & Zoom II
Mike Bostock: Pan & Zoom Axes
Brushes let you select parts of a chart:
Selfiecity: Selfiexploratory
d3 (of course) has a helper function for that: d3-brushd3.brush()
.
Go back to your circle-example from before. Instead of dragging them, add a brush and highlight them when they're inside the brush.
Another cool brushing demo:
Mike Bostock: Mona Lisa Histogram
You can also combine d3 behaviors:
Mike Bostock: Brush & Zoom
(even more advanced - enjoy :)
d3-annotation by Susie Lu