{"record":{"id":"349da5e806899f56","repo":"chartjs/Chart.js","slug":"no-dataset-found-at-index-datasetindex","errorCode":null,"errorMessage":"No dataset found at index ${datasetIndex}","messagePattern":"No dataset found at index (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/core.controller.js","lineNumber":1101,"sourceCode":"\n  /**\n\t * Get active (hovered) elements\n\t * @returns array\n\t */\n  getActiveElements() {\n    return this._active || [];\n  }\n\n  /**\n\t * Set active (hovered) elements\n\t * @param {array} activeElements New active data points\n\t */\n  setActiveElements(activeElements) {\n    const lastActive = this._active || [];\n    const active = activeElements.map(({datasetIndex, index}) => {\n      const meta = this.getDatasetMeta(datasetIndex);\n      if (!meta) {\n        throw new Error('No dataset found at index ' + datasetIndex);\n      }\n\n      return {\n        datasetIndex,\n        element: meta.data[index],\n        index,\n      };\n    });\n    const changed = !_elementsEqual(active, lastActive);\n\n    if (changed) {\n      this._active = active;\n      // Make sure we don't use the previous mouse event to override the active elements in update.\n      this._lastEvent = null;\n      this._updateHoverStyles(active, lastActive);\n    }\n  }\n","sourceCodeStart":1083,"sourceCodeEnd":1119,"githubUrl":"https://github.com/chartjs/Chart.js/blob/cb02e1d207bd4c4c40b20c259017f85f26f1e30a/src/core/core.controller.js#L1083-L1119","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nchart.setActiveElements([{ datasetIndex: 3, index: 2 }]); // chart has only 2 datasets -> throws\n\n// after\nconst safe = [{ datasetIndex: 3, index: 2 }].filter(\n  ({ datasetIndex }) => datasetIndex >= 0 && datasetIndex < chart.data.datasets.length\n);\nchart.setActiveElements(safe);","handlingStrategy":"validation","validationCode":"// Clamp/filter active elements to valid dataset indices before calling setActiveElements.\nfunction safeSetActiveElements(chart, activeElements) {\n  const n = chart.data.datasets.length;\n  const safe = activeElements.filter(\n    ({ datasetIndex, index }) =>\n      Number.isInteger(datasetIndex) && datasetIndex >= 0 && datasetIndex < n &&\n      Number.isInteger(index) && index >= 0\n  );\n  if (safe.length) chart.setActiveElements(safe);\n  else chart.setActiveElements([]);\n}","typeGuard":"// Narrow to well-formed active-element entries.\nfunction isValidActiveElement(e, datasetCount) {\n  return (\n    e && typeof e.datasetIndex === 'number' && typeof e.index === 'number' &&\n    e.datasetIndex >= 0 && e.datasetIndex < datasetCount && e.index >= 0\n  );\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["active-elements","interaction","validation","dataset"],"backgroundTag":null,"analyzedSha":"cb02e1d207bd4c4c40b20c259017f85f26f1e30a","analyzedAt":"2026-08-12T23:38:10.965Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}