Code

D3.js

Line Chart

Intermediate Multi-series line chart using scaleTime, d3.line() path generator, area fill, and curveCatmullRom smoothing. d3-shape/line

const x = d3.scaleTime()
  .domain(d3.extent(data, d => d.date))
  .range([0, innerWidth]);

const y = d3.scaleLinear()
  .domain([0, 90])
  .range([innerHeight, 0]);

// Line generator
const line = d3.line()
  .x(d => x(d.date))
  .y(d => y(d.value))
  .curve(d3.curveCatmullRom);

// Area fill under the line
const area = d3.area()
  .x(d => x(d.date))
  .y0(innerHeight)
  .y1(d => y(d.value))
  .curve(d3.curveCatmullRom);

g.append('path').datum(data)
  .attr('fill', '#4f46e520')
  .attr('d', area);

g.append('path').datum(data)
  .attr('fill', 'none')
  .attr('stroke', '#4f46e5')
  .attr('stroke-width', 2.5)
  .attr('d', line);
Key APIs
  • d3.scaleTime() — maps Date objects to pixel positions
  • d3.line() — path generator for line charts
  • d3.area() — path generator with y0 baseline for area fill
  • d3.curveCatmullRom — smooth curve interpolation
  • d3.extent(array, accessor) — [min, max] helper
  • d3.timeParse(format) / d3.timeFormat(format) — date parsing & formatting