chartjs/Chart.js · error · Error
Cannot find a dataset at index ${datasetIndex}
Error message
Cannot find a dataset at index ${datasetIndex} What it means
Tooltip.setActiveElements maps each {datasetIndex, index} to chart.getDatasetMeta(datasetIndex); like the controller's variant, getDatasetMeta returns undefined when the index is out of range or metasets are not yet built, and the tooltip throws. This is the tooltip's public programmatic API for forcing tooltip visibility on specific points.
Source
Thrown at src/plugins/plugin.tooltip.js:1112
* Get active elements in the tooltip
* @returns {Array} Array of elements that are active in the tooltip
*/
getActiveElements() {
return this._active || [];
}
/**
* Set active elements in the tooltip
* @param {array} activeElements Array of active datasetIndex/index pairs.
* @param {object} eventPosition Synthetic event position used in positioning
*/
setActiveElements(activeElements, eventPosition) {
const lastActive = this._active;
const active = activeElements.map(({datasetIndex, index}) => {
const meta = this.chart.getDatasetMeta(datasetIndex);
if (!meta) {
throw new Error('Cannot find a dataset at index ' + datasetIndex);
}
return {
datasetIndex,
element: meta.data[index],
index,
};
});
const changed = !_elementsEqual(lastActive, active);
const positionChanged = this._positionChanged(active, eventPosition);
if (changed || positionChanged) {
this._active = active;
this._eventPosition = eventPosition;
this._ignoreReplayEvents = true;
this.update(true);
}
}View on GitHub (pinned to cb02e1d207)
Solutions
- Call setActiveElements only after chart.update()/render completes (metasets built).
- Filter input against chart.data.datasets.length before calling.
- Re-derive indices from the current data after any add/remove of datasets.
- Use chart.getDatasetMeta(i) as a guard: include the entry only when it returns a truthy meta.
Example fix
// before
chart.tooltip.setActiveElements(
[{ datasetIndex: 5, index: 1 }],
{ x: 10, y: 10 }
); // chart has 3 datasets -> throws
// after
const active = [{ datasetIndex: 5, index: 1 }].filter(
({ datasetIndex }) => datasetIndex >= 0 && datasetIndex < chart.data.datasets.length
);
if (active.length) chart.tooltip.setActiveElements(active, { x: 10, y: 10 }); Defensive patterns
Strategy: validation
Validate before calling
// Clamp tooltip active elements to valid datasets before calling setActiveElements.
function safeTooltipActive(chart, activeElements, eventPosition) {
const n = chart.data.datasets.length;
const safe = activeElements.filter(
({ datasetIndex, index }) =>
Number.isInteger(datasetIndex) && datasetIndex >= 0 && datasetIndex < n &&
Number.isInteger(index) && index >= 0
);
if (safe.length) chart.tooltip.setActiveElements(safe, eventPosition);
else chart.tooltip.setActiveElements([], eventPosition);
} Type guard
// Validate an active-element descriptor against the current dataset count.
function isValidTooltipActive(e, datasetCount) {
return (
e && typeof e.datasetIndex === 'number' && typeof e.index === 'number' &&
e.datasetIndex >= 0 && e.datasetIndex < datasetCount && e.index >= 0
);
} Prevention
- Only call tooltip.setActiveElements after the chart has rendered (metasets exist).
- Filter by chart.data.datasets.length to stay in range.
- Recompute selection after dataset add/remove operations.
- Persist dataset identity rather than raw indices in saved UI state.
When it happens
Trigger: Calling chart.tooltip.setActiveElements([{datasetIndex: 9, index: 0}], {x, y}) when the chart has fewer datasets; calling it before the first render built metasets; passing an index computed from a previous, larger dataset array after data was replaced.
Common situations: Programmatic tooltip display in tests or demos right after construction; syncing external selection UI to the chart after datasets were filtered; restoring tooltip state from a serialized snapshot whose indices no longer match.
Related errors
- No dataset found at index ${datasetIndex}
- Cannot determine type of '${id}' axis. Please provide 'axis'
AI-assisted analysis of chartjs/Chart.js@cb02e1d207 (2026-08-12).
Data as JSON: /api/errors/95b62a0d905bbae2.
Report an issue: GitHub.