diff options
author | johannst <johannst@users.noreply.github.com> | 2022-11-13 14:13:35 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2022-11-13 14:13:35 +0000 |
commit | 2dfbc312e6ccb88f838170d8e777d48aacde2ff5 (patch) | |
tree | a4a83435e10a7ad38f454a30cbd7b30640cd1a7d /web/src | |
parent | ccbbcafbc7337c8d7a785b9034716618dce36e4d (diff) | |
download | notes-2dfbc312e6ccb88f838170d8e777d48aacde2ff5.tar.gz notes-2dfbc312e6ccb88f838170d8e777d48aacde2ff5.zip |
deploy: 026d679006e5d470bacdc74bb3082072edf31e36
Diffstat (limited to 'web/src')
-rw-r--r-- | web/src/chartjs-ext-tooltip.html | 101 | ||||
-rw-r--r-- | web/src/tabs.html | 30 |
2 files changed, 131 insertions, 0 deletions
diff --git a/web/src/chartjs-ext-tooltip.html b/web/src/chartjs-ext-tooltip.html new file mode 100644 index 0000000..b7358bb --- /dev/null +++ b/web/src/chartjs-ext-tooltip.html @@ -0,0 +1,101 @@ +<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> + +<canvas id="myChart" style="margin:5em;"></canvas> + +<script> +const get_or_create_tooltip = (id) => { + if (tooltip = document.getElementById(id)) { + return tooltip; + } else { + // -- Create a new Tooltip element. + const tooltip = document.createElement('div'); + tooltip.id = id; + document.body.appendChild(tooltip); + + // -- Some minimal styling. + tooltip.style.background = 'rgba(0, 0, 0, 0.1)'; + tooltip.style.position = 'absolute'; + tooltip.style.transition = 'all .2s ease'; + + // -- Add a table element for the tooltip content. + const table = document.createElement('table'); + tooltip.appendChild(table); + + return tooltip + } +} + +const render_tooltip = (context) => { + const {chart, tooltip} = context; + + // -- Get Tooltip element. + const tooltip_elem = get_or_create_tooltip('myTooltip'); + + // -- Get data point values (only one data point). + const {label: x, formattedValue: y} = tooltip.dataPoints[0]; + + // -- Format new tooltip. + const link = document.createElement('a'); + link.href = "https://github.com/johannst"; + link.innerHTML = "X:" + x + " Y:" + y; + + // -- Remove previous child element and add new one. + const table = tooltip_elem.querySelector('table'); + table.innerHTML = ""; + table.appendChild(link); + + // -- Get absolute X/Y position of the top left corner of the canvas. + const {offsetLeft: canvas_x, offsetTop: canvas_y} = chart.canvas; + + // -- Set position and minimal style for the tooltip. + tooltip_elem.style.left = canvas_x + tooltip.caretX + 'px'; + tooltip_elem.style.top = canvas_y + tooltip.caretY + 'px'; + tooltip_elem.style.font = tooltip.options.bodyFont.string; + + // -- Place the tooltip (I) left or (II) right of the data point. + if (tooltip.xAlign === "right") { + tooltip_elem.style.transform = 'translate(-100%, 0)'; // (I) + } else if (tooltip.xAlign === "left") { + tooltip_elem.style.transform = 'translate(0%, 0)'; // (II) + } +} + +// -- Render a chart with some dummy data on the canvas. +const chart = new Chart( + document.getElementById('myChart'), + { + data: { + datasets: [{ + // -- A single dataset. + label: 'Just some values', + type: 'scatter', + data: [ + {x: 4, y: 4}, + {x: 5, y: 1}, + {x: 7, y: 6}, + {x: 10, y: 8}, + {x: 10, y: 7}, + {x: 10, y: 3}, + ], + backgroundColor: 'rgba(255, 99, 132, 0.5)', + borderColor: 'rgb(255, 99, 132)', + }], + }, + options: { + scales: { + y: { + beginAtZero: true, // -- Start the Y-Axis at zero instead min(y) of dataset. + } + }, + plugins: { + tooltip: { + enabled: false, // -- Disable builtin tooltips. + mode: 'nearest', // -- Get the item that is nearest to the mouse. + intersect: false, // -- 'mode' is active also when the mouse doesnt intersect with an item on the chart. + external: render_tooltip, // -- External tooltip handler, allows to create own HTML. + } + } + } + } +); +</script> diff --git a/web/src/tabs.html b/web/src/tabs.html new file mode 100644 index 0000000..af3f56a --- /dev/null +++ b/web/src/tabs.html @@ -0,0 +1,30 @@ +<script> +const showTab = (E, T) => { + const TABS = Array.from(document.getElementsByClassName("content")); + TABS.forEach(T => { + T.style.display = "none"; + }); + + document.getElementById(T).style.display = "block"; +}; + +window.onload = () => { + document.getElementById("bTab1").onclick = (E) => { + showTab(E, "tTab1"); + }; + document.getElementById("bTab2").onclick = (E) => { + showTab(E, "tTab2"); + }; +} +</script> + +<button type="button" id="bTab1">Tab1</button> +<button type="button" id="bTab2">Tab2</button> + +<div id="tTab1" class="content" style="display: block;"> + <p>Some content goes here ...</p> +</div> + +<div id="tTab2" class="content" style="display: none;"> + <p>... and there.</p> +</div> |