For AI agents: the complete documentation index is available at https://a3s-lab.github.io/UI/v0.2.0/en/llms.txt, the full documentation bundle is available at https://a3s-lab.github.io/UI/v0.2.0/en/llms-full.txt, and this page is available as Markdown at https://a3s-lab.github.io/UI/v0.2.0/en/components/chart.md.
  • English
  • v0.2.0
  • Chart

    Use Chart.js with A3S UI defaults for themed colors, external tooltips, and optional generated legends.

    Live previewHTML · CSS · JavaScript

    Usage

    Include CSS

    Import Tailwind and one full A3S UI style bundle.

    @import "tailwindcss";
    @import "@a3s-lab/ui/a3s.css";

    Or import only the base CSS, Chart component CSS, and one style pack.

    @import "tailwindcss";
    @import "@a3s-lab/ui/base.css";
    @import "@a3s-lab/ui/components/chart.css";
    @import "@a3s-lab/ui/styles/a3s.css";

    Using CDN or bundler imports? See the Installation page.

    Include JavaScript

    Copy or serve Chart.js, the A3S UI runtime, and Chart script.

    <script src="/assets/js/chart.umd.min.js"></script>
    <script src="/assets/js/runtime.min.js" defer></script>
    <script src="/assets/js/chart.min.js" defer></script>

    Chart is not included in the full A3S UI JavaScript bundle.

    Using CDN or bundler imports? See the Installation page.

    Add your chart HTML

    <canvas id="visitors-chart" aria-label="Monthly visitors"></canvas>
    <script>
      document.addEventListener("DOMContentLoaded", () => {
        window.a3sUI.chart("#visitors-chart", {
          type: "bar",
          labelKey: "month",
          data: [
            { month: "Jan", desktop: 186, mobile: 80 },
            { month: "Feb", desktop: 305, mobile: 200 },
            { month: "Mar", desktop: 237, mobile: 120 },
            { month: "Apr", desktop: 73, mobile: 190 },
            { month: "May", desktop: 209, mobile: 130 },
            { month: "Jun", desktop: 214, mobile: 140 },
          ],
          series: {
            desktop: { label: "Desktop", color: "var(--chart-1)" },
            mobile: { label: "Mobile", color: "var(--chart-2)" },
          },
        })
      })
    </script>

    a3sUI.chart() targets the <canvas> directly and creates A3S UI's internal .chart container for Chart.js sizing. You do not need to add a wrapper or class in your HTML.

    Charts default to aspect-video. Use a height, min-h-*, or aspect-* utility on a parent element when you need a different measuring box.

    Legend

    Set legend: true to generate a A3S UI-styled legend after the canvas.

    The generated legend is display-only by default, matching shadcn/ui. Use a Chart.js plugin or custom legend markup if you need click-to-toggle behavior.

    window.a3sUI.chart("#visitors-chart", {
      type: "bar",
      labelKey: "month",
      data,
      series,
      legend: true,
    })

    Series

    series maps object keys from each data row to labels, colors, and optional Chart.js dataset settings.

    window.a3sUI.chart("#revenue-chart", {
      type: "line",
      labelKey: "month",
      data,
      series: {
        recurring: {
          label: "Recurring",
          color: "var(--chart-1)",
          surface: "gradient",
          dataset: {
            fill: true,
            tension: 0.35,
          },
        },
        new: {
          label: "New",
          color: "var(--chart-2)",
          dataset: {
            borderDash: [4, 4],
          },
        },
      },
    })

    Escape Hatches

    Pass raw Chart.js options and plugins through options and plugins.

    const chart = window.a3sUI.chart("#visitors-chart", {
      type: "bar",
      labelKey: "month",
      data,
      series,
      options: {
        indexAxis: "x",
        scales: {
          y: {
            beginAtZero: true,
          },
        },
        plugins: {
          tooltip: {
            mode: "index",
          },
        },
      },
      plugins: [myChartJsPlugin],
    })

    Pass complete Chart.js data through chartData when the simplified data plus series mapper is not enough.

    window.a3sUI.chart("#custom-chart", {
      type: "scatter",
      chartData: {
        datasets: [
          {
            label: "Samples",
            data: [{ x: 1, y: 2 }, { x: 2, y: 5 }],
          },
        ],
      },
      options: {
        scales: {
          x: { type: "linear" },
        },
      },
    })

    a3sUI.chart() returns the Chart.js instance for single targets and an array of instances for multi-target selectors.

    Examples

    Bar chart - multiple

    Live previewHTML · CSS · JavaScript

    Line chart - linear

    Live previewHTML · CSS · JavaScript

    Line chart - step

    Live previewHTML · CSS · JavaScript

    Bar chart - stacked

    Live previewHTML · CSS · JavaScript

    Pie chart - donut

    Live previewHTML · CSS · JavaScript

    Radar chart

    Live previewHTML · CSS · JavaScript

    API

    OptionDefaultDescription
    type"bar"Chart.js chart type.
    labelKey"label"Row key used for labels.
    data[]Array of row objects, or raw Chart.js data with datasets.
    series{}Series config mapped from row keys.
    legendfalseGenerates a A3S UI legend after the canvas.
    tooltiptrueUses the A3S UI external tooltip.
    options{}Raw Chart.js options.
    plugins[]Raw Chart.js plugins.
    chartDataundefinedComplete Chart.js data object. Overrides the data mapper.

    Series options

    OptionDescription
    labelDisplay label for legends and tooltips.
    colorBase color for the series stroke, fill, legend marker, and tooltip marker.
    surfaceDerives a fill from color. Use true for a translucent fill, "gradient" for an upstream-style vertical fade, or { from, to } for custom opacity stops.
    datasetRaw Chart.js dataset options.