@@ -52,6 +52,37 @@ object. The click event handler syntax looks similar to the following:
52
52
53
53
chart.addEventListener("click", callback);
54
54
55
+ The event handler also allows you to define the mark roles for which
56
+ you want to receive event information, thus eliminating the need to
57
+ check the payload. The click event handler syntax for defining the mark
58
+ roles to filter by looks similar to the following:
59
+
60
+ .. code-block:: sh
61
+ :copyable: false
62
+
63
+ const options = { includes: [{ roles: ['mark', 'axis-label'] }] };
64
+ chart.addEventListener("click", callback, options);
65
+
66
+ The click event handler must be added after the chart has finished
67
+ rendering, as shown in the following example:
68
+
69
+ .. example::
70
+
71
+ .. code-block:: sh
72
+
73
+ chart.render(document.getElementById("chart")).then(
74
+ () => chart.addEventListener('click',
75
+ (payload) => alert(JSON.stringify(payload)),
76
+ options)
77
+ );
78
+
79
+ .. note::
80
+
81
+ If you specify the ``options`` parameter, the click event handler
82
+ captures events only if the clicked mark's role matches one of the
83
+ values specified in the parameter. If you omit this parameter, the
84
+ click event handler captures all click events on the chart.
85
+
55
86
.. _click-event-payload:
56
87
57
88
Payload
@@ -77,7 +108,7 @@ payload:
77
108
.. code-block:: sh
78
109
:copyable: false
79
110
80
- chart.addEventListener("click", (payload) => {
111
+ {
81
112
"chartId": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
82
113
"chartTitle": "This is my chart's title",
83
114
"chartDescription": "This is my chart's description",
@@ -134,7 +165,7 @@ payload:
134
165
"fill": "#8CB6F2" // fill color of the mark
135
166
},
136
167
"apiVersion": 1 // API version of event payload
137
- });
168
+ }
138
169
139
170
To learn more about the elements inside the payload object, see:
140
171
@@ -227,6 +258,18 @@ filter document contains both the category and series filter pairs.
227
258
Each filter must reference the actual data source fields used and not
228
259
their labels.
229
260
261
+ On an embedded chart that includes event handlers with a filter for
262
+ element roles, the chart shows:
263
+
264
+ - The :icon-fa4:`hand-pointer-o` when you hover over an element that
265
+ triggers a filtered click event
266
+ - The :icon-fa4:`mouse-pointer` when you hover over an element that
267
+ doesn't trigger a click event
268
+
269
+ If the event handler doesn't include filter for element roles, the
270
+ :icon-fa4:`hand-pointer-o` appears when you hover over any chart
271
+ element.
272
+
230
273
.. _click-event-payload-target:
231
274
232
275
``target`` Element
@@ -279,7 +322,9 @@ In the `example app
279
322
<https://codesandbox.io/s/github/mongodb-js/charts-embed-sdk/tree/master/examples/click-events-basic>`__
280
323
for basic handling of click events, when you click on an element on the
281
324
``Movie Genres`` chart, the click event handler displays data based on
282
- the clicked element.
325
+ the clicked element. In this example application, the chart shows the
326
+ :icon-fa4:`hand-pointer-o` on all chart elements because the chart
327
+ doesn't include a filter for mark roles.
283
328
284
329
Each time you click on an element in the chart, the click event
285
330
listener refreshes the ``payload`` to reflect data from the ``x`` and
@@ -303,13 +348,21 @@ In the `example app
303
348
for interactive filtering, when you click on an element on the ``Movie
304
349
Genres`` chart, the embedding SDK generates a filter based on the
305
350
element on which you clicked and then applies the filter to a second
306
- chart.
307
-
308
- The click event listener generates a filter based on the ``y`` axis,
309
- which represents the movie genres, and the lower and upper bound range,
310
- which represents the decade. Each time you click on an element that
311
- represents a specific genre and decade on the ``Movie Genres`` chart,
312
- the ``Movie Details`` data table is filtered by the clicked element and
313
- changes to display the movies available in that genre and decade.
314
-
315
- Refer to the example app to view the full event payload.
351
+ chart. In this example application, the chart shows:
352
+
353
+ - The :icon-fa4:`hand-pointer-o` when you hover over an interactive
354
+ element
355
+ - The :icon-fa4:`mouse-pointer` when you hover over an element which
356
+ doesn't trigger a click event
357
+
358
+ The click event listener triggers events only for the mark roles
359
+ specified through the ``options`` parameter. The payload defines a
360
+ filter based on the ``y`` axis, which represents the movie genres, and
361
+ the lower and upper bound range, which represents the decade. Each time
362
+ you click on an element that represents a specific genre and decade on
363
+ the ``Movie Genres`` chart, the ``Movie Details`` data table is
364
+ filtered by the clicked element and changes to display the movies
365
+ available in that genre and decade.
366
+
367
+ Refer to the example app to view the full a sample event handler
368
+ callback function.
0 commit comments