Need help with ChartJs.Blazor?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

mariusmuntean
396 Stars 83 Forks MIT License 486 Commits 24 Opened issues

Description

Brings Chart.js charts to Blazor

Services available

!
?

Need anything else?

Contributors list

# 149,669
Angular
HTML
C#
webasse...
172 commits
# 155,692
C#
Shell
HTML
chartjs
159 commits
# 57,640
C#
Azure
C
mqtt-pr...
54 commits
# 281,800
C#
HTML
chartjs
razor
33 commits
# 344,727
C#
Shell
HTML
chartjs
13 commits
# 47,504
c-plus-...
iis
c-sharp
Git
3 commits
# 612,374
C#
HTML
chartjs
razor
1 commit
# 202,399
C#
ioc-con...
autofac
c-sharp
1 commit
# 224,441
materia...
chartjs
razor
F#
1 commit
# 599,280
C#
HTML
chartjs
razor
1 commit
# 612,375
C#
HTML
chartjs
razor
1 commit

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.

If you are using a time scale (

TimeAxis
), you also need to include Moment.js before including Chart.js.

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: -

ChartJs.Blazor.Common
-
ChartJs.Blazor.Common.Axes
-
ChartJs.Blazor.Common.Axes.Ticks
-
ChartJs.Blazor.Common.Enums
-
ChartJs.Blazor.Common.Handlers
-
ChartJs.Blazor.Common.Time
-
ChartJs.Blazor.Util
-
ChartJs.Blazor.Interop

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:

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: - It's simple but depending on your situation it helps to go to the definition of the C#-class you're working with. The XML-docs are usually quite detailed and often contain relevant links. - Browse our samples - You can find a lot of information there. - Browse the official Chart.js docs - This library is just a wrapper, there's a very high chance you can find what you need there. - Browse our issues - Your issue might've already been reported. - If none of that helped, open a new issue and fill out the template with your details. There's a good chance that someone else can help you.

Known Limitations

  • Client-side Blazor projects are currently affected by a bug in

    JSON.NET
    tracked by this issue. If you run into this issue, use one of these two workarounds:
    • Prefered Option - add a file named Linker.xml to the root of your client-side project to instruct the Mono linker to keep a certain constructor that
      JSON.NET
      invokes via reflection. Make sure to select
      BlazorLinkerDescription
      as the build action of the
      Linker.xml
      file. In case that your IDE doesn't offer that option, simply edit the
      .csproj
      file and add this to it:
        
    
    

    The content of the

    Linker.xml
    should be similar to this (adjust to your project's entry point assembly):
        
            
            
        
        
            
            
        
        
        
        
        
            
                
            
        
    
    
    • Alternative Option - manually invoke the
      ReferenceConverter
      constructor to avoid the linker optimizing it away. Example:
    private ReferenceConverter ReferenceConverter = new ReferenceConverter(typeof(Chart));
    

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.

  • This project is currently unmaintained.
  • Owner of the project is Marius Muntean.

Contributing

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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.