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

  1. Replace the negative value with a non-negative number (>= 0).
  2. If negatives are semantically meaningful (e.g. net change), choose a different chart type — pie cannot represent them.
  3. Sanitize imported data by clamping negatives to 0 or filtering them out before rendering.
  4. 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

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


AI-assisted analysis of mermaid-js/mermaid@d93e9c88c0 (2026-08-12). Data as JSON: /api/errors/9a07128e84f96fcc. Report an issue: GitHub.