Custom Chart Tooltips in Angular
Every chart shows a tooltip when you hover over a data point. By default, it shows the point's key and value. To show your own content instead, put an <ng-template pcacTooltip> inside the chart. It's a regular Angular template with the hovered data passed in, so pipes, control flow, your own components, and styles all work as usual.
Try It
Hover over the charts below. Each one has its own template. The bar and line tooltips use parent to show the point's share of its group or series, and the bar chart's tooltip uses isThreshold to label thresholds.
Grouped bar
parent is the group
Line
parent is the series
Pie
No parent: slices are top level
Markup
Import PcacTooltipDirective along with the chart component, then put an <ng-template pcacTooltip> inside the chart element. The hovered data point is the template's implicit value (let-point, or any name you like). The rest of the context is picked up by name, as in let-group="parent".
import { PcacBarVerticalChart, PcacTooltipDirective } from '@pioneer-code/pioneer-charts';
@Component({
imports: [PcacBarVerticalChart, PcacTooltipDirective],
// ...
})<!-- Project an <ng-template pcacTooltip> into any chart. -->
<pcac-bar-vertical-chart [config]="config">
<ng-template pcacTooltip let-point let-group="parent" let-threshold="isThreshold">
<div class="my-tooltip">
<strong>{{ group?.key }}</strong>
<span>{{ point.key }}: {{ point.value | number }}</span>
@if (threshold) {
<em>threshold</em>
}
</div>
</ng-template>
</pcac-bar-vertical-chart>Template Context
With strictTemplates on, the template is type-checked against PcacTooltipContext, so point.value and the other fields are checked as PcacData.
interface PcacTooltipContext {
// The hovered data point: a bar, a line/area/plot point, a pie slice, or a threshold.
$implicit: PcacData;
// What the point belongs to: a bar's group, a point's series, or for a threshold, the data
// group it's drawn against. null when there's no parent level (pie slices, a chart-wide
// threshold).
parent: PcacData | null;
// true when the hovered item is a bar chart threshold marker instead of a data point.
isThreshold: boolean;
// The hovered point's position in parent.data (or in the top-level data when there's no
// parent, such as a pie slice). For a threshold, its position in thresholds.
index: number;
// parent's position in the top-level data. null whenever parent is null.
parentIndex: number | null;
// Every *other* point drawn at exactly the same coordinate as the hovered one, in data order,
// each described the same way ({ data, parent, index, parentIndex }). Filled in by the line,
// area, and plot charts, whether or not the plot chart's pointFanOut has spread them apart.
// Empty on the other charts, and when no other point shares the spot.
coincident: PcacTooltipCoincident[];
}Your Own Data
A PcacData only has a key and a value, but a tooltip often needs more, like the customer who placed an order or the unit a reading was taken in. The context's index and parentIndex give the hovered point's position in data. Charts never reorder data, so if you built it from a collection in the same order, with nothing filtered out, you can use those two indexes to look up the original item:
// Your source collection, grouped the same way as the chart: one entry per group and
// one item per bar. Build the chart's data from it without reordering or filtering.
regions: Region[] = [
{ name: 'North', orders: [{ customer: 'Acme', total: 1200 }, { customer: 'Globex', total: 800 }] },
{ name: 'South', orders: [{ customer: 'Initech', total: 950 }] },
];
config: PcacBarVerticalChartConfig = {
...new PcacBarVerticalChartConfig(),
data: this.regions.map(region => ({
key: region.name, value: null, hide: false,
data: region.orders.map(order => ({ key: order.customer, value: order.total, hide: false, data: [] })),
})),
};<!-- parentIndex picks the region, and index picks the order within it. -->
<pcac-bar-vertical-chart [config]="config">
<ng-template pcacTooltip let-point let-i="index" let-g="parentIndex">
@let order = regions[g!].orders[i];
<div class="my-tooltip">
<strong>{{ order.customer }}</strong>
<span>{{ order.total | currency }}</span>
</div>
</ng-template>
</pcac-bar-vertical-chart>parentIndex is null when there's no parent level, such as for a pie slice or a chart-wide threshold. For a pie, use items[index] directly. For a threshold, index is its position in thresholds (or in thresholds[parentIndex].data for a per-bar threshold). If your collection doesn't line up with data because you filtered, sorted, or grouped it along the way, look items up by key instead, or keep a map from key to item. Otherwise, the index could quietly point to the wrong row.
Styling
When you provide a template, the library applies none of its own tooltip styling: no background, padding, font, or color. It only positions the template (centered above the cursor) and keeps it from blocking the mouse. Your template controls the entire look.
/* Your template controls the whole look, so style it however you like. */
.my-tooltip {
padding: 0.5rem 0.75rem;
border-radius: 6px;
background: #fff;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
white-space: nowrap;
}The tooltip is added to <body> so it can float above everything else, but it still carries your component's style scope. That means component styles like the one above apply to it, without ::ng-deep or global CSS.
Notes
- Every chart supports it. That includes bar (vertical and horizontal, including thresholds), line, area, plot, pie, donut, dot plot, proximity, and state timeline. Without a template, the chart shows the default key/value tooltip, formatted with the chart's
xAxis.format/yAxis.format. Those formats aren't applied to a custom template, which gets the raw PcacData. - It updates live. The template is created when a hover starts, updated as the mouse moves, and removed when the hover ends. While it's showing, it takes part in change detection like any other view, so a signal read inside it keeps updating.
- One template per chart. Only the first
pcacTooltiptemplate inside a chart element is used. - Points in the same spot. On the line, area, and plot charts, the context's coincident field lists every other point at the same coordinate as the hovered one, so your template can show what else is there. The last demo on the Plot Chart page does this, along with pointFanOut, which spreads those points apart so each one can be hovered.