//
// Copyright © 2011-2025 Cambridge Intelligence Limited.
// All rights reserved.
//
// Sample Code
//
//! Customise dragging and panning behaviour.
import KeyLines from "keylines";
import { data } from "./customdrag-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);
<!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="/css/keylines.css">
<link rel="stylesheet" type="text/css" href="/css/minimalsdk.css">
<link rel="stylesheet" type="text/css" href="/css/sdk-layout.css">
<link rel="stylesheet" type="text/css" href="/css/demo.css">
<script src="/vendor/jquery.js" defer type="text/javascript"></script>
<script id="keylines" src="/public/keylines.umd.cjs" defer type="text/javascript"></script>
<script src="/customdrag.js" crossorigin="use-credentials" type="module"></script>
</head>
<body>
<div class="chart-wrapper demo-cols">
<div class="tab-content-panel flex1" id="lhs" data-tab-group="rhs">
<div class="toggle-content is-visible" id="lhsVisual" style="width:100%; height: 100%;">
<div class="klchart" id="klchart">
</div>
</div>
</div>
<div class="rhs citext closed" id="demorhscontent">
<div class="title-bar">
<svg viewBox="0 0 360 60" style="max-width: 360px; max-height: 60px; font-size: 24px; font-family: Raleway; flex: 1;">
<text x="0" y="38" style="width: 100%;">Custom Dragging</text>
</svg>
</div>
<div class="tab-content-panel" data-tab-group="rhs">
<div class="toggle-content is-visible tabcontent" id="controlsTab">
<p>Select the dragging and panning behaviour.</p>
<form autocomplete="off" onsubmit="return false" id="rhsForm">
<div class="cicontent">
<fieldset>
<label class="radio">
<input type="radio" name="customDrag" value="default" checked="checked"> Default (left-click drag)
</label>
<label class="radio">
<input type="radio" name="customDrag" value="dragRightPan"> Left-click drag, right-click pan
</label>
<label class="radio">
<input type="radio" name="customDrag" value="dragCtrlPan"> Left-click drag, left-click + <strong>CTRL</strong> key pan
</label>
<label class="radio">
<input type="radio" name="customDrag" value="alwaysPan"> Left-click pan, right-click pan, no drag
</label>
</fieldset>
</div>
</form>
</div>
</div>
</div>
</div>
<div id="moreParent">
<div id="moreContainer">
</div>
</div>
<div id="lazyScripts">
</div>
</body>
</html>
Loading source
