On Github sksizer / Tutorial-D3
<div id='BasicSelectExample'>BasicSelectExample Div</div>
d3.select("#BasicSelectExample")
.append("p")
.text("Some new text . . .");
<div class='BasicSelectAllExample'>BasicSelectionExample Div</div>
<div class='BasicSelectAllExample'>BasicSelectionExample Div</div>
<div class='BasicSelectAllExample'>BasicSelectionExample Div</div>
d3.selectAll(".BasicSelectAllExample")
.append("p")
.text("Some new text . . .");
<ul id="BasicDataBindingExample">Data Binding List</ul>
var data = [5,10,0,25];
d3.select('#BasicDataBindingExample')
.selectAll("li")
.data(data)
.enter()
.append('li')
.text('Data entry');
Super secret __data__ attribute
<ul id="WhySelectAndAppendExample">#WhySelectAndAppendExample</ul>
var data = [5,10,0,25];
d3.select('#WhySelectAndAppendExample')
.selectAll("p")
.data(data)
.enter()
.append('li')
.text('Data entry');
<ul id="DataFunctionExample">Data Binding List</ul>
var data = [5,10,0,25];
d3.select('#DataFunctionExample')
.selectAll("li")
.data(data)
.enter()
.append('li')
.text(function(value, position) {
return 'Value: ' + value + ' at the ' + position + ' position';
});
Most methods accept a value or a function
<div id="CreateSVGHost"></div>
var svg = d3.select('#CreateSVGHost')
.append('svg')
.attr('width', 500)
.attr('height', 300)
.style('background-color', 'white');
svg.append('rect')
.attr('width', 100)
.attr('height', 100)
.attr('x', 100)
.attr('y', 100);
};
var CreateSVGViz = (function() {
var data = [5,10,3,25];
var maxValue = data.reduce(function(prevValue, currValue) {
return prevValue > currValue ? prevValue : currValue;
}, 0);
var svgHeight = 300;
var svgWidth = 500;
function heightFunction(value) {
return(svgHeight * value/maxValue);
}
function widthFunction(value) {
return(svgWidth / data.length - 4);
}
function yPosition(value) {
return(svgHeight - heightFunction(value));
}
function xPosition(value, position) {
return(svgWidth / data.length * position);
}
return function() {
var svg = d3.select('#CreateSVGViz')
.append('svg')
.attr('width', svgWidth)
.attr('height', svgHeight);
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('x', xPosition)
.attr('y', yPosition)
.attr('height', heightFunction)
.attr('width', widthFunction)
.attr('fill', 'red')
.attr('stroke', 'white');
}
})();