Skip to content

Commit 966afa4

Browse files
authored
Add support for annotation and button click (#525)
1 parent afc72e2 commit 966afa4

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

readme.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,44 @@ on_dblclick: |-
758758
}
759759
```
760760

761+
## Annotation and button click handlers
762+
763+
In a similar way, you can respond to clicks on annotations (requiring `captureevents: true`).
764+
765+
```yaml
766+
type: custom:plotly-graph
767+
entities:
768+
- entity: sensor.temperature1
769+
layout:
770+
annotations:
771+
- x: 1
772+
xref: paper
773+
"y": 1
774+
yref: paper
775+
showarrow: false
776+
text: "📊"
777+
captureevents: true
778+
on_click: $ex () => { window.location="/history?entity_id=sensor.temperature1"; }
779+
```
780+
781+
Or to clicks on custom update menu buttons.
782+
783+
```yaml
784+
type: custom:plotly-graph
785+
entities:
786+
- entity: sensor.temperature1
787+
layout:
788+
updatemenus:
789+
- buttons:
790+
- label: History
791+
method: skip
792+
on_click: $ex () => { window.location="/history?entity_id=sensor.temperature1"; }
793+
showactive: false
794+
type: buttons
795+
x: 1
796+
"y": 1
797+
```
798+
761799
See more in plotly's [official docs](https://plotly.com/javascript/plotlyjs-events)
762800

763801
## Universal functions

src/plotly-graph-card.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export class PlotlyGraph extends HTMLElement {
5454
legendItemDoubleclick?: EventEmitter;
5555
dataClick?: EventEmitter;
5656
doubleclick?: EventEmitter;
57+
annotationClick?: EventEmitter;
58+
buttonClick?: EventEmitter;
5759
} = {};
5860

5961
constructor() {
@@ -178,6 +180,15 @@ export class PlotlyGraph extends HTMLElement {
178180
"plotly_doubleclick",
179181
this.onDoubleclick
180182
)!;
183+
this.handles.annotationClick = this.contentEl.on(
184+
"plotly_clickannotation",
185+
this.onAnnotationClick
186+
)!;
187+
this.handles.buttonClick = this.contentEl.on(
188+
// @ts-ignore Not properly typed in @types/plotly.js
189+
"plotly_buttonclicked",
190+
this.onButtonClick
191+
)!;
181192
this.resetButtonEl.addEventListener("click", this.exitBrowsingMode);
182193
this.touchController.connect();
183194
this.plot({ should_fetch: true });
@@ -197,6 +208,8 @@ export class PlotlyGraph extends HTMLElement {
197208
);
198209
this.handles.dataClick?.off("plotly_click", this.onDataClick);
199210
this.handles.doubleclick?.off("plotly_doubleclick", this.onDoubleclick);
211+
this.handles.annotationClick?.off("plotly_clickannotation", this.onAnnotationClick);
212+
this.handles.buttonClick?.off("plotly_buttonclicked", this.onButtonClick);
200213
clearTimeout(this.handles.refreshTimeout!);
201214
this.resetButtonEl.removeEventListener("click", this.exitBrowsingMode);
202215
this.touchController.disconnect();
@@ -309,6 +322,18 @@ export class PlotlyGraph extends HTMLElement {
309322
onDoubleclick = () => {
310323
return this.parsed_config.on_dblclick();
311324
};
325+
onAnnotationClick = ({ annotation, ...rest }) => {
326+
if (annotation.on_click) {
327+
return annotation.on_click({ annotation, ...rest });
328+
}
329+
return true;
330+
};
331+
onButtonClick = ({ button, ...rest }) => {
332+
if (button._input.on_click) {
333+
return button._input.on_click({ button, ...rest });
334+
}
335+
return true;
336+
};
312337
onRestyle = async () => {
313338
// trace visibility changed, fetch missing traces
314339
if (this.isInternalRelayout) return;

yaml-editor/src/schema.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,17 @@
34983498
"$ref": "#/definitions/With$fn<string|undefined>",
34993499
"description": "Sets text to appear when hovering over this annotation.\nIf omitted or blank, no hover label will appear."
35003500
},
3501+
"onclick": {
3502+
"anyOf": [
3503+
{
3504+
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
3505+
"type": "string"
3506+
},
3507+
{
3508+
"$ref": "#/definitions/With$fn<Function>"
3509+
}
3510+
]
3511+
},
35013512
"opacity": {
35023513
"anyOf": [
35033514
{
@@ -4028,6 +4039,17 @@
40284039
"$ref": "#/definitions/With$fn<string|undefined>",
40294040
"description": "Sets text to appear when hovering over this annotation.\nIf omitted or blank, no hover label will appear."
40304041
},
4042+
"onclick": {
4043+
"anyOf": [
4044+
{
4045+
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
4046+
"type": "string"
4047+
},
4048+
{
4049+
"$ref": "#/definitions/With$fn<Function>"
4050+
}
4051+
]
4052+
},
40314053
"opacity": {
40324054
"anyOf": [
40334055
{

0 commit comments

Comments
 (0)