Skip to content

Commit d91d616

Browse files
DOCSP-15171/15172 doc for filter click events by element role (#464)
* DOCSP-15171 doc for filter click events by element role * DOCSP-15171 updates for copy review feedback * DOCSP-15171 updates for feedback
1 parent b671e4b commit d91d616

File tree

1 file changed

+66
-13
lines changed

1 file changed

+66
-13
lines changed

source/handle-click-events.txt

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,37 @@ object. The click event handler syntax looks similar to the following:
5252

5353
chart.addEventListener("click", callback);
5454

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+
5586
.. _click-event-payload:
5687

5788
Payload
@@ -77,7 +108,7 @@ payload:
77108
.. code-block:: sh
78109
:copyable: false
79110

80-
chart.addEventListener("click", (payload) => {
111+
{
81112
"chartId": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
82113
"chartTitle": "This is my chart's title",
83114
"chartDescription": "This is my chart's description",
@@ -134,7 +165,7 @@ payload:
134165
"fill": "#8CB6F2" // fill color of the mark
135166
},
136167
"apiVersion": 1 // API version of event payload
137-
});
168+
}
138169

139170
To learn more about the elements inside the payload object, see:
140171

@@ -227,6 +258,18 @@ filter document contains both the category and series filter pairs.
227258
Each filter must reference the actual data source fields used and not
228259
their labels.
229260

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+
230273
.. _click-event-payload-target:
231274

232275
``target`` Element
@@ -279,7 +322,9 @@ In the `example app
279322
<https://codesandbox.io/s/github/mongodb-js/charts-embed-sdk/tree/master/examples/click-events-basic>`__
280323
for basic handling of click events, when you click on an element on the
281324
``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.
283328

284329
Each time you click on an element in the chart, the click event
285330
listener refreshes the ``payload`` to reflect data from the ``x`` and
@@ -303,13 +348,21 @@ In the `example app
303348
for interactive filtering, when you click on an element on the ``Movie
304349
Genres`` chart, the embedding SDK generates a filter based on the
305350
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

Comments
 (0)