Skip to content

Instantly share code, notes, and snippets.

@henryjameslau
Last active August 13, 2019 21:01
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 henryjameslau/cf4965e5de6fcc47ca53b9707d2eeeca to your computer and use it in GitHub Desktop.
Save henryjameslau/cf4965e5de6fcc47ca53b9707d2eeeca to your computer and use it in GitHub Desktop.
Tween position of d3-annotation
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-annotation/2.3.2/d3-annotation.js" type="text/javascript"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var x = d3.scaleLinear()
.rangeRound([0, 960]);
x.domain([0, 400])
annotations = [{
note: {
label: "00:00",
title: "You"
},
x:x(0),
y:500/2,
dy: 20,
dx: 20,
subject: {
radius: 10,
radiusPadding: 5
},
type:d3.annotationCalloutCircle
}]
makeAnnotations = d3.annotation()
.annotations(annotations)
svg.append('g')
.attr('class', 'annotation-group')
.call(makeAnnotations)
d3.select('.annotation-group')
.transition()
.duration(4000)
.tween('updateAnno',function(d){
xTrans = d3.interpolateNumber(0,200)
timeTrans = d3.interpolateDate(new Date("January 01, 2019 00:00:00"),new Date(new Date("January 01, 2019 00:00:00").getTime()+200*60000))
return function(t){
annotations[0].x = x(xTrans(t));
annotations[0].note.label = d3.timeFormat("%H:%M")(timeTrans(t))
makeAnnotations.annotations(annotations)
makeAnnotations.update()
}
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment