D3
Combines low-level data, layout, and DOM primitives for custom graph visualizations.
- Downloads / mo
- 125.9M
- Stars
- 114.0K
- Last release
- 7.9.2
- License
- ISC
Strengths
- Controls graph rendering directly. Compose SVG or Canvas graphics instead of adopting a fixed graph interface.
- Computes layouts separately from drawing. Run force simulations over node objects and draw the positions in application code.
Limitations
- Graph interfaces require composition. Build rendering and interaction from primitives rather than a ready-made graph widget.
- Force simulation mutates input nodes. Copy node objects when the application must preserve their original values.
Quickstart
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm"; // Declare the chart dimensions and margins. const width = 640; const height = 400; const marginTop = 20; const marginRight = 20; const marginBottom = 30; const marginLeft = 40; // Declare the x (horizontal position) scale. const x = d3.scaleUtc() .domain([new Date("2023-01-01"), new Date("2024-01-01")]) .range([marginLeft, width - marginRight]); // Declare the y (vertical position) scale. const y = d3.scaleLinear() .domain([0, 100]) .range([height - marginBottom, marginTop]); // Create the SVG container. const svg = d3.create("svg") .attr("width", width) .attr("height", height); // Add the x-axis. svg.append("g") .attr("transform", `translate(0,${height - marginBottom})`) .call(d3.axisBottom(x)); // Add the y-axis. svg.append("g") .attr("transform", `translate(${marginLeft},0)`) .call(d3.axisLeft(y)); // Append the SVG element. container.append(svg.node());
D3 examples
export function createLayoutElementGroups( element: D3Selection, { edgePathsClass = 'edges edgePaths' }: CreateLayoutElementGroupsOptions = {} ): LayoutElementGroups { const rootGroups = element.insert('g').attr('class', 'root'); const clusters = rootGroups.insert('g').attr('class', 'clusters'); const edgePaths = rootGroups.insert('g').attr('class', edgePathsClass); const edgeLabels = rootGroups.insert('g').attr('class', 'edgeLabels'); const nodes = rootGroups.insert('g').attr('class', 'nodes'); return { clusters, edgePaths, edgeLabels, nodes, rootGroups }; }