Skip to content

Instantly share code, notes, and snippets.

@basilesimon
Last active August 23, 2019 13:23
Show Gist options
  • Save basilesimon/8868abff7f376f1862ef990d8aefeeab to your computer and use it in GitHub Desktop.
Save basilesimon/8868abff7f376f1862ef990d8aefeeab to your computer and use it in GitHub Desktop.
Arrowheads at each end of axis
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-jetpack"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
const svg = d3
.select('body')
.append('svg')
.attr('width', 500)
.attr('height', 500);
// Arrowhead defs
svg.append('defs')
.append('marker')
.at({
id: 'arrowhead-right',
refX: 5,
refY: 5,
markerWidth: 16,
markerHeight: 13,
})
.append('path')
.at({
d: 'M 0 0 L 5 5 L 0 10',
stroke: 'black',
strokeWidth: 1,
fill: 'none',
});
svg.append('defs')
.append('marker')
.at({
id: 'arrowhead-left',
refX: 0,
refY: 5,
markerWidth: 16,
markerHeight: 13,
})
.append('path')
.at({
d: 'M 5 0 L 0 5 L 5 10',
stroke: 'black',
strokeWidth: 1,
fill: 'none',
});
const x = d3
.scaleLinear()
.range([0, 400])
.domain([0, 10]);
const xa = svg
.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(50,250)')
.call(d3.axisBottom(x)
.tickSizeInner(0)
.tickPadding(6)
.tickSizeOuter(0));
// append the markers at the start and end of our axis path
xa.select('path').at({ markerEnd: 'url(#arrowhead-right)' });
xa.select('path').at({ markerStart: 'url(#arrowhead-left)' });
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment