Skip to content

Instantly share code, notes, and snippets.

@riccardoscalco
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardoscalco/6a4d2e805a5e0f54218a to your computer and use it in GitHub Desktop.
Save riccardoscalco/6a4d2e805a5e0f54218a to your computer and use it in GitHub Desktop.
Time axis changing in real time
<div id="box">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
var svg = d3.select("#box")
.append("svg")
.attr('width', 900)
.attr('height', 100)
var endingDate = Date.now(),
startingDate = endingDate - 1000 * 60;
var dt = 1000;
var x = d3.time.scale()
.range([0,900])
.domain([startingDate,endingDate]);
var axis = svg.append("g")
.attr("class", "x axis")
.call(d3.svg.axis().scale(x).orient("bottom"));
axis.transition = d3.select({})
.transition()
.duration(1000)
.ease("linear");
(function tick() {
axis.transition = axis.transition.each(function() {
endingDate = Date.now();
startingDate = endingDate - 1000 * 60;
axis.call(d3.svg.axis()
.scale(d3.time.scale()
.range([0,900])
.domain([startingDate,endingDate]))
.orient("bottom"))
}).transition().each("start", tick);
})()
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment