{"record":{"id":"95b62a0d905bbae2","repo":"chartjs/Chart.js","slug":"cannot-find-a-dataset-at-index-datasetindex","errorCode":null,"errorMessage":"Cannot find a dataset at index ${datasetIndex}","messagePattern":"Cannot find a dataset at index (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/plugins/plugin.tooltip.js","lineNumber":1112,"sourceCode":"\t * Get active elements in the tooltip\n\t * @returns {Array} Array of elements that are active in the tooltip\n\t */\n  getActiveElements() {\n    return this._active || [];\n  }\n\n  /**\n\t * Set active elements in the tooltip\n\t * @param {array} activeElements Array of active datasetIndex/index pairs.\n\t * @param {object} eventPosition Synthetic event position used in positioning\n\t */\n  setActiveElements(activeElements, eventPosition) {\n    const lastActive = this._active;\n    const active = activeElements.map(({datasetIndex, index}) => {\n      const meta = this.chart.getDatasetMeta(datasetIndex);\n\n      if (!meta) {\n        throw new Error('Cannot find a dataset at index ' + datasetIndex);\n      }\n\n      return {\n        datasetIndex,\n        element: meta.data[index],\n        index,\n      };\n    });\n    const changed = !_elementsEqual(lastActive, active);\n    const positionChanged = this._positionChanged(active, eventPosition);\n\n    if (changed || positionChanged) {\n      this._active = active;\n      this._eventPosition = eventPosition;\n      this._ignoreReplayEvents = true;\n      this.update(true);\n    }\n  }","sourceCodeStart":1094,"sourceCodeEnd":1130,"githubUrl":"https://github.com/chartjs/Chart.js/blob/cb02e1d207bd4c4c40b20c259017f85f26f1e30a/src/plugins/plugin.tooltip.js#L1094-L1130","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nchart.tooltip.setActiveElements(\n  [{ datasetIndex: 5, index: 1 }],\n  { x: 10, y: 10 }\n); // chart has 3 datasets -> throws\n\n// after\nconst active = [{ datasetIndex: 5, index: 1 }].filter(\n  ({ datasetIndex }) => datasetIndex >= 0 && datasetIndex < chart.data.datasets.length\n);\nif (active.length) chart.tooltip.setActiveElements(active, { x: 10, y: 10 });","handlingStrategy":"validation","validationCode":"// Clamp tooltip active elements to valid datasets before calling setActiveElements.\nfunction safeTooltipActive(chart, activeElements, eventPosition) {\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.tooltip.setActiveElements(safe, eventPosition);\n  else chart.tooltip.setActiveElements([], eventPosition);\n}","typeGuard":"// Validate an active-element descriptor against the current dataset count.\nfunction isValidTooltipActive(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":["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."],"tags":["tooltip","interaction","validation","dataset"],"backgroundTag":null,"analyzedSha":"cb02e1d207bd4c4c40b20c259017f85f26f1e30a","analyzedAt":"2026-08-12T23:38:10.965Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}