Chart

The RzChart component provides a fluent C# API for rendering Chart.js visualizations in SSR workflows.

Looking for bar-specific patterns? See the Chart Bar Examples page.

Basic Usage

Basic chart usage
<RzChart Configure=@(c => c
    .Data(d => d
        .Labels("January", "February", "March", "April", "May", "June", "July")
        .Datasets(ds => ds
            .Line()
            .Label("Monthly Sales")
            .Data(65, 59, 80, 81, 56, 55, 40)
            .BackgroundColor("var(--chart-1)")
            .BorderColor("var(--chart-1)")
            .Tension(0.4)
        )
    )
    .Options(o => o
        .Responsive(true)
        .MaintainAspectRatio(false)
        .Plugins(p => p.Legend(l => l.Position(LegendPosition.Top)))
    )) />

Size, type and theme

A chart fills the height of its parent when the parent has one (h-64, a grid cell, a card with a fixed height), so it never grows past it. In a parent without a height it is half as tall as it is wide, Chart.js's default 2:1 ratio. Override the slots to change either, for example class="h-80" on the chart.

The chart's type comes from its first dataset (.Doughnut(), .Bar(), ...), so Chart.js applies that type's defaults, such as a doughnut legend with one entry per slice. For a mixed chart whose first dataset is not the base type, set it with .Type("bar") on the builder.

Colors given as CSS variables (var(--chart-1)) are resolved again when the theme changes, so a chart follows dark mode without being re-rendered. A chart removed from the page, for example by an htmx swap, destroys its Chart.js instance.