D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
EmbraceLife
Full window
Github gist
23. CSS hover vs mouseover-mouseout-transition
Built with
blockbuilder.org
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>D3: Sorting and re-sorting visual elements with delay</title> <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script> <style type="text/css"> rect:hover { fill: orange; } </style> </head> <body> <script type="text/javascript"> var w = 600; var h = 250, margin = {top: 20, bottom:20, left:30, right:40}, width = w - margin.right - margin.left, height = h - margin.top - margin.bottom; var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h) .append("g") .attr("transform", "translate("+margin.left+","+margin.top+")"); var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13, 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ]; var xScale = d3.scaleBand() .domain(d3.range(dataset.length)) .rangeRound([0, width]) .padding(0.05); var yScale = d3.scaleLinear() .domain([0, d3.max(dataset)]) .range([height, 0]); svg.selectAll("rect") .data(dataset) .enter() .append("rect") .attr("x", function(d, i) { return xScale(i); }) .attr("y", function(d) { return yScale(d); }) .attr("width", xScale.bandwidth()) .attr("height", function(d) { return height - yScale(d); }) .attr("fill", function(d) { return "rgb(0, 0, " + (d * 10) + ")"; }) .on("click", function() { sortBars(); }) // ---- during click transition, if mouse transition occurs, click transition will be disrupted. // Solution: to use CSS hover replace mouseover and mouseout transition // Drawbacks: losing fade effect => duration(250) // ---- event listener: .on("mouseover", function(){}) // .on("mouseover", function() { // d3.select(this) // .attr("fill", "orange"); // }) // .on("mouseout", function(d) { // d3.select(this) // .transition() // .duration(250) // .attr("fill", "rgb(0, 0, " + (d * 10) + ")"); // }); ; //Create labels svg.selectAll("text") .data(dataset) .enter() .append("text") .text(function(d) { return d; }) .attr("text-anchor", "middle") .attr("x", function(d, i) { return xScale(i) + xScale.bandwidth() / 2; }) .attr("y", function(d) { return yScale(d) + 14; }) .attr("font-family", "sans-serif") .attr("font-size", "11px") .attr("fill", "white"); //Define sort order flag var sortOrder = false; //Define sort function var sortBars = function() { //Flip value of sortOrder sortOrder = !sortOrder; svg.selectAll("rect") .sort(function(a, b) { if (sortOrder) { return d3.ascending(a, b); } else { return d3.descending(a, b); } }) .transition() .delay(function(d, i) { return i * 50; }) .duration(1000) .attr("x", function(d, i) { return xScale(i); }); svg.selectAll("text") .sort(function(a, b) { if (sortOrder) { return d3.ascending(a, b); } else { return d3.descending(a, b); } }) .transition() .delay(function(d, i) { return i * 50; }) .duration(1000) .attr("x", function(d, i) { return xScale(i) + xScale.bandwidth()/2; }); }; </script> </body> </html>
https://d3js.org/d3.v4.min.js