Skip to content

Instantly share code, notes, and snippets.

@dimitardanailov
Last active January 4, 2017 17:08
Show Gist options
  • Save dimitardanailov/ab56ce7f14d30379544ee6d56a42928d to your computer and use it in GitHub Desktop.
Save dimitardanailov/ab56ce7f14d30379544ee6d56a42928d to your computer and use it in GitHub Desktop.
Lines
license: gpl-3.0

d3 and svg lines

<!DOCTYPE html>
<meta charset="utf-8">
<style type="text/css">
svg {
font-family: "Helvetica Neue", Helvetica;
}
line {
shape-rendering: crispEdges;
vector-effect: non-scaling-stroke;
stroke: #000;
stroke-width: 2px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var svg = d3.select('body').append('svg');
svg.attr('width', width);
svg.attr('height', height);
//The data for our lines
var data = [
{
'x1': 350,
'y1': 15,
'x2': 250,
'y2': 33
},
{
'x1': 420,
'y1': 420,
'x2': 250,
'y2': 333
},
{
'x1': 140,
'y1': 120,
'x2': 220,
'y2': 220
},
{
'x1': 180,
'y1': 55,
'x2': 330,
'y2': 320
},
{
'x1': 200,
'y1': 260,
'x2': 150,
'y2': 180
}
];
// Generating the svg lines attributes
var lineAttributes = {
'x1': function(d) {
return d.x1;
},
'y1': function(d) {
return d.y1;
},
'x2': function(d) {
return d.x2;
},
'y2': function(d) {
return d.y2;
}
};
// Pointer to the d3 lines
var lines = svg
.selectAll('line')
.data(data)
.enter()
.append('line')
.attr(lineAttributes);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment