Chart Events
Chart events allow you to respond to user interactions on a chart with callback functions. Supported events include clicks, selections, layout changes (pan/zoom), and legend interactions.
Example
What are chart events useful for?
- Filtering related tables when a user clicks a data point (e.g., click a point to show details in another panel)
- Bulk-selecting data to feed into downstream computations or analysis
- Synchronizing multiple components by responding to pan/zoom events
- Controlling legend behavior (e.g., prevent hiding a trace if it’s the last one visible)
- Controlling drill-down on hierarchical charts (sunburst, treemap, icicle)
- Responding to annotation clicks for interactive dashboards
Available Events
All event callbacks are optional keyword arguments that default to None.
| Parameter | Description |
|---|---|
on_click | Point click. On hierarchical charts, return False to prevent drill-down. |
on_press | Alias for on_click |
on_double_click | Double-click (resets axes in zoom/pan mode) |
on_double_press | Alias for on_double_click |
on_selected | Box/lasso selection complete |
on_deselect | Selection cleared |
on_relayout | Layout changed (pan, zoom, axis reset) |
on_legend_click | Legend item clicked. Return False to prevent toggle. |
on_legend_double_click | Legend item double-clicked. Return False to prevent isolate/show-all. |
on_click_annotation | Annotation clicked |
on_web_gl_context_lost | WebGL context lost |
Note
All events are accepted by all chart types. Some combinations have no natural top-level trigger (such as on_click_annotation), but may become relevant if you add elements via unsafe_update_figure.
Preventable Events
Three events can optionally return a bool to control default behavior:
on_click: returningFalseprevents drill-down on hierarchical charts (sunburst, treemap, icicle). The return value is ignored on other chart types.on_legend_click: returningFalseprevents the trace visibility toggle.on_legend_double_click: returningFalseprevents the isolate/show-all toggle.
Warning
Preventable event callbacks block the default behavior until the Python function returns. Avoid heavy synchronous computation inside these callbacks. If the function takes too long, the chart will feel unresponsive.
Event Data
Every event payload includes a modifiers dict describing which keyboard modifier keys were held during the interaction.
For example, use modifiers to branch on shift-click:
Click events (on_click, on_press)
on_press is an alias for on_click and receives the same payload.
On hierarchical charts, an additional next_level field is included, which indicates the label of the next level that would be drilled into if the click is allowed:
If clickanywhere is enabled via unsafe_update_figure, clicking empty space fires with an empty points list plus xvals and yvals.
Double-click events (on_double_click, on_double_press)
on_double_press is an alias for on_double_click and receives the same payload. This event fires when the user double-clicks the plot area (commonly used to reset axes in zoom/pan mode).
Selection events (on_selected)
On box or lasso selection, the event includes an array of selected points and the selection box range (box select only):
Warning
Selection has several caveats:
- Selection sends details on every point within the selection box/lasso. Use this event only when you expect a small number of selected points.
- Only points drawn as markers can be selected. Traces that render as lines only (such as
dx.line) contribute no points to the selection. To make a line chart selectable, add markers, such as withmarkers=True.- The selection will be misleading if the chart is downsampled.
Deselect events (on_deselect)
When the user clicks outside the selected area or presses escape, the selection is cleared and the event fires with modifiers only:
Layout events (on_relayout)
When the user pans or zooms, the event contains the new axis ranges:
When the user resets the axes (e.g., by double-clicking), the event contains autorange fields instead:
Legend events (on_legend_click, on_legend_double_click)
Annotation events (on_click_annotation)
Examples
Many of the following examples use deephaven.ui. deephaven.ui is a library for building complex interactive web apps, which naturally complements event-driven charts.
Filter a table on click
Use on_click to capture the clicked data point and use that information to filter a related table.
Prevent drill-down on hierarchical charts on click
Return False from the on_click handler to prevent drill-down on hierarchical charts of type sunburst, treemap, or icicle.
Capture selected points into a table on box/lasso select
Use on_selected to capture the points within the selection box/lasso and build a new table from those points.
Sync visible data to a table on pan/zoom
Use on_relayout to capture the new axis ranges when the user pans or zooms, and use that information to filter a related table to only the visible data.
Prevent legend toggle on click
Use on_legend_click to prevent the default trace visibility toggle when a user clicks a legend item.
Prevent legend double-click (isolate/show-all) toggle
Use on_legend_double_click to prevent the default isolate/show-all toggle when a user double-clicks a legend item.
Respond to annotation clicks
Annotations are added via unsafe_update_figure. The on_click_annotation event fires when clicking on an annotation.
Add an annotation wherever you click
By default, on_click only fires when a data point is clicked. Enable Plotly’s clickanywhere layout option (via unsafe_update_figure) so the callback also fires on empty plot area, where the payload carries xvals/yvals (the cursor position in data space).
Events with layer and make_subplots
Event callbacks can be passed directly to layer and make_subplots. Passing in callbacks directly takes priority over callbacks derived from the child figures. If multiple children set the same callback, the callback from the last subplot passed in will take priority.
API Reference
Returns a scatter chart
Returns: DeephavenFigure A DeephavenFigure that contains the scatter chart
| Parameters | Type | Default | Description |
|---|---|---|---|
| table | PartitionedTable | Table | DataFrame | A table to pull data from. | |
| x | str | list[str] | None | None | A column or list of columns that contain x-axis values. |
| y | str | list[str] | None | None | A column or list of columns that contain y-axis values. |
| by | str | list[str] | None | None | A column or list of columns that contain values to plot the figure traces by. All values or combination of values map to a unique design. The variable by_vars specifies which design elements are used. This is overriden if any specialized design variables such as color are specified |
| by_vars | str | list[str] | 'color' | A string or list of string that contain design elements to plot by. Can contain size, line_dash, width, color, and symbol. If associated maps or sequences are specified, they are used to map by column values to designs. Otherwise, default values are used. |
| filter_by | str | list[str] | bool | None | None | A column or list of columns that contain values to filter the chart by. If a boolean is passed and the table is partitioned, all partition key columns used to create the partitions are used. If no filters are specified, all partitions are shown on the chart. |
| required_filter_by | str | list[str] | bool | None | None | A column or list of columns that contain values to filter the chart by. Values set in input filters or linkers for the relevant columns determine the exact values to display. If a boolean is passed and the table is partitioned, all partition key columns used to create the partitions are used. All required input filters or linkers must be set for the chart to display any data. |
| color | str | list[str] | None | None | A column or list of columns that contain color values. If only one column is passed, and it contains numeric values, the value is used as a value on a continuous color scale. Otherwise, the value is used for a plot by on color. See color_discrete_map for additional behaviors. |
| symbol | str | list[str] | None | None | A column or list of columns that contain symbol values. The value is used for a plot by on symbol. See color_discrete_map for additional behaviors. |
| size | str | list[str] | None | None | A column or list of columns that contain size values. If only one column is passed, and it contains numeric values, the value is used as a size. Otherwise, the value is used for a plot by on size. See size_map for additional behaviors. |
| error_x | str | None | None | A column with x error bar values. These form the error bars in both the positive and negative direction if error_x_minus is not specified, and the error bars in only the positive direction if error_x_minus is specified. None can be used to specify no error bars on the corresponding series. |
| error_x_minus | str | None | None | A column with x error bar values. These form the error bars in the negative direction, and are ignored if error_x is not specified. |
| error_y | str | None | None | A column with x error bar values. These form the error bars in both the positive and negative direction if error_y_minus is not specified, and the error bars in only the positive direction if error_y_minus is specified. None can be used to specify no error bars on the corresponding series. |
| error_y_minus | str | None | None | A column with y error bar values. These form the error bars in the negative direction, and are ignored if error_y is not specified. |
| text | str | None | None | A column that contains text annotations. |
| hover_name | str | None | None | A column that contains names to bold in the hover tooltip. |
| labels | dict[str, str] | None | None | A dictionary of labels mapping columns to new labels. |
| color_discrete_sequence | list[str] | None | None | A list of colors to sequentially apply to the series. The colors loop, so if there are more series than colors, colors will be reused. |
| color_discrete_map | str | tuple[str, dict[str | tuple[str], dict[str | tuple[str], str]]] | dict[str | tuple[str], str] | None | None | If dict, the keys should be strings of the column values (or a tuple of combinations of column values) which map to colors. If "identity", the values are taken as literal colors. If "by" or ("by", dict) where dict is as described above, the colors are forced to by |
| symbol_sequence | list[str] | None | None | A list of symbols to sequentially apply to the markers in the series. The symbols loop, so if there are more series than symbols, symbols will be reused. |
| symbol_map | str | tuple[str, dict[str | tuple[str], dict[str | tuple[str], str]]] | dict[str | tuple[str], str] | None | None | If dict, the keys should be strings of the column values (or a tuple of combinations of column values) which map to symbols. If "identity", the values are taken as literal symbols. If "by" or ("by", dict) where dict is as described above, the symbols are forced to by |
| size_sequence | list[int] | None | None | A list of sizes to sequentially apply to the markers in the series. The sizes loop, so if there are more series than symbols, sizes will be reused. This is overriden is "size" is specified. |
| size_map | str | tuple[str, dict[str | tuple[str], dict[str | tuple[str], str]]] | dict[str | tuple[str], str] | None | None | If dict, the keys should be strings of the column values (or a tuple of combinations of column values) which map to sizes. If "identity", the values are taken as literal sizes. If "by" or ("by", dict) where dict is as described above, the sizes are forced to by |
| xaxis_sequence | list[int] | None | None | A list of x axes to assign series to. Odd numbers starting with 1 are created on the bottom x axis and even numbers starting with 2 are created on the top x axis. Axes are created up to the maximum number specified. The axes loop, so if there are more series than axes, axes will be reused. |
| yaxis_sequence | list[int] | None | None | A list of y axes to assign series to. Odd numbers starting with 1 are created on the left y axis and even numbers starting with 2 are created on the top y axis. Axes are created up to the maximum number specified. The axes loop, so if there are more series than axes, axes will be reused. |
| color_continuous_scale | list[str] | None | None | A list of colors for a continuous scale |
| range_color | list[float] | None | None | A list of two numbers that form the endpoints of the color axis |
| color_continuous_midpoint | float | None | None | A number that is the midpoint of the color axis |
| opacity | float | None | None | Opacity to apply to all markers. 0 is completely transparent and 1 is completely opaque. |
| marginal_x | str | None | None | The type of x-axis marginal; histogram, violin, rug, box |
| marginal_y | str | None | None | The type of y-axis marginal; histogram, violin, rug, box |
| log_x | bool | list[bool] | None | False | A boolean or list of booleans that specify if the corresponding axis is a log axis or not. The booleans loop, so if there are more series than booleans, booleans will be reused. |
| log_y | bool | list[bool] | False | A boolean or list of booleans that specify if the corresponding axis is a log axis or not. The booleans loop, so if there are more series than booleans, booleans will be reused. |
| range_x | list[int] | list[list[int]] | None | None | A list of two numbers or a list of lists of two numbers that specify the range of the x axes. None can be specified for no range The ranges loop, so if there are more axes than ranges, ranges will be reused. |
| range_y | list[int] | list[list[int]] | None | None | A list of two numbers or a list of lists of two numbers that specify the range of the y axes. None can be specified for no range The ranges loop, so if there are more axes than ranges, ranges will be reused. |
| yaxis_titles | list[str] | None | None | A list of titles to sequentially apply to the y axes. The titles do not loop. |
| xaxis_titles | list[str] | None | None | A list of titles to sequentially apply to the x axes. The titles do not loop. |
| title | str | None | None | The title of the chart |
| template | str | None | None | The template for the chart. |
| render_mode | str | 'webgl' | Either "svg" or "webgl". The default is "webgl" as it leads to a more performant plot but there may be graphical bugs, in which case it is recommended to switch to "svg" |
| calendar | bool | str | Any | False | A boolean, BusinessCalendar or string that specifies a calendar to use for the chart. By default, False and no calendar is used. If True, the default calendar is used. If a string, the calendar with that name is used. If a BusinessCalendar is passed, that calendar is used. Note that if this is provided, render_mode is forced to "svg" as "webgl" is not supported. |
| unsafe_update_figure | Callable | <function default_callback> | An update function that takes a plotly figure as an argument and optionally returns a plotly figure. If a figure is not returned, the plotly figure passed will be assumed to be the return value. Used to add any custom changes to the underlying plotly figure. Note that the existing data traces should not be removed. This may lead to unexpected behavior if traces are modified in a way that break data mappings. |
| on_click | Callable[..., bool | None] | None | None | A callback function that is called when a point is clicked. The function receives a dict with 'points' (list of clicked point data) and 'modifiers' (keyboard state). On hierarchical charts (sunburst, treemap, icicle), return False to prevent drill-down. The return value is ignored on other chart types. |
| on_press | Callable[..., bool | None] | None | None | Alias for on_click. |
| on_double_click | Callable[..., None] | None | None | A callback function that is called on double-click. The function receives a dict with 'modifiers' (keyboard state). Fires in zoom/pan mode only; in select mode, on_deselect fires instead. |
| on_double_press | Callable[..., None] | None | None | Alias for on_double_click. |
| on_selected | Callable[..., None] | None | None | A callback function that is called when a box or lasso selection completes. The function receives a dict with 'points' (list of selected point data), 'range' (for box select), and 'modifiers'. |
| on_deselect | Callable[..., None] | None | None | A callback function that is called when the selection is cleared (e.g., by double-clicking on an empty area). |
| on_relayout | Callable[..., None] | None | None | A callback function that is called when the chart layout changes due to user interaction (pan, zoom, axis reset, etc.). The function receives a dict of the layout keys that changed. |
| on_legend_click | Callable[..., bool | None] | None | None | A callback function that is called when a legend item is clicked. Return False to prevent the default trace visibility toggle. Return True or None to allow it. |
| on_legend_double_click | Callable[..., bool | None] | None | None | A callback function that is called when a legend item is double-clicked. Return False to prevent the default isolate/show-all toggle. Return True or None to allow it. |
| on_click_annotation | Callable[..., None] | None | None | A callback function that is called when an annotation is clicked. The function receives a dict with 'index', 'annotation', and 'modifiers'. |
| on_web_gl_context_lost | Callable[..., None] | None | None | A callback function that is called when the WebGL rendering context is lost (e.g., GPU reclaims resources). |