You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/events.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ This plugin currently supports the following label events:
14
14
15
15
## Listeners
16
16
17
-
The `listeners` option allows to register callbacks to be notified when an event is detected on a specific label. This option is an object where each property represents an event, the key being the type of the event to listen and the value being a callback accepting a unique `context`argument.
17
+
The `listeners` option allows to register callbacks to be notified when an event is detected on a specific label. This option is an object where each property represents an event, the key being the type of the event to listen and the value being a callback accepting the `context`and `event` arguments.
18
18
19
19
The `context` contains the same information as for [scriptable options](options.md#option-context), can be modified (e.g. add new properties) and thus, **if the callback explicitly returns `true`**, the label is updated with the new context and the chart re-rendered. This allows to implement visual interactions with labels such as highlight, selection, etc.
20
20
@@ -32,10 +32,15 @@ If no listener is registered, incoming events are immediately ignored, preventin
32
32
datasets: [{
33
33
datalabels: {
34
34
listeners: {
35
-
click:function(context) {
35
+
click:function(context, event) {
36
36
// Receives `click` events only for labels of the first dataset.
37
37
// The clicked label index is available in `context.dataIndex`.
38
38
console.log('label '+context.dataIndex+' has been clicked!');
39
+
console.log('mouse is at position x:', event.x, 'and y:', event.y);
40
+
41
+
if (event.native.ctrlKey) {
42
+
console.log('control key is pressed!');
43
+
}
39
44
}
40
45
}
41
46
}
@@ -47,15 +52,15 @@ If no listener is registered, incoming events are immediately ignored, preventin
47
52
plugins: {
48
53
datalabels: {
49
54
listeners: {
50
-
enter:function(context) {
55
+
enter:function(context, event) {
51
56
// Receives `enter` events for any labels of any dataset. Indices of the
52
57
// clicked label are: `context.datasetIndex` and `context.dataIndex`.
53
58
// For example, we can modify keep track of the hovered state and
54
59
// return `true` to update the label and re-render the chart.
55
60
context.hovered=true;
56
61
returntrue;
57
62
},
58
-
leave:function(context) {
63
+
leave:function(context, event) {
59
64
// Receives `leave` events for any labels of any dataset.
0 commit comments