chart.js Module Documentation

Bahasa Indonesia

chart.js draws bar, line, and donut charts as SVG, written from scratch with no other library and no dependencies. Note: despite the file name chart.js, this module is not Chart.js and its API is not compatible with Chart.js.

Included:

Usage (Import)

import { createChart, niceScale } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/chart.js";

Minimal Example

<h2>Sign-ups per month</h2>
<div id="chart"></div>
<script type="module" src="chart-page.js"></script>
// chart-page.js
import { createChart } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/chart.js";

const chart = createChart("chart", {
    type: "bar",
    title: "Sign-ups per month",
    labels: ["Jan", "Feb", "Mar", "Apr"],
    series: [
        { name: "Bachelor", values: [120, 135, 160, 150] },
        { name: "Diploma", values: [80, 95, 90, 110] }
    ]
});

createChart(id, config)

Creates a chart inside element id (usually an empty <div>). If that element already holds a chart, the old one is removed first. Returns { element, update(changes), remove() }, or null (with a console warning) if the element does not exist.

Option Description
type "bar" (default), "line", or "donut"
labels Array of labels, one per bar/point/slice
series Array of { name, values }. values lines up with labels; null means no data (bar not drawn, line broken, table shows “–”). Donut uses only the first series
title Chart name for screen readers (SVG <title> and table caption). Not displayed; write the visible heading in your page, e.g. an <h2>
description Description for screen readers. If omitted, one is generated (type, number of labels and series, axis range or donut percentages, how to read values with the arrow keys)
stacked true to stack the bars of several series instead of placing them side by side. "bar" only
format Function number => text for the tooltip, table, axis, and donut total. Defaults to Intl.NumberFormat("id-ID")
colors Array of CSS colors replacing the built-in palette, used in order per series (donut: per slice)
height Chart height in pixels. Default 300

update(changes)

Replaces some options and redraws in the same container and SVG, e.g. new data from a server:

import { getJSON } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/api.js";

const { status, data } = await getJSON("https://api.example.com/stats", null);
if (status === 200) chart.update({ labels: data.months, series: data.series });

changes can hold any option from the table above, e.g. chart.update({ stacked: true }) or chart.update({ type: "line" }).

remove()

Removes the chart with its legend, table, and size observer. The container element stays.

Chart Types

A bar or line chart with more series than colors throws an Error. Merge series, split into several charts, or pass a longer colors option.

Size Follows the Container

The chart width always equals its container’s width. chart.js watches the container with ResizeObserver and redraws the SVG in real pixels whenever the width changes. This was chosen over a scaled viewBox because with viewBox the text shrinks on phones, whereas redrawing keeps text at 12 px and adapts the number of axis labels to the width (labels that do not fit are skipped). The height is fixed, from the height option.

A hidden container (display: none, width 0) is drawn once it becomes visible.

Colors and Appearance

The built-in palette has eight colors, used in order (never repeated), through CSS variables --croot-chart-1 to --croot-chart-8 on the .crootjs-chart element. Every color is at least 3:1 against croot.css backgrounds (--croot-surface and --croot-bg) in both light and dark mode, and the order was chosen so neighboring colors stay distinguishable for color-blind readers. Dark mode follows croot.css: the device setting, or data-theme="dark" / "light" on <html>.

Replacing the palette from your page CSS (applies to both modes unless you write your own dark-mode rule):

.crootjs-chart {
    --croot-chart-1: #1d4ed8;
    --croot-chart-2: #b45309;
}

Or per chart with the colors: ["#1d4ed8", "#b45309"] option.

Text, gridlines, gaps between segments, and the tooltip use croot.css variables (--croot-text, --croot-muted, --croot-border, --croot-surface, --croot-focus, --croot-font) with fallback values, so it works without croot.css. The 2 px gap between stacked segments and donut slices is --croot-surface, so place the chart on that background (e.g. inside a .card).

The default styles are injected at the start of <head>, so your page CSS wins without !important. Classes you can override: .crootjs-chart, .crootjs-chart-legend, .crootjs-chart-tooltip, .crootjs-chart-text, .crootjs-chart-grid.

Accessibility

niceScale(min, max, maxTicks = 5)

The scale function used by the axes, exported for your own use (e.g. a hand-made chart). Returns { min, max, step, ticks } with round numbers covering min..max and at most about maxTicks + 1 lines.

niceScale(0, 980);        // { min: 0, max: 1000, step: 200, ticks: [0, 200, 400, 600, 800, 1000] }
niceScale(24.3, 31.8, 4); // { min: 24, max: 32, step: 2, ticks: [24, 26, 28, 30, 32] }

Notes