Skip to content

Instantly share code, notes, and snippets.

@darlacameron
Last active March 2, 2017 01:44
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 darlacameron/96a194ab7b5a91b142f2f0970216eb13 to your computer and use it in GitHub Desktop.
Save darlacameron/96a194ab7b5a91b142f2f0970216eb13 to your computer and use it in GitHub Desktop.
1. DOM Manipulation
license: gpl-3.0
height: 240
border: no

Demonstration of DOM manipulation using D3

For NICAR Beginning D3, 2017

<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>DOM Manipulation</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
}
circle {
fill: red;
stroke: black;
stroke-width: 2;
}
</style>
<body>
<svg width="500" height="300">
<circle cx="50" cy="50" r="20" id="first-circle" />
</svg>
<script src="http://d3js.org/d3.v4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
// ADD A CIRCLE WITH D3
// d3.select("svg").append("circle")
// .attr("id", "second-circle")
// .attr("cx", 150)
// .attr("cy", 50)
// .attr("r", 20);
// AND ANOTHER!
// d3.select("svg").append("circle")
// .attr("id", "third-circle")
// .attr("cx", 250)
// .attr("cy", 50)
// .attr("r", 30);
// DEMO OF HOW D3 COMPARES TO JAVASCRIPT AND JQUERY
// console.debug('javascript ', document.getElementById("first-circle"))
// console.debug('jquery ', $("#first-circle")[0])
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment