OKdevTV Docs
Home OKdevTV
ESC
d3js 1 min read 1.0 KB Raw Markdown
On this page (6)

D3 js

text
<script src="https://d3js.org/d3.v4.min.js"></script>

Selections

  • example
    • legacy
text
var paragraphs = document.getElementsByTagName("p");
for (var i = 0; i < paragraphs.length; i++) {
  var paragraph = paragraphs.item(i);
  paragraph.style.setProperty("color", "white", null);
}
  • d3 selector
text
d3.selectAll("p").style("color", "white");
  • or another selector
text
d3.select("body").style("background-color", "black");

Dynamic Properties

text
d3.selectAll("p").style("color", function() {
  return "hsl(" + Math.random() * 360 + ",100%,50%)";
});
text
d3.selectAll("p").style("color", function(d, i) {
  return i % 2 ? "#fff" : "#eee";
});
text
d3.selectAll("p")
  .data([4, 8, 15, 16, 23, 42])
    .style("font-size", function(d) { return d + "px"; });

Enter and Exit

Transformation, not Representation

Transitions

ref