data-driven documents
{
"data": [
{
"id": "X999_Y999",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2010!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
}
]
}
{
"red":"#f00",
"green":"#0f0",
"blue":"#00f",
"cyan":"#0ff",
"magenta":"#f0f",
"yellow":"#ff0",
"black":"#000"
}
{
"colorsArray":[{
"red":"#f00",
"green":"#0f0",
"blue":"#00f",
"cyan":"#0ff",
"magenta":"#f0f",
"yellow":"#ff0",
"black":"#000"
}
]
}
Getting data from another file into an application
// JSON method
d3.json('path-to-file.json', function(error, data){
});
// CSV method
d3.csv('path-to-file.csv', function(error, data){
});
// HTML method
d3.html('path-to-file.html', function(error, data){
});
// text method
d3.text('path-to-file.txt', function(error, data){
});
// TSV method
d3.tsv('path-to-file.tsv', function(error, data){
});
// XML method
d3.xml('path-to-file.xml', function(error, data){
});
// D3 JSON method
d3.json('path-to-file.json', function(error, data){
if ( error ) {
// handle the error
}
// do something with the loaded data
});
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<ul>
<li>Lorem ipsum</li>
<li>Lorem ipsum</li>
<li>Lorem ipsum</li>
<li>Lorem ipsum</li>
</ul>
</body>
</html>
// Data to be bind
var dummyData = [2, 30, 45, 11];
// Select all <li>
d3.selectAll('li')
// Bind data from Array with the data method
.data(dummyData)
// Pass the data to <li> and concat px to the data to change styling
.style('margin-left', function(data) {
return data + 'px';
});
// Returns the first element matching the CSS selector given.
d3.select('div')
d3.select('#name')
// Returns a selection in an array that contains
// all elements matching the CSS selector given.
d3.selectAll('div')
d3.selectAll('#name')
// method
.attr()
// Needs 2 values: attribute that needs to be changed and the new value
selection.attr("attribute string", new value);
// Example
circle.attr("r", 55);
// method
.style()
// Needs 2 values: CSS style that needs to be changed and the new value
selection.style("CSS Style string", new value);
// Example
circle.style("fill", "#FF0000");
circle.style("stroke", "#000000");
// Select an element and chain transition to it
d3.select('div').transition();
// Select a div element and give it the ease transition
d3.select('div')
.transition()
.ease('bounce');