{"record":{"id":"9a07128e84f96fcc","repo":"mermaid-js/mermaid","slug":"label-has-invalid-value-value-negative-v","errorCode":null,"errorMessage":"\"${label}\" has invalid value: ${value}. Negative values are not allowed in pie charts. All slice values must be >= 0.","messagePattern":"\"(.+?)\" has invalid value: (.+?)\\. Negative values are not allowed in pie charts\\. All slice values must be >= 0\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mermaid/src/diagrams/pie/pieDb.ts","lineNumber":38,"sourceCode":"  showData: false,\n  config: DEFAULT_PIE_CONFIG,\n} as const;\n\nlet sections: Sections = DEFAULT_PIE_DB.sections;\nlet showData: boolean = DEFAULT_PIE_DB.showData;\nconst config: Required<PieDiagramConfig> = structuredClone(DEFAULT_PIE_CONFIG);\n\nconst getConfig = (): Required<PieDiagramConfig> => structuredClone(config);\n\nconst clear = (): void => {\n  sections = new Map();\n  showData = DEFAULT_PIE_DB.showData;\n  commonClear();\n};\n\nconst addSection = ({ label, value }: D3Section): void => {\n  if (value < 0) {\n    throw new Error(\n      `\"${label}\" has invalid value: ${value}. Negative values are not allowed in pie charts. All slice values must be >= 0.`\n    );\n  }\n  if (!sections.has(label)) {\n    sections.set(label, value);\n    log.debug(`added new section: ${label}, with value: ${value}`);\n  }\n};\n\nconst getSections = (): Sections => sections;\n\nconst setShowData = (toggle: boolean): void => {\n  showData = toggle;\n};\n\nconst getShowData = (): boolean => showData;\n\nexport const db: PieDB = {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid/src/diagrams/pie/pieDb.ts#L20-L56","documentation":"Thrown by addSection in the pie db when a slice's value is less than zero. Pie chart slices represent non-negative quantities (counts, percentages); a negative slice has no meaningful geometry and is rejected before insertion into the sections map. The check fires on any negative numeric value regardless of label.","triggerScenarios":"Writing a pie diagram line with a negative number, e.g. 'pie title ... \n \"Label\" : -5'; feeding computed values where a subtraction underflows; CSV/data import that includes negatives.","commonSituations":"Data pipeline emitting negative deltas; author misunderstanding that pie values must be non-negative; typos entering a minus sign before a value.","solutions":["Replace the negative value with a non-negative number (>= 0).","If negatives are semantically meaningful (e.g. net change), choose a different chart type — pie cannot represent them.","Sanitize imported data by clamping negatives to 0 or filtering them out before rendering.","Validate the value in your data layer before passing it to mermaid."],"exampleFix":"// before\npie title Sales\n  \"Returns\" : -12\n  \"New\" : 50\n\n// after\npie title Sales\n  \"Returns\" : 12\n  \"New\" : 50","handlingStrategy":"validation","validationCode":"function sanitizePieValue(label: string, value: number): number {\n  if (value < 0) {\n    throw new Error(`Slice '${label}' has negative value ${value}; pie values must be >= 0`);\n  }\n  return value;\n}","typeGuard":"function isNonNegative(value: number): boolean {\n  return Number.isFinite(value) && value >= 0;\n}","tryCatchPattern":"try {\n  await mermaid.render('g', diagramText);\n} catch (e) {\n  if (e instanceof Error && /Negative values are not allowed in pie charts/.test(e.message)) {\n    showUserError('Pie chart slices cannot have negative values. Use a non-negative number or a different chart type.');\n  } else throw e;\n}","preventionTips":["Validate data values are >= 0 before building the pie diagram text.","Filter or clamp negatives in data pipelines that feed mermaid.","Choose a bar chart if negatives are semantically required."],"tags":["mermaid","pie","parsing","validation"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}