D3.js
Data-Driven Documents — the powerful library for binding data to SVG/DOM and creating custom.
01D3.js Introduction & Data BindingWhat is D3.js? D3.js (Data-Driven Documents) by Mike Bostock lets you bind arbitrary data to a DOM and apply. <script src="https://d3js.org/d3.v7.min.js"></script> // or: npm install d3codequizbeginner
What is D3.js? D3.js (Data-Driven Documents) by Mike Bostock lets you bind arbitrary data to a DOM and apply. <script src="https://d3js.org/d3.v7.min.js"></script> // or: npm install d3
<script src="https://d3js.org/d3.v7.min.js"></script>
// or: npm install d3D3.js is primarily a:
What does the .data() method do in D3?
In D3, .enter() returns:
02Scales & AxesD3 Scales & Axes Scales map data values to visual values (pixels, colors). Axes render scale markings on SVG. Scale Types // Linear: continuous numeric input → output const xScale = d3.scaleLinear() .domain([0, 100]) // input range (data) .range([0, codequizbeginner
D3 Scales & Axes Scales map data values to visual values (pixels, colors). Axes render scale markings on SVG. Scale Types // Linear: continuous numeric input → output const xScale = d3.scaleLinear() .domain([0, 100]) // input range (data) .range([0,
// Linear: continuous numeric input → output
const xScale = d3.scaleLinear()
.domain([0, 100]) // input range (data)
.range([0, 500]); // output range (pixels)
xScale(50); // → 250px
// Band: discrete categories → widths
const xBand = d3.scaleBand()
.domain(['A', 'B', 'C', 'D'])
.range([0, 400])
.padding(0.2);
xBand('B'); // → x position
xBand.bandwidth(); // → bar width
// Time: date input
const timeScale = d3.scaleTime()
.domain([new Date('2024-01-01'), new Date('2024-12-31')])
.range([0, 800]);
// Color scales
const colorScale = d3.scaleSequential(d3.interpolateBlues).domain([0, 100]);
colorScale(50); // → 'rgb(...)' colorWhich D3 scale is used for mapping discrete category names to bar positions?
What does scale.domain() set?
Which D3 axis renders at the bottom of the chart?
03Building a Bar ChartComplete D3 Bar Chart const data = [ { month: 'Jan', value: 120 }, { month: 'Feb', value: 150 }, { month: 'Mar', value: 90 }, { month: 'Apr', value: 200 } ]; const margin = { top: 20, right: 20, bottom: 40, left: 60 }; const width = 600 - margin.leftcodequizintermediate
Complete D3 Bar Chart const data = [ { month: 'Jan', value: 120 }, { month: 'Feb', value: 150 }, { month: 'Mar', value: 90 }, { month: 'Apr', value: 200 } ]; const margin = { top: 20, right: 20, bottom: 40, left: 60 }; const width = 600 - margin.left
const data = [
{ month: 'Jan', value: 120 },
{ month: 'Feb', value: 150 },
{ month: 'Mar', value: 90 },
{ month: 'Apr', value: 200 }
];
const margin = { top: 20, right: 20, bottom: 40, left: 60 };
const width = 600 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
// Create SVG
const svg = d3.select('#chart')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// Scales
const x = d3.scaleBand()
.domain(data.map(d => d.month))
.range([0, width]).padding(0.3);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.nice()
.range([height, 0]);
// Axes
svg.append('g').attr('transform', `translate(0,${height})`).call(d3.axisBottom(x));
svg.append('g').call(d3.axisLeft(y));
// Bars
svg.selectAll('.bar')
.data(data)
.enter().append('rect')
.attr('class', 'bar')
.attr('x', d => x(d.month))
.attr('y', d => y(d.value))
.attr('width', x.bandwidth())
.attr('height', d => height - y(d.value))
.attr('fill', 'steelblue')
.on('mouseover', function(event, d) {
d3.select(this).attr('fill', 'orange');
tooltip.style('opacity', 1).html(`${d.month}: ${d.value}`);
});What does .nice() do on a D3 scale?
The margin convention in D3 is used to:
d3.max(data, d => d.value) returns: