chartjs/Chart.js · error · Error
No dataset found at index ${datasetIndex}
Error message
No dataset found at index ${datasetIndex} What it means
Chart.setActiveElements maps each {datasetIndex, index} to a meta via getDatasetMeta(datasetIndex). getDatasetMeta returns the dataset metadata only if the index is within the currently built _metasets; otherwise it returns undefined. When the caller passes a datasetIndex that was never built (out of range or before the first update), meta is falsy and this error is thrown. This is a public API for programmatically setting hovered/active points.
Source
Thrown at src/core/core.controller.js:1101
/**
* Get active (hovered) elements
* @returns array
*/
getActiveElements() {
return this._active || [];
}
/**
* Set active (hovered) elements
* @param {array} activeElements New active data points
*/
setActiveElements(activeElements) {
const lastActive = this._active || [];
const active = activeElements.map(({datasetIndex, index}) => {
const meta = this.getDatasetMeta(datasetIndex);
if (!meta) {
throw new Error('No dataset found at index ' + datasetIndex);
}
return {
datasetIndex,
element: meta.data[index],
index,
};
});
const changed = !_elementsEqual(active, lastActive);
if (changed) {
this._active = active;
// Make sure we don't use the previous mouse event to override the active elements in update.
this._lastEvent = null;
this._updateHoverStyles(active, lastActive);
}
}
View on GitHub (pinned to cb02e1d207)
Solutions
- Ensure the chart has fully rendered (await next frame / chart.update()) before calling setActiveElements.
- Clamp datasetIndex against chart.data.datasets.length before calling: if (i >= 0 && i < chart.data.datasets.length).
- Re-derive the active elements from the current datasets after any data change that removes datasets.
- Guard with chart.getDatasetMeta(i) returning truthy before including the entry.
Example fix
// before
chart.setActiveElements([{ datasetIndex: 3, index: 2 }]); // chart has only 2 datasets -> throws
// after
const safe = [{ datasetIndex: 3, index: 2 }].filter(
({ datasetIndex }) => datasetIndex >= 0 && datasetIndex < chart.data.datasets.length
);
chart.setActiveElements(safe); Defensive patterns
Strategy: validation
Validate before calling
// Clamp/filter active elements to valid dataset indices before calling setActiveElements.
function safeSetActiveElements(chart, activeElements) {
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.setActiveElements(safe);
else chart.setActiveElements([]);
} Type guard
// Narrow to well-formed active-element entries.
function isValidActiveElement(e, datasetCount) {
return (
e && typeof e.datasetIndex === 'number' && typeof e.index === 'number' &&
e.datasetIndex >= 0 && e.datasetIndex < datasetCount && e.index >= 0
);
} Prevention
- Call setActiveElements only after the chart's first update/render completes.
- Recompute indices whenever datasets are added or removed.
- Persist dataset identity, not raw indices, when saving selection state.
- Unit-test selection logic against the smallest dataset you support.
When it happens
Trigger: Calling chart.setActiveElements([{datasetIndex: 5, index: 0}]) when the chart has fewer datasets; calling it before chart.update()/render has built metasets (e.g. synchronously after `new Chart` before the first render frame); passing an index derived from stale data after datasets were removed.
Common situations: Setting active elements in a unit test immediately after construction; syncing selection state from a parent component whose dataset count differs from the chart; restoring saved active state after replacing data with a shorter array.
Related errors
- Cannot find a dataset 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/349da5e806899f56.
Report an issue: GitHub.