I have an array of elements which I want to place around a central element. Think of a graph where every node is connected to a central element. How do I achieve this in LWC? I haven't come across any custom LWC element yet that lets me visualize data as mentioned. If this can't be achieved natively, does any one know of any third party library that helps do the same? Please see the attached picture to better understand what I mean.

enter image description here

  • I don't know if there is a component that satisfies your needs, but you can take a look at this library, and its network graph type: d3-graph-gallery.com/graph/network_basic.html I have never used it, but looks like you only need to import a js file, and that's it. hope it helps.
    – McCubo
    Commented Jun 1, 2022 at 7:31

1 Answer 1


Agreed with McCubo's comment, D3.js seems right. Salesforce has documentation on loading 3rd party libraries which D3.js is in the example: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/js_third_party_library

Below is just the HTML & JS in their examples but is how I'd start. Once I got the example running, I'd take out unneeded code, like dragged, etc if users don't need to move nodes, then tie in my data instead.

Hope it helps.


    <div class="slds-m-around_medium">


/* global d3 */
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import D3 from '@salesforce/resourceUrl/d3';
import DATA from './data';

export default class LibsD3 extends LightningElement {
    svgWidth = 400;
    svgHeight = 400;

    d3Initialized = false;

    renderedCallback() {
        if (this.d3Initialized) {
        this.d3Initialized = true;

            loadScript(this, D3 + '/d3.v5.min.js'),
            loadStyle(this, D3 + '/style.css')
            .then(() => {
            .catch(error => {
                    new ShowToastEvent({
                        title: 'Error loading D3',
                        message: error.message,
                        variant: 'error'

    initializeD3() {
        // Example adopted from https://bl.ocks.org/mbostock/2675ff61ea5e063ede2b5d63c08020c7
        const svg = d3.select(this.template.querySelector('svg.d3'));
        const width = this.svgWidth;
        const height = this.svgHeight;
        const color = d3.scaleOrdinal(d3.schemeDark2);

        const simulation = d3
                d3.forceLink().id(d => {
                    return d.id;
            .force('charge', d3.forceManyBody())
            .force('center', d3.forceCenter(width / 2, height / 2));

        const link = svg
            .attr('class', 'links')
            .attr('stroke-width', d => {
                return Math.sqrt(d.value);

        const node = svg
            .attr('class', 'nodes')
            .attr('r', 5)
            .attr('fill', d => {
                return color(d.group);
                    .on('start', dragstarted)
                    .on('drag', dragged)
                    .on('end', dragended),

        node.append('title').text(d => {
            return d.id;

        simulation.nodes(DATA.nodes).on('tick', ticked);


        function ticked() {
            link.attr('x1', d => d.source.x)
                .attr('y1', d => d.source.y)
                .attr('x2', d => d.target.x)
                .attr('y2', d => d.target.y);
            node.attr('cx', d => d.x).attr('cy', d => d.y);

        function dragstarted(d) {
            if (!d3.event.active) {
            d.fx = d.x;
            d.fy = d.y;

        function dragged(d) {
            d.fx = d3.event.x;
            d.fy = d3.event.y;

        function dragended(d) {
            if (!d3.event.active) {
            d.fx = null;
            d.fy = null;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .