Skip to content

Instantly share code, notes, and snippets.

@ddonatien
Last active January 17, 2020 16:11
Show Gist options
  • Select an option

  • Save ddonatien/2fac7f1e43989752bcaed03152ea1ae5 to your computer and use it in GitHub Desktop.

Select an option

Save ddonatien/2fac7f1e43989752bcaed03152ea1ae5 to your computer and use it in GitHub Desktop.
Tuto 1.2
license: mit
[{"id" : 1, "name": "A", "value": 10, "date": "2016-01"}, {"id" : 2, "name": "B", "value": 30, "date": "2016-02"}, {"id" : 3, "name": "C", "value": 20, "date": "2016-03"} ]
<!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; }
</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 margin = {top: 120, right: 120, bottom: 120, left: 120};
var width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
d3.json("dataset.json", function(error, data) {
if (error) {
console.log(error)
}
else {
var timeParser = d3.timeParse("%Y-%m")
var xScale = d3.scaleTime()
.range([0, width])
.domain(d3.extent(data, function (d) { return timeParser(d.date) }))
var yScale = d3.scaleLinear()
.range([0, height])
.domain(d3.extent(data, function (d, i) { return d.value }))
svg.append('path')
.datum(data)
.attr('d', d3.line()
.x(function(d) { return margin.left + xScale(timeParser(d.date)) })
.y(function(d) { return margin.top + height - yScale(d.value)})
.curve(d3.curveCardinal))
.style('fill', 'none')
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
}
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment