← Files DataARCHIVED FILE

output_formats/html/src/charting/shared/core/chart-presentation.js

5 KB · Nov 27, 2026 · 12:12 UTC

↓ Download file

// Generated by scripts/sync-chart-semantics.mjs from skills/visualize-data/src/core/chart-presentation.js.
// Edit the canonical source and regenerate; do not edit this distribution copy.
import { barPresentations, barPresentationIssue } from "./bar-presentations.js";

const common = ["showXAxisLabel", "showYAxisLabel", "showAnnotations", "baseColor", "tooltipFields", "legend", "stackable"];
const axes = ["xLabelPosition", "xTickLabelLayout", "axisPercentDigits", "showCategoryTicks", "yTickCount"];
const horizontalAxes = axes.filter(key => !["xLabelPosition", "xTickLabelLayout", "yTickCount"].includes(key)).concat(["yLabelPosition", "yTickLabelLayout", "xTickCount"]);
const trend = ["dashedFields", "rightAxisFields", "rightYAxisLabel", "barFields", "lineGradients"];
const ranking = ["rowHeight", "markRadius", "markStartRadius", "initialVisibleCount", "labelMaxLength"];
const bars = [...ranking, "colorBySign"];
const recipes = ["presentation", "barOptions"];
const heatmap = ["rowHeight", "colorScaleStartAtZero", "reverseRows", "colorBands", "cellLabel", "hoverHighlight"];
const sankey = ["sankeyLabelFontSize", "sankeyLinkColorMode", "sankeyNodeWidth", "sankeySortNodes", "sankeyTerminalLabelAlignment", "sankeyValueFontSize", "penultimateLabelAlignment"];
export function presentationOptionKeys(type) {
  return [...common, ...(["line", "area", "stackedArea", "sparkline"].includes(type) ? [...axes, ...trend]
    : type?.startsWith("horizontal") ? [...axes, ...horizontalAxes, ...bars, ...(type === "horizontalBar" ? recipes : [])]
    : type?.toLowerCase().includes("bar") ? [...axes, ...bars, ...(type === "bar" ? recipes : [])]
    : type === "heatmap" ? [...axes, ...heatmap]
    : type === "sankey" ? sankey : type === "scatter" ? axes
    : type === "pie" ? ["groupOther", "maxCategories", "preserveCategories"]
    : ["leaderboard", "rankedList", "funnel"].includes(type) ? ranking : [])];
}

/** Options stay JSON; runtime callbacks never enter this contract. */
export function checkPresentation(spec, rows) {
  const boolean = ["showXAxisLabel", "showYAxisLabel", "showAnnotations", "stackable", "showCategoryTicks", "colorBySign", "colorScaleStartAtZero", "reverseRows", "sankeySortNodes", "groupOther"];
  const positive = ["yTickCount", "rowHeight", "initialVisibleCount", "labelMaxLength", "sankeyLabelFontSize", "sankeyNodeWidth", "sankeyValueFontSize", "maxCategories"];
  const nonnegative = ["axisPercentDigits", "markRadius", "markStartRadius"];
  for (const key of boolean) if (spec[key] != null && typeof spec[key] !== "boolean") throw new Error(`Chart ${key} must be a boolean.`);
  for (const key of [...positive, ...nonnegative]) if (spec[key] != null && (!Number.isFinite(spec[key]) || spec[key] < (positive.includes(key) ? 1 : 0))) throw new Error(`Chart ${key} must be a finite ${positive.includes(key) ? "positive" : "nonnegative"} number.`);
  for (const [key, values] of Object.entries({ xLabelPosition: ["bottom", "top", "inline-right"], xTickLabelLayout: ["auto", "horizontal", "wrapped", "angled", "date-time"], sankeyLinkColorMode: ["source", "target", "gradient"], presentation: barPresentations })) {
    if (spec[key] != null && !values.includes(spec[key])) throw new Error(`Unsupported chart ${key}.`);
  }
  const measures = spec.series ? [...new Set(rows.map(row => row[spec.series]))] : spec.fields ?? [spec.y];
  for (const key of ["dashedFields", "rightAxisFields", "barFields", "preserveCategories"]) {
    if (spec[key] != null && (!Array.isArray(spec[key]) || spec[key].some(value => typeof value !== "string" || !value.trim()) || new Set(spec[key]).size !== spec[key].length)) throw new Error(`Chart ${key} must contain distinct nonblank names.`);
    if (spec[key] && key !== "preserveCategories" && spec[key].some(field => !measures.includes(field))) throw new Error(`Chart ${key} must name plotted measures.`);
  }
  if (spec.lineGradients != null) {
    if (!spec.lineGradients || Array.isArray(spec.lineGradients) || typeof spec.lineGradients !== "object") throw new Error("Chart lineGradients must be a field map.");
    for (const [field, stops] of Object.entries(spec.lineGradients)) {
      if (!measures.includes(field) || !Array.isArray(stops) || stops.length < 2 || stops.some(stop => typeof stop !== "string" && !(stop && typeof stop.color === "string" && Number.isFinite(stop.offset) && stop.offset >= 0 && stop.offset <= 1))) throw new Error("Chart lineGradients require plotted measures and at least two color stops.");
    }
  }
  if (spec.barOptions != null && (!spec.barOptions || typeof spec.barOptions !== "object" || Array.isArray(spec.barOptions))) throw new Error("Chart barOptions must be an object.");
  if (spec.presentation && (spec.presentation !== "plot" || spec.barOptions) &&
      ["referenceLines", "referenceBands", "uncertainty"].some(key => spec[key] != null))
    throw new Error("Bar presentation recipes do not support references or uncertainty; use a standard plot with target context.");
  if (spec.presentation) {
    const issue = barPresentationIssue(rows, spec);
    if (issue) throw new Error(issue);
  }
}

SHA-256: d84985d6683ad0ac3d2021ff7963fde22cf624059d31979299a3145590db9966