Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container">
  <div id="paper" class="paper"></div>
  <p>Result of <code>joint.graphUtils.toAdjacencyList(graph)</code>:</p>
  <div id="adjacencyList"></div>
  <p>
    <button id="generate">Generate a new random diagram</button>
  </p>
</div>

<a target="_blank" href="https://www.jointjs.com">
  <img id="logo" src="https://assets.codepen.io/7589991/jointjs-logo.svg" width="200" height="50"></img>
</a>
              
            
!

CSS

              
                body {
  font-family: Arial, sans-serif;
  color: #131e29;
  background: #dde6ed;
}

code {
  padding-left: 3px;
  padding-right: 3px;
}

button {
  color: #131e29;
  background: #ed2637;
  font-weight: bold;
  border: none;
  cursor: pointer;
  padding: 10px 20px;
}

.joint-type-chart-matrix .columns .label {
  transform: rotate(0deg) translate(-7px, -7px);
}

#container {
  width: 380px;
  margin: 0 auto;
  text-align: center;
}

#paper {
  border: 1px solid #a8b1be;
  margin: 30px auto;
}

#adjacencyList {
  font-size: 1.1rem;
  letter-spacing: 0.1rem;
  text-align: center;
}

#generate {
  margin: 30px;
}

#logo {
  position: absolute;
  bottom: 20px;
  right: 0;
}
              
            
!

JS

              
                const width = 380;
const height = 380;

const nodesIds = ["a", "b", "c", "d", "e"];

const colorRed = "#ed2637";
const colorLightGrey = "#dde6ed";
const colorDarkBlue = "#131e29";

document.getElementById("container").style.width = width + "px";

const createNode = (id) =>
  new joint.shapes.standard.Circle({
    id,
    size: { width: 40, height: 40 },
    z: 2,
    attrs: {
      body: {
        fill: colorLightGrey,
        stroke: colorRed,
        "stroke-width": 3
      },
      label: {
        text: id,
        style: { textTransform: "capitalize" }
      }
    }
  });

const createLink = (sourceId, targetId) =>
  new joint.shapes.standard.Link({
    source: {
      id: sourceId
    },
    target: {
      id: targetId
    },
    z: 3,
    attrs: {
      line: {
        stroke: colorRed
      }
    }
  });

const createNodesAndLinks = (nodesIds, graph) => {
  nodesIds.forEach((id) => {
    createNode(id).addTo(graph);
  });

  nodesIds.forEach((id) => {
    const possibleTargets = nodesIds.filter(function (value) {
      return value !== id;
    });

    for (let i = 0; i <= g.random(3); i++) {
      const linkedId =
        possibleTargets[Math.floor(Math.random() * (nodesIds.length - 1))];
      createLink(id, linkedId).addTo(graph);
    }
  });
};

const createGraphLayout = (graph) => {
  const graphLayout = new joint.layout.ForceDirected({
    graph: graph,
    x: 0,
    y: 0,
    width: width,
    height: height,
    gravityCenter: { x: width / 2 - 100, y: height / 2 - 60 },
    charge: 800,
    linkDistance: 130
  });

  graphLayout.start();

  Array.from({ length: 100 }).forEach(function () {
    graphLayout.step();
  });
};

const createAdjacencyMatrix = (graph, nodesIds, adjacencyList) => {
  const cells = nodesIds.map((id) =>
    nodesIds.map((nestedId) => ({
      fill: adjacencyList[id].includes(nestedId) ? colorRed : colorDarkBlue
    }))
  );

  const labels = nodesIds.map((id) => ({
    text: id.toUpperCase(),
    fill: colorDarkBlue
  }));

  const matrixSize = 100;

  const matrix = new joint.shapes.chart.Matrix({
    position: { x: width - matrixSize - 10, y: height - matrixSize - 10 },
    size: { width: matrixSize, height: matrixSize },
    z: 1,
    attrs: {
      ".grid-line": { stroke: colorLightGrey }
    },
    cells: cells,
    labels: {
      rows: labels,
      columns: labels
    }
  });

  matrix.addTo(graph);
};

const generateSampleDiagram = (graph, nodesIds, adjacencyListElement) => {
  graph.clear();

  createNodesAndLinks(nodesIds, graph);
  createGraphLayout(graph);

  const adjacencyList = joint.graphUtils.toAdjacencyList(graph);

  createAdjacencyMatrix(graph, nodesIds, adjacencyList);

  adjacencyListElement.innerText = JSON.stringify(adjacencyList);
};

const graph = new joint.dia.Graph();

const paper = new joint.dia.Paper({
  el: document.getElementById("paper"),
  width: width,
  height: height,
  model: graph,
  overflow: true,
  defaultConnectionPoint: { name: "boundary", args: { offset: 4 } },
  background: {
    color: colorLightGrey
  },
  interactive: function (cellView) {
    if (cellView.model.get("type") === "chart.Matrix") {
      return {
        elementMove: false
      };
    }

    return true;
  }
});

const adjacencyListElement = document.getElementById("adjacencyList");

generateSampleDiagram(graph, nodesIds, adjacencyListElement);

document
  .getElementById("generate")
  .addEventListener("click", () =>
    generateSampleDiagram(graph, nodesIds, adjacencyListElement)
  );

              
            
!
999px

Console