Skip to content

Commit 2d51a71

Browse files
author
Marco Zocca
committed
plot div id can be selected
1 parent 8bf5833 commit 2d51a71

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

htmx-plotly.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,23 @@
33
htmx.defineExtension('htmx-plotly', {
44
transformResponse : (text, xhr, elt) => {
55

6-
// const dataCurr = window['plotlyTestData']
7-
// console.log('htmx-plotly: plotlyTestData:' + JSON.stringify(dataCurr) ) // debug
8-
9-
const dataNew = JSON.parse(text)
10-
// console.log('htmx-plotly: new data: ' + JSON.stringify(dataNew))
11-
// window['plotlyTestData'] = dataNew
12-
Plotly.restyle('my-plot', dataNew);
6+
var plotEl = htmx.closest(elt, "[plot-id]"); // closest including div element
7+
if (plotEl) {
8+
const dataNew = JSON.parse(text)
9+
const plotId = plotEl.getAttribute('plot-id'); // lookup value of ? in < .. plot-id="?">
10+
var plotDiv = htmx.find("#" + plotId); // div element pointed at
11+
if (plotDiv) {
12+
// https://plotly.com/javascript/plotlyjs-function-reference/#plotlyrestyle
13+
Plotly.restyle(plotId, dataNew)
14+
} else {
15+
throw "Cannot find plot id: " + plotId
16+
}
17+
} else {
18+
console.log('No plot-id attribute defined')
19+
}
1320

1421
return ''
1522

16-
// var plotIdAttr = htmx.closest(elt, "[plot-id]"); // closest including div element
17-
// if (plotIdAttr) {
18-
// var data = JSON.parse(text);
19-
// var plotId = plotIdAttr.getAttribute('plot-id'); // < .. plot-id="?"> value of ?
20-
21-
// var plotDiv = htmx.find("#" + plotId); // div element pointed at
22-
// if (plotDiv) {
23-
// // https://plotly.com/javascript/plotlyjs-function-reference/#plotlyrestyle
24-
// Plotly.restyle(plotId, 'y', [data]);
25-
26-
// } else {
27-
// throw "Cannot find plot id:" + plotDiv
28-
// }
29-
// }
30-
3123
}
3224
}
3325
)

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
<div hx-ext="htmx-plotly">
12-
<a href="#" hx-post="/get-data" hx-swap="none"><h1>UPDATE</h1></a>
12+
<a href="#" hx-post="/get-data" hx-swap="none" plot-id="my-plot"><h1>UPDATE</h1></a>
1313
<div id="my-plot"></div>
1414
</div>
1515

0 commit comments

Comments
 (0)