9951 explained code solutions for 126 technologies


javascript-d3How do I create a UML diagram using D3.js?


Creating a UML diagram using D3.js requires a few steps. First, create a div element in the HTML document to contain the diagram. Then, create a JavaScript object that will contain the data for the diagram. This data should include the nodes and links of the diagram. Next, create a D3.js selection with the div element and pass the data object to the selection. Finally, create a force layout and add the nodes and links to the layout.

Example code

// Create a div element to contain the diagram
let div = d3.select('body').append('div')

// Create the data object for the diagram
let data = {
  nodes: [
    {name: 'Node1'},
    {name: 'Node2'},
    {name: 'Node3'},
  ],
  links: [
    {source: 0, target: 1},
    {source: 1, target: 2},
  ]
}

// Create a D3.js selection with the div element
let svg = div.append('svg')

// Pass the data object to the selection
let link = svg.selectAll('link')
  .data(data.links)
  .enter().append('line')
  .attr('stroke', '#ccc')
  .attr('stroke-width', 1)

// Create a force layout
let simulation = d3.forceSimulation()
  .force('link', d3.forceLink().id(d => d.name))
  .force('charge', d3.forceManyBody())
  .force('center', d3.forceCenter())

// Add the nodes and links to the layout
simulation.nodes(data.nodes).on('tick', ticked)
simulation.force('link').links(data.links)

No output

Code explanation

  • Create a div element in the HTML document to contain the diagram (d3.select('body').append('div'))
  • Create a JavaScript object that will contain the data for the diagram (nodes and links)
  • Create a D3.js selection with the div element (d3.select('div'))
  • Pass the data object to the selection (svg.selectAll('link').data(data.links))
  • Create a force layout (d3.forceSimulation())
  • Add the nodes and links to the layout (simulation.nodes(data.nodes) and simulation.force('link').links(data.links))

Helpful links

Edit this code on GitHub