Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@curran
Last active August 29, 2015 14:27
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 curran/db1e524cae5e4344b2e6 to your computer and use it in GitHub Desktop.
Save curran/db1e524cae5e4344b2e6 to your computer and use it in GitHub Desktop.
Dynamic Size
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic Size Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<style>
/* Make the chart container fill the page using CSS. */
#chart {
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
}
</style>
</head>
<body>
<div id="chart"></div>
<script>
// Extract the width and height that was computed by CSS.
var chartDiv = document.getElementById("chart");
var width = chartDiv.clientWidth;
var height = chartDiv.clientHeight;
// Use the extracted size to set the size of an SVG element.
var svg = d3.select(chartDiv).append("svg")
.attr("width", width)
.attr("height", height);
// Draw an X to show that the size is correct.
var lines = svg.selectAll("line").data([
{x1: 0, y1: 0, x2: width, y2: height},
{x1: 0, y1: height, x2: width, y2: 0}
]);
lines.enter().append("line");
lines
.attr("x1", function (d) { return d.x1; })
.attr("y1", function (d) { return d.y1; })
.attr("x2", function (d) { return d.x2; })
.attr("y2", function (d) { return d.y2; })
.style("stroke-width", 50)
.style("stroke-opacity", 0.4)
.style("stroke", "black");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment