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.

ParameterDescription
on_clickPoint click. On hierarchical charts, return False to prevent drill-down.
on_pressAlias for on_click
on_double_clickDouble-click (resets axes in zoom/pan mode)
on_double_pressAlias for on_double_click
on_selectedBox/lasso selection complete
on_deselectSelection cleared
on_relayoutLayout changed (pan, zoom, axis reset)
on_legend_clickLegend item clicked. Return False to prevent toggle.
on_legend_double_clickLegend item double-clicked. Return False to prevent isolate/show-all.
on_click_annotationAnnotation clicked
on_web_gl_context_lostWebGL 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: returning False prevents drill-down on hierarchical charts (sunburst, treemap, icicle). The return value is ignored on other chart types.
  • on_legend_click: returning False prevents the trace visibility toggle.
  • on_legend_double_click: returning False prevents 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:

  1. Selection sends details on every point within the selection box/lasso. Use this event only when you expect a small number of selected points.
  2. 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 with markers=True.
  3. 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

ParametersTypeDefaultDescription
tablePartitionedTable |
Table |
DataFrame
A table to pull data from.
xstr |
list[str] |
None
NoneA column or list of columns that contain x-axis values.
ystr |
list[str] |
None
NoneA column or list of columns that contain y-axis values.
bystr |
list[str] |
None
NoneA 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_varsstr |
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_bystr |
list[str] |
bool |
None
NoneA 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_bystr |
list[str] |
bool |
None
NoneA 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.
colorstr |
list[str] |
None
NoneA 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.
symbolstr |
list[str] |
None
NoneA 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.
sizestr |
list[str] |
None
NoneA 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_xstr |
None
NoneA 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_minusstr |
None
NoneA 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_ystr |
None
NoneA 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_minusstr |
None
NoneA column with y error bar values. These form the error bars in the negative direction, and are ignored if error_y is not specified.
textstr |
None
NoneA column that contains text annotations.
hover_namestr |
None
NoneA column that contains names to bold in the hover tooltip.
labelsdict[str, str] |
None
NoneA dictionary of labels mapping columns to new labels.
color_discrete_sequencelist[str] |
None
NoneA 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_mapstr |
tuple[str, dict[str | tuple[str], dict[str | tuple[str], str]]] |
dict[str | tuple[str], str] |
None
NoneIf 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_sequencelist[str] |
None
NoneA 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_mapstr |
tuple[str, dict[str | tuple[str], dict[str | tuple[str], str]]] |
dict[str | tuple[str], str] |
None
NoneIf 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_sequencelist[int] |
None
NoneA 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_mapstr |
tuple[str, dict[str | tuple[str], dict[str | tuple[str], str]]] |
dict[str | tuple[str], str] |
None
NoneIf 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_sequencelist[int] |
None
NoneA 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_sequencelist[int] |
None
NoneA 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_scalelist[str] |
None
NoneA list of colors for a continuous scale
range_colorlist[float] |
None
NoneA list of two numbers that form the endpoints of the color axis
color_continuous_midpointfloat |
None
NoneA number that is the midpoint of the color axis
opacityfloat |
None
NoneOpacity to apply to all markers. 0 is completely transparent and 1 is completely opaque.
marginal_xstr |
None
NoneThe type of x-axis marginal; histogram, violin, rug, box
marginal_ystr |
None
NoneThe type of y-axis marginal; histogram, violin, rug, box
log_xbool |
list[bool] |
None
FalseA 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_ybool |
list[bool]
FalseA 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_xlist[int] |
list[list[int]] |
None
NoneA 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_ylist[int] |
list[list[int]] |
None
NoneA 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_titleslist[str] |
None
NoneA list of titles to sequentially apply to the y axes. The titles do not loop.
xaxis_titleslist[str] |
None
NoneA list of titles to sequentially apply to the x axes. The titles do not loop.
titlestr |
None
NoneThe title of the chart
templatestr |
None
NoneThe template for the chart.
render_modestr'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"
calendarbool |
str |
Any
FalseA 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_figureCallable<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_clickCallable[..., bool | None] |
None
NoneA 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_pressCallable[..., bool | None] |
None
NoneAlias for on_click.
on_double_clickCallable[..., None] |
None
NoneA 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_pressCallable[..., None] |
None
NoneAlias for on_double_click.
on_selectedCallable[..., None] |
None
NoneA 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_deselectCallable[..., None] |
None
NoneA callback function that is called when the selection is cleared (e.g., by double-clicking on an empty area).
on_relayoutCallable[..., None] |
None
NoneA 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_clickCallable[..., bool | None] |
None
NoneA 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_clickCallable[..., bool | None] |
None
NoneA 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_annotationCallable[..., None] |
None
NoneA callback function that is called when an annotation is clicked. The function receives a dict with 'index', 'annotation', and 'modifiers'.
on_web_gl_context_lostCallable[..., None] |
None
NoneA callback function that is called when the WebGL rendering context is lost (e.g., GPU reclaims resources).