Skip to content

Instantly share code, notes, and snippets.

@monaddle
Last active June 15, 2019 20:34
Show Gist options
  • Save monaddle/7d6f456e2f3cf4f013fd065c4fa5c18a to your computer and use it in GitHub Desktop.
Save monaddle/7d6f456e2f3cf4f013fd065c4fa5c18a to your computer and use it in GitHub Desktop.
simple timeline
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
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 events = [
{
'date': (new Date("2007-01-01")),
'text': 'This little piggy went to market'
},
{
'date': new Date("2007-02-04"),
'text': 'This little piggy stayed home'
},
{
'date': new Date('2007-04-05'),
'text': 'This little piggy went to Chile'
}];
var data = [1, 2, 3, 5, 8, 13];
var svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 500);
var x = d3.scaleTime(events)
.range([0, 480])
.domain(d3.extent(events, function(d){return d.date}))
.nice();
svg.selectAll('.dot')
.data(events).enter()
.append('circle')
.attr('class', 'dot')
.attr("r", 3.5)
.attr('cx', function(d){return x(d.date)})
.attr('cy', 400)
svg.selectAll('.text')
.data(events).enter()
.append('text')
.text(function(d) { return d.text})
.attr('x', function(d){return x(d.date)})
.attr('y', 350);
var xAxis = d3
.axisBottom()
.scale(x)
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0,480)")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", 450)
.attr("y", -6)
.style("text-anchor", "end")
.text("Sepal Width (cm)");
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment