Tooltip

January 25, 2021 · View on GitHub

Tooltip area displays information regarding the data on which the mouse is hovering.

image

The Tooltip area is divided into header area and the body area. The header area displays the category value, and the body area displays the data value.

options

The following is a list of options that can be used to control the tooltip.

/* Tooltip options */
interface TooltipOptions {
  offsetX?: number;
  offsetY?: number;
  formatter?: (value) => string;
  template?: (
    model: TooltipTemplateModel,
    defaultTemplate: { header: string; body: string },
    theme: Required<TooltipTheme>
  ) => string;
  transition: boolean | string;
}

type TooltipTemplateModel = {
  data: {
    label: string;
    color: string;
    value: TooltipDataValue;
    formattedValue?: string;
    category?: string;
    percentValue?: number;
  }[];
  category?: string;
};

offsetX, offsetY

The offsetX and offsetY options can be used to change the location of the tooltip.

  • default
    • offsetX: 0
    • offsetY: 0

Given that the tooltip's original location is (0,0), a positive offsetX and a negative offsetY can move the tooltip in the North East direction.

const options = {
  tooltip: {
    offsetX: 30,
    offsetY: -100,
  },
};

image

Formatter

The tooltip.formatter option can be used to format the data before the data is displayed. The formatting function takes the values and tooltip data information as parameters and returns the formatted string.

Let's write a simple example that compares the entered values and adds an emoji.

const options = {
  tooltip: {
    formatter: (value, tooltipDataInfo) => {
      const temp = Number(value);
      let icon = '☀️';
      if (temp < 0) {
        icon = '❄️';
      } else if (temp > 25) {
        icon = '🔥';
      }

      console.log(tooltipDataInfo); // { category: '08/01/2020', color: '#785fff', index: 7, seriesIndex: 4, value: -0.1, label: 'Jungfrau'}

      return `${icon} ${value} ℃`;
    },
  },
};

image

In this case, the given value is -4.1 and the value is formatted according to our needs and is displayed.

Custom tooltip

The custom tooltip can be designed using the tooltip.template. It takes a function that returns HTML string as a parameter. This function takes three parameters: data, original template, and the theme for the original template.

NameTypeDetails
modelobjectCollection of data information used to activate the tooltip as an object. The first parameter of the function.
model.xnumberx coordinate of the data
model.ynumbery coordinate of the data
model.categorystringCategory value of the data
model.labelstringLabel value of the data
model.dataobjectCollection of data as an object
model.data.categorystringCategory value of the data
model.data.colorstringColor of the data series
model.data.formattedValuestringFormatted value of the data
model.data.labelstringLabel value of the data
model.data.valueCurrent chart's data typeValue of the data
defaultTooltipTemplateobjectOriginal HTML template object. Second parameter of the function.
defaultTooltipTemplate.headerstringHTML template for the header area where the category is displayed.
defaultTooltipTemplate.bodystringHTML template for the body area where the data is displayed.
themeobjectTheme used for the original tooltip. Third parameter of the function. More details are included in the tooltip theme chapter.

Let's use these options to create a custom tooltip that uses a new header with the original body.

const options = {
  tooltip: {
    template: (model, defaultTooltipTemplate, theme) => {
      const { body, header } = defaultTooltipTemplate;
      const { background } = theme;

      return `
        <div style="
          background: ${background};
          width: 140px;
          padding: 0 5px;
          text-align: center;
          color: white;
          ">
            <p>🎊 ${model.category} 🎊</p>
            ${body}
          </div>`;
      `
    }
  }
}

The result of the code above is shown below.

image

transition

tooltip.transition is an option to control the movement animation of the tooltip. Usage is the same as CSS transition property.

  • default: false

The position of the tooltip is changed through the transform property, so the transition-property should be transform. If the option is set to true, it moves to transform 0.2s ease.

Let's use these options to make a tooltip move more slowly.

const options = {
  tooltip: {
    transition: 'transform 1s ease-in',
  },
};

tooltip-transition

theme

The theme options that are currently provided for tooltip are as follows.

interface TooltipTheme {
  background?: string;
  borderColor?: string;
  borderWidth?: number;
  borderStyle?: string;
  borderRadius?: number;
  header?: {
    fontSize?: number;
    fontFamily?: string;
    fontWeight?: string | number;
    color?: string;
  };
  body?: {
    fontSize?: number;
    fontFamily?: string;
    fontWeight?: string | number;
    color?: string;
  };
}
NameTypeDetails
backgroundstringBackground color
borderColorstringBorder line color
borderWidthnumberBorder line width
borderStylestringStyles for the border line. Available options can be found in this MDN link.
borderRadiusnumberBorder rounding value
headerobjectStyles for the tooltip header
bodyobjectStyles for the tooltip body

Let's style the tooltip background with different colors and borders, for example.

const options = {
  theme: {
    tooltip: {
      background: '#80CEE1',
      borderColor: '#3065AC',
      borderWidth: 10,
      borderRadius: 20,
      borderStyle: 'double',
    },
  },
};

The code above results as shown below.

image