Home

Awesome

License: MIT GitHub issues GitHub forks GitHub stars

Join the chat at https://gitter.im/ChartJs-Blazor/community

NuGet Downloads (official NuGet)
NuGet Downloads (2.0 NuGet)

Netlify Status

Introduction

This is a Blazor library that wraps Chart.js. You can use it in both client- and server-side projects.

Don't know what Blazor is? Read this.

Getting started

Prerequisites

You need an IDE that supports Blazor and .NET Core SDK 3.x+

  1. Possible IDEs are: Visual Studio 2019 (Community Edition is fine) / VisualStudio for Mac, Jetbrains Rider and more
  2. .NET Core 3.1 or newer

Installation

Due to an unfortunate situation, the new 2.0 release is only available in an alternative NuGet package for the time being.
The original NuGet is ChartJs.Blazor.

Install our NuGet package: ChartJs.Blazor.Fork

You can install it with the Package Manager in your IDE or alternatively using the command line:

dotnet add package ChartJs.Blazor.Fork

Usage

Assets

Before you can start creating a chart, you have to add some static assets to your project.

In your _Host.cshtml (server-side) or in your index.html (client-side) add the following lines to the body tag after the _framework reference.

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js"></script>

<!-- This is the glue between Blazor and Chart.js -->
<script src="_content/ChartJs.Blazor.Fork/ChartJsBlazorInterop.js"></script>

If you are using a time scale (TimeAxis), you also need to include Moment.js before including Chart.js.

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

If you don't want to use CDNs, check out the Chart.js installation page and the Moment.js installation page where you can find alternative ways to install the necessary libraries.

Imports

Now add a reference to ChartJs.Blazor in your _Imports.razor.

@using ChartJs.Blazor;

Other commonly used namespaces which you might want to import globally are:

Apart from that every chart type has a namespace e.g. ChartJs.Blazor.PieChart.

Chart

Now let's create a simple pie chart!

In order to use the classes for a pie chart, we need to add @using ChartJs.Blazor.PieChart to the top of our component.

Then we can add a Chart component anywhere in the markup like so:

<Chart Config="_config"></Chart>

The only thing left to do now is to provide the data and chart configuration by declaring an instance variable which we reference in the Chart component. We do this in the @code section of our component.

private PieConfig _config;

protected override void OnInitialized()
{
    _config = new PieConfig
    {
        Options = new PieOptions
        {
            Responsive = true,
            Title = new OptionsTitle
            {
                Display = true,
                Text = "ChartJs.Blazor Pie Chart"
            }
        }
    };

    foreach (string color in new[] { "Red", "Yellow", "Green", "Blue" })
    {
        _config.Data.Labels.Add(color);
    }

    PieDataset<int> dataset = new PieDataset<int>(new[] { 6, 5, 3, 7 })
    {
        BackgroundColor = new[]
        {
            ColorUtil.ColorHexString(255, 99, 132), // Slice 1 aka "Red"
            ColorUtil.ColorHexString(255, 205, 86), // Slice 2 aka "Yellow"
            ColorUtil.ColorHexString(75, 192, 192), // Slice 3 aka "Green"
            ColorUtil.ColorHexString(54, 162, 235), // Slice 4 aka "Blue"
        }
    };

    _config.Data.Datasets.Add(dataset);
}

This small sample should get you started and introduce you to the most basic concepts used for creating a chart. For more relevant examples, please see our samples.

Samples

Since Version 2.0 we'd like to keep the samples as similar to the official Chart.js samples as possible. Unfortunately, we're not up to date yet and many samples are missing. If you'd like to help, please check out this issue :heart:

To check out the code of the most recent (development version - explained below) samples, go to the ChartJs.Blazor.Samples/Client/Pages folder.

The ChartJs.Blazor.Samples folder contains the projects to showcase the samples. It's based on Suchiman/BlazorDualMode and allows you to switch between the server- and the client-side Blazor mode.

The samples should always be up to date with the current development on master. That means that the code you see on master might not work for your version. To browse the samples for the latest NuGet version, see the samples on the releases branch or select a specific tag. If there's not a sample for your use-case on the releases branch, check out the master one. Maybe someone already contributed what you're looking for and if not, why not do it yourself :wink:

We would usually host the samples on https://www.iheartblazor.com but unfortunately, the version shown there is really old and we highly recommend downloading and running our samples on your machine.

Docs

We can't offer thorough docs at this time.

If you run into an issue, we recommend you to do the following steps:

Known Limitations

Contributors

We thank everyone who has taken their time to open detailed issues, discuss problems with us or contribute code to the repository. Without you, projects like this would be very hard to maintain!

Check out the list of contributors.

Contributing

We really like people helping us with the project. Nevertheless, take your time to read our contributing guidelines here.