Skip to content

Instantly share code, notes, and snippets.

@marijer
Created July 20, 2017 09:26
Show Gist options
  • Save marijer/8aeabe3bc9c70bfc8f6f654fd98b7aa6 to your computer and use it in GitHub Desktop.
Save marijer/8aeabe3bc9c70bfc8f6f654fd98b7aa6 to your computer and use it in GitHub Desktop.
linechart
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
path {
stroke: #000;
stroke-width: 1px;
fill: none;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var data = [
{date: new Date(2017, 3, 24), value: 0},
{date: new Date(2017, 3, 25), value: 9},
{date: new Date(2017, 3, 26), value: 25},
{date: new Date(2017, 3, 27), value: 50},
{date: new Date(2017, 3, 28), value: 12}
];
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
var xExtent = d3.extent(data, d => d.date);
var yExtent = d3.extent(data, d => d.value);
var xScale = d3.scaleLinear().domain(xExtent).range([0, 400]);
var yScale = d3.scaleLinear().domain(yExtent).range([200, 0]);
var line = d3.line()
.x((d) => { return xScale(d.date)})
.y((d) => { return yScale(d.value)})
svg.append('path')
.attr('d', line(data));
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment