Skip to content

Commit 4d173c6

Browse files
authored
Allow tooltip callback override in dataset (#8640)
1 parent 85123ac commit 4d173c6

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

docs/docs/configuration/tooltip.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,26 @@ Allows filtering of [tooltip items](#tooltip-item-context). Must implement at mi
102102

103103
Namespace: `options.plugins.tooltip.callbacks`, the tooltip has the following callbacks for providing text. For all functions, `this` will be the tooltip object created from the `Tooltip` constructor.
104104

105+
Namespace: `data.datasets[].tooltip.callbacks`, items marked with `Yes` in the column `Dataset override` can be overridden per dataset.
106+
105107
All functions are called with the same arguments: a [tooltip item context](#tooltip-item-context). All functions must return either a string or an array of strings. Arrays of strings are treated as multiple lines of text.
106108

107-
| Name | Arguments | Description
108-
| ---- | --------- | -----------
109-
| `beforeTitle` | `TooltipItem[], object` | Returns the text to render before the title.
110-
| `title` | `TooltipItem[], object` | Returns text to render as the title of the tooltip.
111-
| `afterTitle` | `TooltipItem[], object` | Returns text to render after the title.
112-
| `beforeBody` | `TooltipItem[], object` | Returns text to render before the body section.
113-
| `beforeLabel` | `TooltipItem, object` | Returns text to render before an individual label. This will be called for each item in the tooltip.
114-
| `label` | `TooltipItem, object` | Returns text to render for an individual item in the tooltip. [more...](#label-callback)
115-
| `labelColor` | `TooltipItem, Chart` | Returns the colors to render for the tooltip item. [more...](#label-color-callback)
116-
| `labelTextColor` | `TooltipItem, Chart` | Returns the colors for the text of the label for the tooltip item.
117-
| `labelPointStyle` | `TooltipItem, Chart` | Returns the point style to use instead of color boxes if usePointStyle is true (object with values `pointStyle` and `rotation`). Default implementation uses the point style from the dataset points. [more...](#label-point-style-callback)
118-
| `afterLabel` | `TooltipItem, object` | Returns text to render after an individual label.
119-
| `afterBody` | `TooltipItem[], object` | Returns text to render after the body section.
120-
| `beforeFooter` | `TooltipItem[], object` | Returns text to render before the footer section.
121-
| `footer` | `TooltipItem[], object` | Returns text to render as the footer of the tooltip.
122-
| `afterFooter` | `TooltipItem[], object` | Text to render after the footer section.
109+
| Name | Arguments | Dataset override | Description
110+
| ---- | --------- | ---------------- | -----------
111+
| `beforeTitle` | `TooltipItem[], object` | | Returns the text to render before the title.
112+
| `title` | `TooltipItem[], object` | | Returns text to render as the title of the tooltip.
113+
| `afterTitle` | `TooltipItem[], object` | | Returns text to render after the title.
114+
| `beforeBody` | `TooltipItem[], object` | | Returns text to render before the body section.
115+
| `beforeLabel` | `TooltipItem, object` | Yes | Returns text to render before an individual label. This will be called for each item in the tooltip.
116+
| `label` | `TooltipItem, object` | Yes | Returns text to render for an individual item in the tooltip. [more...](#label-callback)
117+
| `labelColor` | `TooltipItem, Chart` | Yes | Returns the colors to render for the tooltip item. [more...](#label-color-callback)
118+
| `labelTextColor` | `TooltipItem, Chart` | Yes | Returns the colors for the text of the label for the tooltip item.
119+
| `labelPointStyle` | `TooltipItem, Chart` | Yes | Returns the point style to use instead of color boxes if usePointStyle is true (object with values `pointStyle` and `rotation`). Default implementation uses the point style from the dataset points. [more...](#label-point-style-callback)
120+
| `afterLabel` | `TooltipItem, object` | Yes | Returns text to render after an individual label.
121+
| `afterBody` | `TooltipItem[], object` | | Returns text to render after the body section.
122+
| `beforeFooter` | `TooltipItem[], object` | | Returns text to render before the footer section.
123+
| `footer` | `TooltipItem[], object` | | Returns text to render as the footer of the tooltip.
124+
| `afterFooter` | `TooltipItem[], object` | | Text to render after the footer section.
123125

124126
### Label Callback
125127

src/plugins/plugin.tooltip.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ function createTooltipContext(parent, tooltip, tooltipItems) {
350350
});
351351
}
352352

353+
function overrideCallbacks(callbacks, context) {
354+
const override = context && context.dataset && context.dataset.tooltip && context.dataset.tooltip.callbacks;
355+
return override ? callbacks.override(override) : callbacks;
356+
}
357+
353358
export class Tooltip extends Element {
354359
constructor(config) {
355360
super();
@@ -451,9 +456,10 @@ export class Tooltip extends Element {
451456
lines: [],
452457
after: []
453458
};
454-
pushOrConcat(bodyItem.before, splitNewlines(callbacks.beforeLabel.call(me, context)));
455-
pushOrConcat(bodyItem.lines, callbacks.label.call(me, context));
456-
pushOrConcat(bodyItem.after, splitNewlines(callbacks.afterLabel.call(me, context)));
459+
const scoped = overrideCallbacks(callbacks, context);
460+
pushOrConcat(bodyItem.before, splitNewlines(scoped.beforeLabel.call(me, context)));
461+
pushOrConcat(bodyItem.lines, scoped.label.call(me, context));
462+
pushOrConcat(bodyItem.after, splitNewlines(scoped.afterLabel.call(me, context)));
457463

458464
bodyItems.push(bodyItem);
459465
});
@@ -511,9 +517,10 @@ export class Tooltip extends Element {
511517

512518
// Determine colors for boxes
513519
each(tooltipItems, (context) => {
514-
labelColors.push(options.callbacks.labelColor.call(me, context));
515-
labelPointStyles.push(options.callbacks.labelPointStyle.call(me, context));
516-
labelTextColors.push(options.callbacks.labelTextColor.call(me, context));
520+
const scoped = overrideCallbacks(options.callbacks, context);
521+
labelColors.push(scoped.labelColor.call(me, context));
522+
labelPointStyles.push(scoped.labelPointStyle.call(me, context));
523+
labelTextColors.push(scoped.labelTextColor.call(me, context));
517524
});
518525

519526
me.labelColors = labelColors;

0 commit comments

Comments
 (0)