Simple example using D3 to generate content in a webpage
Using D3 to generate webpage content (Data: randomly generated)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit | |
border: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>D3 example</title> | |
<script src="//d3js.org/d3.v4.min.js" charset="utf-8"></script> | |
</head> | |
<body> | |
<input type="button" onclick="update()" value="Update"/> | |
<div id="vis"> | |
</div> | |
<script src="script.js"> | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createRandomData() { | |
var numDataItems = Math.floor((Math.random() * 10) + 1); | |
var d = []; | |
for (var i = 0; i < numDataItems; i++) { | |
d.push(Math.floor((Math.random() * 50) + 1)); | |
} | |
return d; | |
} | |
function update() { | |
var exampleData = createRandomData(); | |
var visElement = d3.select('#vis'); | |
var paras = visElement.selectAll('p') | |
.data(exampleData); | |
paras | |
.exit() | |
.remove(); | |
var new_paras = paras | |
.enter() | |
.append('p'); | |
new_paras.merge(paras) | |
.text(function(d, i) { | |
return "this is paragraph " + i + " and the data value is " + d; | |
}); | |
} | |
update(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment