Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active February 19, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save d3indepth/5df095dd1604b08541ac6bd62adce3a0 to your computer and use it in GitHub Desktop.
Save d3indepth/5df095dd1604b08541ac6bd62adce3a0 to your computer and use it in GitHub Desktop.
Line generator (accessor functions)
license: gpl-3.0
height: 170
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Line with scales</title>
</head>
<style>
path {
fill: none;
stroke: #999;
}
</style>
<body>
<svg width="700" height="160">
<g transform="translate(20, 0)"></g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var xScale = d3.scaleLinear().domain([0, 6]).range([0, 600]);
var yScale = d3.scaleLinear().domain([0, 80]).range([150, 0]);
var lineGenerator = d3.line()
.x(function(d, i) {
return xScale(i);
})
.y(function(d) {
return yScale(d.value);
});
var data = [
{value: 10},
{value: 50},
{value: 30},
{value: 40},
{value: 20},
{value: 70},
{value: 50}
];
var line = lineGenerator(data);
// Create a path element and set its d attribute
d3.select('g')
.append('path')
.attr('d', line);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment