Chart
A minimal svg chart: lines, areas, bars, pie, donut and radar, with axes, a legend and a click on the points. Its colors are CSS variables.
import { Chart } from "x4js"
Radar
An axis for each label, from 0 at the center; each serie is a closed line, tinted inside. fill: "none" draws the line only, yaxis.max fixes the outer ring.
export function radar( ) {
const labels = [ "Speed", "Power", "Range", "Comfort", "Safety", "Price" ];
return new HBox( { content: [
new Chart( { width: 320, height: 260, type: "radar", labels,
series: [
{ name: "Model A", values: [ 8, 6, 7, 4, 9, 5 ] },
{ name: "Model B", values: [ 5, 8, 4, 7, 6, 6 ], dots: true },
],
} ),
new Chart( { width: 280, height: 260, type: "radar", labels: [ "Mon", "Tue", "Wed", "Thu", "Fri" ],
series: [ { name: "Load", values: [ 80, 45, 95, 60, 70 ], fill: "none", dots: true } ],
yaxis: { max: 100, step: 25 },
formatter: ( value ) => value + "%",
} ),
] } );
}
Lines
Each serie is a line; with two named series a legend is shown; dots shows every point, undefined leaves a hole.
export function lines( ) {
return new Chart( { height: 240,
labels: [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ],
series: [
{ name: "Visits", values: [ 120, 180, 150, 210, undefined, 90, 60 ], dots: true },
{ name: "Orders", values: [ 20, 35, 28, 44, 39, 18, 9 ] },
],
} );
}
Area and axes
An area under the line; the axes get a name, the y axis its bounds.
export function area( ) {
return new Chart( { height: 240, type: "area",
labels: [ "0h", "4h", "8h", "12h", "16h", "20h", "24h" ],
series: [ { name: "Temperature", values: [ 11, 9, 13, 21, 24, 18, 13 ] } ],
xaxis: { name: "Hour" },
yaxis: { name: "°C", min: 0, max: 30 },
formatter: ( value ) => value + "°",
} );
}
Stacked bars
Bars side by side, or stacked: each serie on the one before.
export function bars( ) {
return new Chart( { height: 240, type: "bar", stacked: true,
labels: [ "Q1", "Q2", "Q3", "Q4" ],
series: [
{ name: "Hardware", values: [ 40, 52, 48, 61 ] },
{ name: "Software", values: [ 25, 30, 41, 45 ] },
{ name: "Services", values: [ 12, 18, 15, 22 ] },
],
} );
}
Pie and donut
Both use the first serie: a slice per value, named by its label.
export function donut( ) {
const labels = [ "Chrome", "Safari", "Firefox", "Edge", "Other" ];
const shares = [ { values: [ 64, 19, 4, 5, 8 ] } ];
return new HBox( { content: [
new Chart( { width: 300, height: 220, type: "pie", labels, series: shares } ),
new Chart( { width: 300, height: 220, type: "donut", labels, series: shares } ),
] } );
}
Click and new data
click gives the serie, the index and the value of the point; setSeries draws other values.
export function click( ) {
const label = new Label( { text: "Click a bar" } );
const first = [ { name: "Rain (mm)", values: [ 52, 41, 48, 60, 72, 45 ] } ];
const second = [ { name: "Rain (mm)", values: [ 30, 66, 58, 39, 41, 80 ] } ];
const chart = new Chart( { height: 220, type: "bar", labels: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun" ], series: first,
click: ( ev ) => label.setText( `serie ${ev.serie}, point ${ev.index}: ${ev.value} mm` ),
} );
return new VBox( { content: [
chart,
new HBox( { content: [
new Button( { label: "2025", cls: "outline", click: ( ) => chart.setSeries( first ) } ),
new Button( { label: "2026", cls: "outline", click: ( ) => chart.setSeries( second ) } ),
label,
] } ),
] } );
}
For an assistant: llms.txt, llms-full.txt.