//
// Copyright © 2011-2025 Cambridge Intelligence Limited.
// All rights reserved.
//
// Sample Code
//
//! Understand the main concepts behind KeyLines annotations.
import KeyLines from "keylines";
import { data, itemsMap } from "./annotationsbasics-data.js";
let chart;
/* Formats the code snippets neatly in the display box on the right-hand side
if no annotation is provided, prompt the user to select an annotation */
function prettyPrint(item) {
const codeDisplayEl = document.getElementById("display");
if (item?.type === "annotation") {
const json = JSON.stringify(item, undefined, 1);
codeDisplayEl.textContent = json;
} else {
codeDisplayEl.textContent = "Click an annotation";
}
}
function initialiseInteractions() {
chart.on("click", ({ id, preventDefault }) => {
const item = itemsMap[id];
if (item) {
// Prevent item selection
preventDefault();
}
prettyPrint(item);
});
}
async function startKeyLines() {
const options = {
handMode: true,
backColour: "rgb(230, 234, 224)",
minZoom: 0.42,
navigation: false,
};
chart = await KeyLines.create({ container: "klchart", options });
await chart.load(data);
await chart.zoom("fit");
initialiseInteractions();
}
window.addEventListener("DOMContentLoaded", startKeyLines);
<!DOCTYPE html>
<html lang="en" style="background-color: #2d383f;">
<head>
<meta charset="UTF-8">
<title>Annotations Basics</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="/vendor/webfontloader.js" defer type="text/javascript"></script>
<script src="/annotationsbasics.js" crossorigin="use-credentials" defer 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%;">Annotations Basics</text>
</svg>
</div>
<div class="tab-content-panel" data-tab-group="rhs">
<div class="toggle-content is-visible tabcontent" id="controlsTab">
<form autocomplete="off" onsubmit="return false" id="rhsForm">
<div class="cicontent">
<p>Click any annotation to view its source code.</p>
<p>
Pan and zoom the chart and drag the nodes to see how annotations respond to changes.
</p>
<pre id="display" style="-webkit-user-select: text; -khtml-user-select: text; -moz-user-select: text; -ms-user-select: text; word-break: keep-all;">Click an annotation</pre>
</div>
</form>
</div>
</div>
</div>
</div>
<div id="moreParent">
<div id="moreContainer">
</div>
</div>
<div id="lazyScripts">
</div>
</body>
</html>
Loading source
