Tooltips

VictoryTooltip is a label component with defaultEvents It renders a customizable flyout container as well as a VictoryLabel component. VictoryTooltip can be used with any Victory component by setting the labelComponent prop like so labelComponent={<VictoryTooltip/>

This guide discusses customization and advanced usage of tooltips in Victory

Simple tooltips

The simplest way to add tooltips to a chart is to use VictoryTooltip as a labelComponent as in the example below:

When tooltips are added to a chart in this way, defaultEvents on VictoryTooltip are automatically added to the component using them, in this case VictoryBar. By default, VictoryTooltip will adjust its position, orientation, and the width and height of its container to match the corresponding data and labels.

Customizing Tooltips

Tooltips can be customized directly on the VictoryTooltip component

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

Tooltips with VictoryVoronoiContainer

Voronoi tooltips are useful for adding tooltips to a line, or adding tooltips to data points that are too small to hover over effectively. VictoryVoronoiContainer calculates a voronoi diagram based on the data of every child component it renders. The voronoi data is used to associate a mouse position with its nearest data point(s). When VictoryVoronoiContainer is added as the containerComponent of your chart, changes in mouse position will add and remove the active prop on appropriate data and label elements.

Multi-point Tooltips with VictoryVoronoiContainer

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.

VictoryVoronoiContainer also has a mouseFollowTooltips boolean prop that works with single point and multi-point tooltip labels.

Tooltips with Other 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 })
    })
  }
}];

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.

Wrapping VictoryTooltip

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.

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>