Search

Custom Dragging

Interactions

Customise dragging and panning behaviour.

Custom Dragging
View live example →

The drag-start event and its properties let you customise the dragging behaviour.

By passing arguments to the setDragOptions function you can control various aspects of dragging including default draggers.

Use the active modifierKeys and mouse buttons to create custom draggers over the chart or its specific components.

Key functions used:

import KeyLines from "keylines";
import { data } from "./data.js";

let chart;

function dragStartHandler({ button, modifierKeys, setDragOptions }) {
  // Retrieve the selected option
  const selectedDragOption = document.querySelector('input[name="customDrag"]:checked').value;

  if (
    (selectedDragOption === "dragRightPan" && button === 2) ||
    (selectedDragOption === "dragCtrlPan" && modifierKeys.ctrl) ||
    selectedDragOption === "alwaysPan"
  ) {
    // Set the dragging options by returning a function here
    // with the desired dragging behaviour
    setDragOptions({ type: "pan" });
  }
}

async function startKeyLines() {
  const options = {
    logo: {
      u: "/images/Logo.png",
    },
    selectionColour: "#ff0000",
  };

  chart = await KeyLines.create({ container: "klchart", options });
  chart.load(data);
  chart.layout("organic");
  // Attach a handler to the drag-start event
  chart.on("drag-start", dragStartHandler);
}

window.addEventListener("DOMContentLoaded", startKeyLines);
export const data = {
  type: "LinkChart",
  items: [
    {
      type: "node",
      id: "Node 1",
      c: "#397af2",
    },
    {
      type: "node",
      id: "Node 2",
      c: "#397af2",
    },
    {
      type: "node",
      id: "Node 3",
      c: "#397af2",
    },
    {
      type: "node",
      id: "Node 4",
      c: "#397af2",
    },
    {
      type: "node",
      id: "Node 5",
      c: "#397af2",
    },
    {
      type: "node",
      id: "Node 6",
      c: "#397af2",
    },
    {
      type: "node",
      id: "Node 7",
      c: "#397af2",
    },
    {
      type: "link",
      id: "Node 1 - Node 2",
      id1: "Node 1",
      id2: "Node 2",
    },
    {
      type: "link",
      id: "Node 1 - Node 3",
      id1: "Node 1",
      id2: "Node 3",
    },
    {
      type: "link",
      id: "Node 1 - Node 4",
      id1: "Node 1",
      id2: "Node 4",
    },
    {
      type: "link",
      id: "Node 2 - Node 5",
      id1: "Node 2",
      id2: "Node 5",
    },
    {
      type: "link",
      id: "Node 2 - Node 6",
      id1: "Node 2",
      id2: "Node 6",
    },
    {
      type: "link",
      id: "Node 5 - Node 7",
      id1: "Node 5",
      id2: "Node 7",
    },
  ],
};
<!doctype html>
<html lang="en" style="background-color: #2d383f">
  <head>
    <meta charset="utf-8" />
    <title>Custom Dragging</title>
    <link rel="stylesheet" type="text/css" href="@ci/theme/kl/css/keylines.css" />
    <link rel="stylesheet" type="text/css" href="@ci/theme/kl/css/minimalsdk.css" />
    <link rel="stylesheet" type="text/css" href="@ci/theme/kl/css/sdk-layout.css" />
    <link rel="stylesheet" type="text/css" href="@ci/theme/kl/css/demo.css" />
  </head>
  <body>
    <div id="klchart" class="klchart"></div>
    <script type="module" src="./code.js"></script>
  </body>
</html>

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.