Skip to content

Instantly share code, notes, and snippets.

@riccardoscalco
Last active August 29, 2015 14:22
Show Gist options
  • Save riccardoscalco/c7cf434cb8afbb0df51b to your computer and use it in GitHub Desktop.
Save riccardoscalco/c7cf434cb8afbb0df51b to your computer and use it in GitHub Desktop.
Edges of a network
<div id="box">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
width = 500
height = 1000
m =
top: 10
bottom: 10
right: 10
left: 10
w = width - m.left - m.right
h = height - m.top - m.bottom
# https://css-tricks.com/scale-svg/
# padding-bottom Hack on an Inline <svg> Element
svg = d3.select "#box"
.append "svg"
.attr
"viewBox": "0 0 " + width + " " + height
"preserveAspectRatio": "xMidYMin slice"
"width": "100%"
.style
"padding-bottom": (100 * height / width) + "%"
"height": "1px"
"overflow": "visible"
.append "g"
.attr "transform", "translate(" + m.left + "," + m.top + ")"
q = 100
rand = (max,min=0) -> return Math.floor(Math.random() * (max - min) + min)
data = ([rand(q), rand(q)] for i in [1..400])
y = d3.scale.linear()
.domain [0, q]
.range [0, h]
svg.selectAll ".links"
.data data
.enter()
.append "line"
.attr
"x1": 0
"x2": w
"y1": (d) -> y d[0]
"y2": (d) -> y d[1]
"class": "links"
.on "mouseover", () ->
d3.select this
.classed "focus", true
.on "mouseout", () ->
d3.select this
.classed "focus", false
html, body {
width: 100%;
background-color: #3e3e3e;
}
#box {
margin: auto;
max-width: 400px;
}
.links {
stroke: #ab3e41;
stroke-width: 1px;
}
.focus {
stroke-width: 3px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment