-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add plugin to draw crosshair to track y at x
and refactor js to use start using modules
- Loading branch information
Showing
8 changed files
with
217 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const chartStates = new WeakMap(); | ||
const crosshairs = { | ||
id: "chartjs-crosshairs", | ||
|
||
defaults: { | ||
color: "black", | ||
lineWidth: 1, | ||
}, | ||
|
||
beforeInit(chart) {}, | ||
|
||
// https://stackoverflow.com/a/77663018/23104322 | ||
afterEvent(chart, args, options) { | ||
const { ctx, chartArea } = chart; | ||
|
||
switch (args.event.type) { | ||
case "mousemove": | ||
drawCrosshair(chart, { x: args.event.x, y: args.event.y }); | ||
break; | ||
} | ||
}, | ||
|
||
afterDatasetsDraw(chart, args, options) {}, | ||
}; | ||
|
||
function drawCrosshair(chart, coord) { | ||
const { canvas, ctx, chartArea, scales } = chart; | ||
if (!scales.y) { | ||
return; | ||
} | ||
if ( | ||
chartArea.right >= coord.x && | ||
coord.x >= chartArea.left && | ||
chartArea.bottom >= coord.y && | ||
coord.y >= chartArea.top | ||
) { | ||
canvas.style.cursor = "crosshair"; | ||
|
||
ctx.lineWidth = 1; | ||
ctx.setLineDash([3, 3]); | ||
|
||
ctx.beginPath(); | ||
ctx.moveTo(chartArea.left, coord.y); | ||
ctx.lineTo(chartArea.right, coord.y); | ||
ctx.stroke(); | ||
ctx.closePath(); | ||
|
||
// draw y-value | ||
const yTipHeight = 10; | ||
ctx.beginPath(); | ||
ctx.fillStyle = "grey"; | ||
ctx.moveTo(chartArea.left, coord.y); | ||
ctx.lineTo(chartArea.left - yTipHeight / 2, coord.y + yTipHeight); | ||
ctx.lineTo(0, coord.y + yTipHeight); | ||
ctx.lineTo(0, coord.y - yTipHeight); | ||
ctx.lineTo(chartArea.left - yTipHeight / 2, coord.y - yTipHeight); | ||
ctx.fill(); | ||
ctx.closePath(); | ||
ctx.fillStyle = "white"; | ||
ctx.textBaseline = "middle"; | ||
ctx.textAlign = "right"; | ||
ctx.fillText( | ||
scales.y.getValueForPixel(coord.y).toFixed(2), | ||
chartArea.left - yTipHeight / 2, | ||
coord.y, | ||
); | ||
|
||
ctx.beginPath(); | ||
ctx.moveTo(coord.x, chartArea.top); | ||
ctx.lineTo(coord.x, chartArea.bottom); | ||
ctx.stroke(); | ||
ctx.closePath(); | ||
} else { | ||
canvas.style.cursor = "default"; | ||
} | ||
} | ||
|
||
export { crosshairs }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
let og_datasets; | ||
|
||
function toggleView(elem, chart) { | ||
if (elem.checked) { | ||
og_datasets = chart.data.datasets.map((dataset) => { | ||
return dataset.data; | ||
}); | ||
const total = og_datasets.reduce((r, a) => r.map((b, i) => a[i] + b)); | ||
for (const [index, data] of og_datasets.entries()) { | ||
chart.data.datasets[index].data = data.map((a, i) => (a / total[i]) * 100); | ||
} | ||
} else { | ||
for (const [index, data] of og_datasets.entries()) { | ||
chart.data.datasets[index].data = data; | ||
} | ||
} | ||
chart.update(); | ||
} | ||
|
||
export { toggleView }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// https://www.chartjs.org/docs/latest/developers/updates.html | ||
function addData(chart, newData, labels) { | ||
for (const data of newData) { | ||
chart.data.datasets.push(data); | ||
} | ||
if (labels != null) { | ||
chart.data.labels = labels; | ||
} | ||
chart.update(); | ||
} | ||
|
||
function displayPrices(chart, table, data) { | ||
const prices = data.indicators.quote[0].close; | ||
// ideally, should check overlap of labels between datasets | ||
table.updateOrAddData(data.timestamp.map((a, i) => ({ date: a, [data.meta.symbol]: prices[i] }))); | ||
if (!table.columnManager.columns.length) { | ||
table.addColumn({ title: "Date", field: "date", frozen: true }, true); | ||
} | ||
table.addColumn({ | ||
title: data.meta.symbol, | ||
field: data.meta.symbol, | ||
hozAlign: "right", | ||
headerSort: false, | ||
formatter: "money", | ||
formatterParams: { thousand: false }, | ||
}); | ||
chart.data.labels = data.timestamp; | ||
// change prices to percentage so it's comparable | ||
chart.data.datasets.push({ | ||
label: data.meta.symbol, | ||
data: prices.map((x) => ((x - prices[0]) / prices[0]) * 100), | ||
}); | ||
chart.update(); | ||
} | ||
|
||
function setupResponseHandlers() { | ||
document.body.addEventListener("displayPrices", function (evt) { | ||
const chart = Chart.getChart(evt.detail.target); | ||
const table = Tabulator.findTable("#priceTable")[0]; | ||
const data = JSON.parse(document.getElementById(evt.detail.dataId).textContent); | ||
displayPrices(chart, table, data); | ||
document.getElementById(evt.detail.dataId).remove(); | ||
}); | ||
|
||
// chartjs + htmx with payload in HX-Trigger | ||
document.body.addEventListener("drawChart", function (evt) { | ||
const chart = Chart.getChart(evt.detail.target); | ||
// https://www.reddit.com/r/htmx/comments/10sdk43/comment/j72m2j7/ | ||
const data = JSON.parse(document.getElementById(evt.detail.dataId).textContent); | ||
addData(chart, data.datasets, data.labels); | ||
document.getElementById(evt.detail.dataId).remove(); | ||
}); | ||
} | ||
|
||
const isKeyDown = (() => { | ||
// https://stackoverflow.com/a/48750898 | ||
const state = {}; | ||
|
||
// biome-ignore lint: let it mod | ||
window.addEventListener("keyup", (e) => (state[e.key] = false)); | ||
// biome-ignore lint: let it mod | ||
window.addEventListener("keydown", (e) => (state[e.key] = true)); | ||
|
||
return (key) => (Object.hasOwn(state, key) && state[key]) || false; | ||
})(); | ||
|
||
export { isKeyDown, setupResponseHandlers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.