Skip to content
GitHub11 stars

Beautiful charts. Built using Blazix.Charts, a port of Recharts. Copy and paste into your apps.

Bar Chart - Interactive
Showing total visitors for the last 3 months

Introducing Charts. A collection of chart components that you can copy and paste into your apps.

Charts are designed to look great out of the box. They work well with the other components and are fully customizable to fit your project.

Component

We use Blazix.Charts under the hood: a port of Recharts 3 to Blazor, with the same components and props.

We designed the chart component with composition in mind. You build your charts using Blazix.Charts components and only bring in custom components, such as ChartTooltip, when and where you need it.

<ChartContainer Config="_chartConfig">
    <BarChart Data="_data">
        <Bar DataKey="value" />
        <ChartTooltip>
            <Content>
                <ChartTooltipContent Props="context" />
            </Content>
        </ChartTooltip>
    </BarChart>
</ChartContainer>

We do not wrap Blazix.Charts. This means you're not locked into an abstraction: anything Recharts can do with a component, you can do with its Blazix.Charts counterpart.

Layout runs in C#. A small JavaScript module, loaded when the chart becomes interactive, measures text and the container, and drives the tooltip.

So charts need an interactive render mode, as Recharts needs the browser. Prerendered, a chart is empty until it's interactive, and rendered statically it draws nothing. In a static page, give the component that holds the chart a render mode, and the rest of the page can stay static.

The components are yours.

Installation

dnx Blazix.Cli add chart

Your First Chart

Let's build your first chart. We'll build a bar chart, add a grid, axis, tooltip and legend.

Start by defining your data

The following data represents the number of desktop and mobile users for each month.

private sealed record MonthVisitors(string Month, int Desktop, int Mobile);

private readonly MonthVisitors[] _chartData =
[
    new("January", 186, 80),
    new("February", 305, 200),
    new("March", 237, 120),
    new("April", 73, 190),
    new("May", 209, 130),
    new("June", 214, 140),
];

Define your chart config

The chart config holds configuration for the chart. This is where you place human-readable strings, such as labels, icons and color tokens for theming.

private readonly ChartConfig _chartConfig = new()
{
    ["desktop"] = new ChartConfigItem { Label = "Desktop", Color = "#2563eb" },
    ["mobile"] = new ChartConfigItem { Label = "Mobile", Color = "#60a5fa" },
};

Build your chart

You can now build your chart using Blazix.Charts components.

@using Blazix.Charts

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">

Add a Grid

Let's add a grid to the chart.

Add the CartesianGrid component to your chart.

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">
    <BarChart Data="_chartData">
        <CartesianGrid Vertical="false" />
        <Bar DataKey="desktop" Fill="var(--color-desktop)" Radius="4" />
        <Bar DataKey="mobile" Fill="var(--color-mobile)" Radius="4" />
    </BarChart>
</ChartContainer>
@using Blazix.Charts

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">

Add an Axis

To add an x-axis to the chart, we'll use the XAxis component.

Add the XAxis component to your chart.

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">
    <BarChart Data="_chartData">
        <CartesianGrid Vertical="false" />
        <XAxis DataKey="month" TickLine="false" TickMargin="10" AxisLine="false"
               TickFormatter="@((value, _) => value?.ToString()?[..3] ?? "")" />
        <Bar DataKey="desktop" Fill="var(--color-desktop)" Radius="4" />
        <Bar DataKey="mobile" Fill="var(--color-mobile)" Radius="4" />
    </BarChart>
</ChartContainer>
@using Blazix.Charts

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">

Add Tooltip

So far we've only used components from Blazix.Charts. They look great out of the box thanks to some customization in the chart component.

To add a tooltip, we'll use the custom ChartTooltip and ChartTooltipContent components from chart. The tooltip passes its state to its Content as context.

Add the components to your chart.

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">
    <BarChart Data="_chartData">
        <CartesianGrid Vertical="false" />
        <XAxis DataKey="month" TickLine="false" TickMargin="10" AxisLine="false"
               TickFormatter="@((value, _) => value?.ToString()?[..3] ?? "")" />
        <ChartTooltip>
            <Content>
                <ChartTooltipContent Props="context" />
            </Content>
        </ChartTooltip>
        <Bar DataKey="desktop" Fill="var(--color-desktop)" Radius="4" />
        <Bar DataKey="mobile" Fill="var(--color-mobile)" Radius="4" />
    </BarChart>
</ChartContainer>
@using Blazix.Charts

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">

Hover to see the tooltips. Easy, right? Two components, and we've got a beautiful tooltip.

Add Legend

We'll do the same for the legend. We'll use the ChartLegend and ChartLegendContent components from chart.

Add the components to your chart.

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">
    <BarChart Data="_chartData">
        <CartesianGrid Vertical="false" />
        <XAxis DataKey="month" TickLine="false" TickMargin="10" AxisLine="false"
               TickFormatter="@((value, _) => value?.ToString()?[..3] ?? "")" />
        <ChartTooltip>
            <Content>
                <ChartTooltipContent Props="context" />
            </Content>
        </ChartTooltip>
        <ChartLegend>
            <Content>
                <ChartLegendContent Props="context" />
            </Content>
        </ChartLegend>
        <Bar DataKey="desktop" Fill="var(--color-desktop)" Radius="4" />
        <Bar DataKey="mobile" Fill="var(--color-mobile)" Radius="4" />
    </BarChart>
</ChartContainer>
@using Blazix.Charts

<ChartContainer Config="_chartConfig" Class="min-h-[200px] w-full">

Done. You've built your first chart! What's next?

Chart Config

The chart config is where you define the labels, icons and colors for a chart.

It is intentionally decoupled from chart data.

This allows you to share config and color tokens between charts. It can also work independently for cases where your data or color tokens live remotely or in a different format.

private readonly ChartConfig _chartConfig = new()
{
    ["desktop"] = new()
    {
        Label = "Desktop",
        Icon = @<Icon Data="LucideIcons.Monitor" />,
        // A color like "hsl(220, 98%, 61%)" or "var(--color-name)"
        Color = "#2563eb",
        // OR a color for each theme
        LightColor = "#2563eb",
        DarkColor = "#dc2626",
    },
};

Theming

Charts have built-in support for theming. You can use css variables (recommended) or color values in any color format, such as hex, hsl or oklch.

CSS Variables

Define your colors in your css file

@layer base {
  :root {
    --chart-1: oklch(0.646 0.222 41.116);
    --chart-2: oklch(0.6 0.118 184.704);
  }

  .dark {
    --chart-1: oklch(0.488 0.243 264.376);
    --chart-2: oklch(0.696 0.17 162.48);
  }
}

Add the color to your ChartConfig

private readonly ChartConfig _chartConfig = new()
{
    ["desktop"] = new ChartConfigItem { Label = "Desktop", Color = "var(--chart-1)" },
    ["mobile"] = new ChartConfigItem { Label = "Mobile", Color = "var(--chart-2)" },
};

hex, hsl or oklch

You can also define your colors directly in the chart config. Use the color format you prefer.

private readonly ChartConfig _chartConfig = new()
{
    ["desktop"] = new ChartConfigItem { Label = "Desktop", Color = "#2563eb" },
    ["mobile"] = new ChartConfigItem { Label = "Mobile", Color = "hsl(220, 98%, 61%)" },
    ["tablet"] = new ChartConfigItem { Label = "Tablet", Color = "oklch(0.5 0.2 240)" },
    ["laptop"] = new ChartConfigItem { Label = "Laptop", Color = "var(--chart-2)" },
};

Using Colors

To use the theme colors in your chart, reference the colors using the format var(--color-KEY).

Components

<Bar DataKey="desktop" Fill="var(--color-desktop)" />

Chart Data

private readonly BrowserVisitors[] _chartData =
[
    new("chrome", 275, "var(--color-chrome)"),
    new("safari", 200, "var(--color-safari)"),
];

Tailwind

<LabelList Class="fill-(--color-desktop)" />

Tooltip

A chart tooltip contains a label, name, indicator and value. You can use a combination of these to customize your tooltip.

Label
Page Views
Desktop
186
Mobile
80
Name
Chrome
1,286
Firefox
1,000
Page Views
Desktop
12,486
Indicator
Chrome
1,286

You can turn on/off any of these using the HideLabel, HideIndicator parameters and customize the indicator style using the Indicator parameter.

Use LabelKey and NameKey to use a custom key for the tooltip label and name.

Chart comes with the ChartTooltip and ChartTooltipContent components. You can use these two components to add custom tooltips to your chart.

<ChartTooltip>
    <Content>
        <ChartTooltipContent Props="context" />
    </Content>
</ChartTooltip>

Parameters

Use the following parameters to customize the tooltip.

ParameterTypeDescription
LabelKeystringThe config or data key to use for the label.
NameKeystringThe config or data key to use for the name.
IndicatorDot Line or DashedThe indicator style for the tooltip.
HideLabelboolWhether to hide the label.
HideIndicatorboolWhether to hide the indicator.

Colors

Colors are automatically referenced from the chart config.

Custom

To use a custom key for tooltip label and names, use the LabelKey and NameKey parameters.

private readonly BrowserVisitors[] _chartData =
[
    new("chrome", 187, "var(--color-chrome)"),
    new("safari", 200, "var(--color-safari)"),
];

private readonly ChartConfig _chartConfig = new()
{
    ["visitors"] = new ChartConfigItem { Label = "Total Visitors" },
    ["chrome"] = new ChartConfigItem { Label = "Chrome", Color = "var(--chart-1)" },
    ["safari"] = new ChartConfigItem { Label = "Safari", Color = "var(--chart-2)" },
};
<ChartTooltip>
    <Content>
        <ChartTooltipContent Props="context" LabelKey="visitors" NameKey="browser" />
    </Content>
</ChartTooltip>

This will use Total Visitors for label and Chrome and Safari for the tooltip names.

Legend

You can use the custom ChartLegend and ChartLegendContent components to add a legend to your chart.

<ChartLegend>
    <Content>
        <ChartLegendContent Props="context" />
    </Content>
</ChartLegend>

Colors

Colors are automatically referenced from the chart config.

Custom

To use a custom key for legend names, use the NameKey parameter.

private readonly BrowserVisitors[] _chartData =
[
    new("chrome", 187, "var(--color-chrome)"),
    new("safari", 200, "var(--color-safari)"),
];

private readonly ChartConfig _chartConfig = new()
{
    ["chrome"] = new ChartConfigItem { Label = "Chrome", Color = "var(--chart-1)" },
    ["safari"] = new ChartConfigItem { Label = "Safari", Color = "var(--chart-2)" },
};
<ChartLegend>
    <Content>
        <ChartLegendContent Props="context" NameKey="browser" />
    </Content>
</ChartLegend>

This will use Chrome and Safari for the legend names.

Accessibility

Charts have an accessibility layer, on by default: the chart is focusable, the arrow keys move the tooltip between data points, and screen readers announce it. Turn it off with AccessibilityLayer="false".

<LineChart Data="_chartData" AccessibilityLayer="true" />

RTL

To enable RTL support in Blazix, see the RTL configuration guide.

Translated with AI for the demo, so it may have mistakes.

@using Blazix.Charts

<ChartContainer Config="ChartConfigFor(Language)" Class="min-h-[200px] w-full">

API Reference

ChartContainer

ParameterTypeDefault
Configrequired ChartConfig -
InitialDimension ChartSize -
Id string? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

ChartStyle

ParameterTypeDefault
Idrequired string -
Configrequired ChartConfig -

ChartTooltipContent

ParameterTypeDefault
Propsrequired TooltipContentContext? -
HideLabel bool false
HideIndicator bool false
Indicator TooltipIndicator TooltipIndicator.Dot
NameKey string? -
LabelKey string? -
LabelFormatter Func<object?, IReadOnlyList<TooltipPayloadEntry>, string?>? -
Formatter RenderFragment<ChartTooltipItem>? -
Color string? -
Class string? -
LabelClass string? -

ChartLegendContent

ParameterTypeDefault
Propsrequired LegendContentContext? -
HideIcon bool false
NameKey string? -
Class string? -