D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
officeofjane
Full window
Github gist
Date slider
Adapted from Mike Bostocks's
drag slider
.
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { font-family:"avenir next", Arial, sans-serif; font-size: 12px; color: #696969; } .ticks { font-size: 10px; } .track, .track-inset, .track-overlay { stroke-linecap: round; } .track { stroke: #000; stroke-opacity: 0.3; stroke-width: 10px; } .track-inset { stroke: #ddd; stroke-width: 8px; } .track-overlay { pointer-events: stroke; stroke-width: 50px; stroke: transparent; cursor: crosshair; } .handle { fill: #fff; stroke: #000; stroke-opacity: 0.5; stroke-width: 1.25px; } </style> </head> <body> <div id="slider"></div> <script> var formatDateIntoYear = d3.timeFormat("%Y"); var formatDate = d3.timeFormat("%b %Y"); var startDate = new Date("2004-11-01"), endDate = new Date("2017-04-01"); var margin = {top:0, right:50, bottom:0, left:50}, width = 960 -margin.left - margin.right, height = 500 - margin.top - margin.bottom; var svg = d3.select("#slider") .append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height); var x = d3.scaleTime() .domain([startDate, endDate]) .range([0, width]) .clamp(true); var slider = svg.append("g") .attr("class", "slider") .attr("transform", "translate(" + margin.left + "," + height / 2 + ")"); slider.append("line") .attr("class", "track") .attr("x1", x.range()[0]) .attr("x2", x.range()[1]) .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) .attr("class", "track-inset") .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) .attr("class", "track-overlay") .call(d3.drag() .on("start.interrupt", function() { slider.interrupt(); }) .on("start drag", function() { hue(x.invert(d3.event.x)); })); slider.insert("g", ".track-overlay") .attr("class", "ticks") .attr("transform", "translate(0," + 18 + ")") .selectAll("text") .data(x.ticks(10)) .enter() .append("text") .attr("x", x) .attr("y", 10) .attr("text-anchor", "middle") .text(function(d) { return formatDateIntoYear(d); }); var label = slider.append("text") .attr("class", "label") .attr("text-anchor", "middle") .text(formatDate(startDate)) .attr("transform", "translate(0," + (-25) + ")") var handle = slider.insert("circle", ".track-overlay") .attr("class", "handle") .attr("r", 9); function hue(h) { handle.attr("cx", x(h)); label .attr("x", x(h)) .text(formatDate(h)); svg.style("background-color", d3.hsl(h/1000000000, 0.8, 0.8)); } </script> </body>
https://d3js.org/d3.v4.min.js