Journalism Dataviz - v1.0.30
    Preparing search index...

    Function saveChart

    • Saves an Observable Plot chart or map as a PNG or SVG file. The chart function can return an SVG or HTML element directly or through a promise. The rendering environment remains available until the chart function finishes. Concurrent saves are processed one at a time to protect that environment. Calling saveChart from inside its own chart function is not supported and rejects with an error.

      Type Parameters

      • T

        The type of data passed unchanged to the chart function.

      Parameters

      • data: T

        The data passed to the chart function.

      • chart: (data: T) => SVGSVGElement | HTMLElement | Promise<SVGSVGElement | HTMLElement>

        A synchronous or asynchronous function returning an SVG or HTML element representing the chart or map.

      • path: string

        The output file path. The extension must be .png or .svg.

      • options: { dark?: boolean; style?: string } = {}

        Optional settings to customize the chart's appearance.

        • Optionaldark?: boolean

          If true, renders the chart with a dark theme. Defaults to false.

        • Optionalstyle?: string

          A CSS string inserted into the generated SVG.

      Returns Promise<void>

      A promise that resolves after the file has been saved, or rejects if the chart function or saving fails.

      import { dot, plot } from "@observablehq/plot";

      const data = [{ year: 2024, value: 10 }, { year: 2025, value: 15 }];
      await saveChart(
      data,
      (rows) => plot({ marks: [dot(rows, { x: "year", y: "value" })] }),
      "output/chart.png",
      );
      // Load Observable Plot asynchronously before creating the chart.
      const data = [{ year: 2024, value: 10 }, { year: 2025, value: 15 }];
      await saveChart(
      data,
      async (rows) => {
      const { dot, plot } = await import("@observablehq/plot");
      return plot({ marks: [dot(rows, { x: "year", y: "value" })] });
      },
      "output/chart.svg",
      { dark: true, style: ".chart-title { font-size: 24px; }" },
      );