Skip to main content
Login

Sign in to access your account

Chart - National Design System

A bar, line, pie or donut chart that the script draws as SVG from your data

Overview

A chart is an empty .nds-chart element that the script fills with an SVG drawing, a legend and a tooltip. You give it the data in one of two ways: data-chart-* attributes, which the script reads at page load, or one NDS.Chart.create() call. Both take the same options.

Pick another component when:

  • one number with a small trend line is the focus: Metric.
  • users must read or compare exact values: Tables.
  • the value is progress toward a target: Progress.

Markup

The JS tab is the create() call that draws the chart. To draw one with no script call, see HTML Attributes under Behavior.

Options
Structure
Line style
Axes
Band
More
Preview
<div id="chart-root" class="nds-chart"></div>

Behavior

HTML Attributes

A chart can start with no script call. Write the data in data-chart-type, data-chart-series and data-chart-labels, and any other option in data-chart-config as JSON. Use it for data that is known when the page is built, and create() for data that changes after load.

<div class="nds-chart" data-chart-type="donut" data-chart-series='[420, 310, 180, 90]' data-chart-labels='["Online", "Mobile app", "Service center", "Phone"]' data-chart-config='{"height": 260, "donut": {"size": 0.6}}'></div>

Crowded Labels

When the x axis labels do not fit side by side, the chart turns them 45 degrees. Set xaxis.labelDecimate to true to show every few labels flat instead, or to a number to show every Nth one. xaxis.labelRotate sets a fixed angle.

<div id="chart-days-root" class="nds-chart"></div>

Built-in Features

  • Auto-initialization

    At page load the script draws every .nds-chart that has a series in its attributes. Call NDS.Chart.init() after you add charts to the page. It draws the new ones and redraws any chart that was emptied.

  • Responsive Sizing

    The chart fills its container and redraws when the container width changes. It also redraws when dir or lang changes on <html>, so the axes follow the text direction.

  • Line Crosshair

    On a line chart, a vertical line snaps to the nearest point, enlarges its dots, and one tooltip lists every series there. On a touch screen, a tap pins it and a second tap on the same point clears it. A vertical swipe still scrolls the page.

  • Smart Scaling

    The y axis steps are round numbers that cover the data. Values of 1,000 and more are shortened in the language of the browser, such as 1.2K. The legend wraps onto more lines when it runs out of width.

  • CSS Theming

    Colors come from --chart-color-1 to --chart-color-6, and the grid, text and tooltip have their own properties. Set them on the chart or on any parent.

  • Print and Accessibility

    The SVG has role="img" and an aria-label: the chart element's own aria-label, else the chart type. The tooltip is hidden in print, and the hover transitions are off when the user asks for reduced motion.

  • Programmatic Control

    Create, update and destroy a chart from your code. setOptions() merges the new options into the current ones and redraws.

Best Practices

  • Use a bar chart to compare categories or periods, and a line chart for a trend over time.
  • Use a pie or donut only for the parts of a whole, with six slices or fewer. The colors repeat after six.
  • Use a bar chart instead of a pie when the slices are close in size. Bars make small differences easier to see.
  • Set axis titles that name what the numbers count. Without them, only the legend explains the chart.
  • Set dataLabels.format when the values have a unit, such as % or SAR.
  • Mark a normal range or a target with yaxis.band, not with two flat series: a band shows an open range, and it does not join the tooltip.
  • Put a heading above the chart, and the key numbers in text or a table near it. Put an aria-label on the chart element that says what it shows, such as aria-label="Visitors by month, 2026". Without one, the SVG label says only “Bar chart”, “Line chart” or “Chart”. The chart has no keyboard access.
  • Call destroy() before you remove a chart in a single-page app. It removes the chart’s observers and listeners.

API

Chart ships in its own bundle, which the loader injects after first paint. A create() call before the bundle arrives still works, but returns a Promise instead of the instance. To get the instance at once, await NDS.loadBundle('chart') first.

Data Attributes

Attribute Element Effect
data-chart-type .nds-chart bar, line, pie or donut. Missing means bar. The script also writes the type here on every draw, so CSS picks the color order for that type.
data-chart-series .nds-chart The series as JSON, the same value as the series option. Without a series, the script skips the element at page load.
data-chart-labels .nds-chart The labels as JSON, the same value as the labels option.
data-chart-config .nds-chart Any options as JSON. The three attributes above win over the same keys in it.

CSS Custom Properties

Set them on the chart or on any parent.

Property Default Controls
--chart-color-1 to --chart-color-6 shades of the primary color, in an order for each type The series or slice colors. The seventh series uses color 1 again. The colors option wins over them.
--chart-grid-color --colors-neutral-100, --colors-neutral-700 in dark mode The grid lines.
--chart-band-color --colors-alpha-black-10, --colors-alpha-white-10 in dark mode The yaxis.band fill and its legend swatch. Set a status color, such as --background-success-light, when the range means good or bad, or transparent for the dashes alone.
--chart-band-edge-color --border-neutral-primary The dashed line at each end of the yaxis.band, drawn over the series.
--chart-text --text-primary-paragraph The axis labels and titles, and the crosshair.
--chart-label --text-default The values above the bars, the legend and the tooltip text.
--chart-pie-label-1 to --chart-pie-label-6 --text-oncolor-primary, black on slices 3 and 4, and on 5 and 6 in dark mode The percentage on each slice. Change it when you change its slice color.
--chart-dot-fill --colors-base-white The inside of the line dots and the default slice border color.
--chart-area-opacity 0.15 How strong the Area fill is at the top.
--chart-tooltip-bg --background-menu The tooltip background.
--chart-tooltip-border --border-neutral-secondary The tooltip border.
--chart-radius --radius-md The tooltip corner radius.

Tokens

It reads these shared tokens. A change to one also changes other components: see Tokens.

Source: the var() reads in _sass/components/_chart.scss.

TokenPreviewValue
--background-menu
Read by --chart-tooltip-bg

var(--colors-base-white)
Dark mode var(--background-default)
--text-default
Read by --chart-label

var(--colors-base-black)
Dark mode var(--colors-neutral-200)
--text-primary-paragraph
Read by --chart-text

var(--colors-neutral-700)
Dark mode var(--colors-neutral-300)
--text-oncolor-primary
Read by --chart-pie-label-1, --chart-pie-label-2, --chart-pie-label-5, --chart-pie-label-6
var(--colors-base-white)
--border-neutral-primary
Read by --chart-band-edge-color

var(--colors-neutral-300)
Dark mode var(--colors-neutral-600)
--border-neutral-secondary
Read by --chart-tooltip-border

var(--colors-neutral-200)
Dark mode var(--colors-neutral-700)
--border-neutral-tertiary
Read by --chart-grid-color

var(--colors-neutral-100)
Dark mode var(--colors-neutral-750)

Options

Option Default Effect
type 'bar' 'bar', 'line', 'pie' or 'donut'.
series required Bar and line: an array of { name, data }, one per series. Pie and donut: an array of numbers, one per slice. A point is a number: null or a missing point draws as 0, with no gap in the line.
labels 1, 2, …, pie and donut Item 1, Item 2, … Bar and line: the x axis categories. Pie and donut: the slice names.
height 350, pie and donut 300 The height in pixels. A pie or donut is at most this tall.
colors the --chart-color-* properties An array of colors, used in order and repeated.
legend.show true Shows the legend.
legend.position 'top', pie and donut 'bottom' 'top' or 'bottom'.
tooltip.show true Shows the tooltip on hover or tap.
dataLabels.show false, pie and donut true Bar: the value above each bar. Pie and donut: the percentage on each slice wider than 20 degrees.
dataLabels.format none A string is added after the value ('%'). A function v => … returns the whole label. Bar and line only: it also formats the y axis and the tooltip.
direction the computed CSS direction 'ltr' or 'rtl'. Forces the direction of the axes.
padding top 20, bottom 40, sides 20 Bar and line: { top, bottom, left, right } in pixels around the plot. The side with the y axis gets 55 while the axis shows. All 0 draws a chart edge to edge.
grid.show true Bar and line: the horizontal grid lines.
yaxis.show true Bar and line: the value labels.
yaxis.title '' Bar and line: the title beside the y axis.
yaxis.band none Bar and line: a strip behind the series, such as a normal range, with a dashed line at each end it has: { from, to, label }. Leave out from or to for an open range, such as { to: 200 }. The scale grows to show both ends. The label names the strip in the legend, and the chart’s name tells a screen reader its range.
xaxis.show true Bar and line: the category labels.
xaxis.title '' Bar and line: the title under the x axis.
xaxis.labelRotate 'auto' 'auto' turns the labels 45 degrees when they do not fit. A number sets a fixed angle. The angle is mirrored right to left.
xaxis.labelDecimate false true or 'auto' shows only as many labels as fit. A number shows every Nth label.
bar.stacked false Stacks the series in one bar per category.
bar.borderRadius 6 The radius of the bar tops.
bar.gap 0.3 The space between categories, as a share of each category’s width (0 to 1).
line.smooth true Curved lines. false draws straight segments.
line.dots true A dot on each point.
line.dotRadius 4 The dot radius in pixels.
line.width 3 The line width in pixels.
line.area false Fills the space under each line.
line.crosshair true The crosshair and its shared tooltip. false gives each dot its own tooltip.
line.spotlight none 'first', 'last' or an index. Keeps a ring on that point.
donut.size 0.5 The hole, as a share of the radius (0 to 1).
stroke.show false Pie and donut: a border between the slices.
stroke.width 2 The border width.
stroke.color var(--_chart-dot-fill) The border color. The default follows --chart-dot-fill.
startAngle 0 Pie and donut: where the first slice starts, in degrees from the top.

Methods

Method Effect
NDS.Chart.create(el, options) Draws a chart on an element, its id or a selector, and returns the instance. A chart already on the element is returned unchanged: use chart.setOptions(options) to change it, or destroy() first to draw a new one. With no options, it reads the data-chart-* attributes of the element. Returns null when nothing matches, or when those attributes declare no series.
NDS.Chart.destroy(el) The same as chart.destroy(). el is the element, its id or a selector.
NDS.Chart.init() Draws every .nds-chart with a series in its attributes that has no chart yet, and redraws a chart whose element was emptied.
chart.setOptions(options) Merges the options into the current ones and redraws. An array, such as series or labels, replaces the old one.
chart.render() Redraws from the current options.
chart.destroy() Empties the element, removes its observers and listeners, and deletes el.ndsChart.

The instance is also on the element, as el.ndsChart. It is there once the component starts: code that runs at page load waits for NDS.init.ready. Chart fires no events.

const chart = NDS.Chart.create('#requests-chart', { type: 'line', series: [{ name: 'Requests', data: [120, 180, 150, 220] }], labels: ['Q1', 'Q2', 'Q3', 'Q4'], yaxis: { title: 'Requests' }, dataLabels: { format: (v) => v.toLocaleString() } }); chart.setOptions({ series: [{ name: 'Requests', data: [130, 175, 160, 240] }] }); chart.destroy();

The full API is in the banner of _js/nds-chart.js.

Last Modified Date: 09/11/2026 - 08:21 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks