mermaid-js/mermaid · error · Error
"${label}" has invalid value: ${value}. Negative values are
Error message
"${label}" has invalid value: ${value}. Negative values are not allowed in pie charts. All slice values must be >= 0. What it means
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.
Source
Thrown at packages/mermaid/src/diagrams/pie/pieDb.ts:38
showData: false,
config: DEFAULT_PIE_CONFIG,
} as const;
let sections: Sections = DEFAULT_PIE_DB.sections;
let showData: boolean = DEFAULT_PIE_DB.showData;
const config: Required<PieDiagramConfig> = structuredClone(DEFAULT_PIE_CONFIG);
const getConfig = (): Required<PieDiagramConfig> => structuredClone(config);
const clear = (): void => {
sections = new Map();
showData = DEFAULT_PIE_DB.showData;
commonClear();
};
const addSection = ({ label, value }: D3Section): void => {
if (value < 0) {
throw new Error(
`"${label}" has invalid value: ${value}. Negative values are not allowed in pie charts. All slice values must be >= 0.`
);
}
if (!sections.has(label)) {
sections.set(label, value);
log.debug(`added new section: ${label}, with value: ${value}`);
}
};
const getSections = (): Sections => sections;
const setShowData = (toggle: boolean): void => {
showData = toggle;
};
const getShowData = (): boolean => showData;
export const db: PieDB = {View on GitHub (pinned to d93e9c88c0)
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.
Example fix
// before pie title Sales "Returns" : -12 "New" : 50 // after pie title Sales "Returns" : 12 "New" : 50
Defensive patterns
Strategy: validation
Validate before calling
function sanitizePieValue(label: string, value: number): number {
if (value < 0) {
throw new Error(`Slice '${label}' has negative value ${value}; pie values must be >= 0`);
}
return value;
} Type guard
function isNonNegative(value: number): boolean {
return Number.isFinite(value) && value >= 0;
} Try / catch
try {
await mermaid.render('g', diagramText);
} catch (e) {
if (e instanceof Error && /Negative values are not allowed in pie charts/.test(e.message)) {
showUserError('Pie chart slices cannot have negative values. Use a non-negative number or a different chart type.');
} else throw e;
} Prevention
- 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.
When it happens
Trigger: Writing a pie diagram line with a negative number, e.g. 'pie title ... "Label" : -5'; feeding computed values where a subtraction underflows; CSV/data import that includes negatives.
Common situations: Data pipeline emitting negative deltas; author misunderstanding that pie values must be non-negative; typos entering a minus sign before a value.
Related errors
- No such shape: ${doc.shape}. Shape names should be lowercase
- Packet block ${start} - ${end} is invalid. End must be great
- Packet block ${start} - ${end ?? start} is not contiguous. I
- Packet block ${start} is invalid. Cannot have a zero bit fie
- value for ${style} ${value} is invalid, please use a valid n
AI-assisted analysis of mermaid-js/mermaid@d93e9c88c0 (2026-08-12).
Data as JSON: /api/errors/9a07128e84f96fcc.
Report an issue: GitHub.