Skip to main content

Tooltips

Victory supports multiple ways to show tooltips on your charts. Tooltips can be added to any Victory component, and can be customized to suit your needs. This guide will cover the basics of adding tooltips to your charts, as well as more advanced configurations.

Basic

The simplest way to add tooltips to a chart is to use VictoryTooltip as a labelComponent. By default, the labelComponent will display the label prop of the data point it is associated with, unless you specify a custom labels function.

Live View
Loading...
Live Editor

Styles

VictoryTooltip can be styled using the style prop. The style prop accepts an object with data, labels, and flyout keys. The data key styles the data point, the labels key styles the text of the tooltip, and the flyout key styles the background of the tooltip.

Live View
Loading...
Live Editor

Events

VictoryTooltip automatically attaches events to data components. When events of the same type are specified for data components, it is necessary to reconcile events so that tooltips still work. For web, the default tooltip events are:

static defaultEvents = [{
target: "data",
eventHandlers: {
onMouseOver: () => ({
target: "labels",
mutation: () => ({ active: true })
}),
onMouseOut: () => ({
target: "labels",
mutation: () => ({ active: undefined })
}),
onFocus: () => ({
target: "labels",
mutation: () => ({ active: true })
}),
onBlur: () => ({
target: "labels",
mutation: () => ({ active: undefined })
})
}
}];
warning

When other onMouseOver and onMouseOut events are specified for data, the event returns described above must be added to the events for tooltips to continue to work properly.

Live View
Loading...
Live Editor

Voronoi

Use VictoryVoronoiContainer to associate a mouse position with the data point(s) closest to it. When this container is added to a chart, changes in mouse position will add the active prop to data and label components closest to the current mouse position. The closeness of data points to a given position is determined by calculating a voronoi diagram based on the data of every child VictoryVoronoiContainer renders. This container is useful for adding hover interactions, like tooltips, to small data points, or charts with dense or overlapping data.

See the full API here.

Basic

VictoryVoronoiContainer may be used with any Victory component that works with an x-y coordinate system, and should be added as the containerComponent of the top-level component. However, the component that uses it must be standalone (standalone={true}), which is the default for all top-level Victory components.

Live View
Loading...
Live Editor

Follow Tooltips

When using VictoryVoronoiContainer with VictoryTooltip, you can add tooltips to your chart that follow the mouse position.

Live View
Loading...
Live Editor

Active Points

VictoryVoronoiContainer adds the active prop to any data point closest to the current mouse position. This prop can be used to style the active data point differently from the rest.

Live View
Loading...
Live Editor

Multipoint Labels

VictoryVoronoiContainer can also be used to create multi-point labels when the labels prop is provided. In the example below the voronoiDimension prop indicates that the voronoi diagram will only be specific to the x dimension. For a given mouse position, all data matching the associated x value will be activated regardless of y value. In the following example, this leads to several tooltips being active at the same time. Provide a labels and (optionally) a labelComponent prop to configure multi-point labels.

Live View
Loading...
Live Editor

Cursor

Use VictoryCursorContainer to add a cursor to a chart to inspect coordinates. The cursor can either be a 2-dimensional crosshair, or a 1-dimensional line. The cursor moves with the mouse (or on touch on mobile devices) along the visible domain of the chart. The cursor can also display a label for the active coordinates using the cursorLabel prop.

See the full API here.

Line Charts

Using the VictoryCursorContainer component, you can add a 2D cursor to a line chart.

Live View
Loading...
Live Editor

Dimension Limits

You can also use the cursorDimension prop to create a 1D cursor. This is useful for line charts where you only want to inspect one dimension. Note you can also set a defaultCursorValue to set the initial position of the cursor.

Live View
Loading...
Live Editor

Bar Charts

You can also use the VictoryCursorContainer component with bar charts.

Live View
Loading...
Live Editor

Scatter Charts

You can also use the VictoryCursorContainer component with scatter charts. Note how we can apply the container directly to the VictoryScatter component.

Live View
Loading...
Live Editor

Events

You can also use the onCursorChange prop to listen to cursor changes and inspect the values.

Live View
Loading...
Live Editor

Custom Components

VictoryTooltip is composed of VictoryLabel and the primitive Flyout component. Both of these components are highly configurable, but may also be replaced if necessary.

Custom Flyout

An example of replacing the default Flyout component with a custom component.

Live View
Loading...
Live Editor

Custom Label

An example of using custom labels with a donut chart.

Live View
Loading...
Live Editor

Custom Wrapping

The events that control VictoryTooltip are stored on the static defaultEvents property. Wrapped instances of VictoryTooltip will need to replicate or hoist this property in order to add automatic events to the components that use them.

Live View
Loading...
Live Editor

Victory Native

In Victory Native tooltips are much more reliable when using VictoryVoronoiContainer. Using VictoryVoronoiContainer registers all touch events on the container itself, which mitigates interference with other chart elements, which can be a problem on some platforms. Showing the closest data point with VictoryVoronoiContainer also increases the tap targets for the tooltip, which can otherwise be quite small. Set VictoryVoronoiContainer as the containerComponent prop on the outermost Victory component.

<VictoryChart
containerComponent={
<VictoryVoronoiContainer />
}
>
<VictoryScatter
labelComponent={<VictoryTooltip />}
labels={({ datum }) => datum.y}
style={{
data: {
fill: ({ datum }) => datum.fill,
},
}}
data={[
{ x: 1, y: 3 },
{ x: 3, y: 5 },
]}
/>
</VictoryChart>

Like this project? You'll love working with us.

Contact us to learn more about our full range of services and offerings.

Learn More