Skip to content

Instantly share code, notes, and snippets.

@riccardoscalco
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardoscalco/3997cd32a7e7146a489d to your computer and use it in GitHub Desktop.
Save riccardoscalco/3997cd32a7e7146a489d to your computer and use it in GitHub Desktop.
Outset of a d3 chart
<div id="box">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
width = 100
height = 100
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 + ")"
svg.append "circle"
.attr
"cx": w / 2
"cy": h / 2
"r": w / 2
.style
"fill": "limegreen"
#box {
max-width: 600px;
}
svg {
border: 1px solid black;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment