Brings Chart.js charts to Blazor
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.
You need an IDE that supports Blazor and .NET Core SDK 3.x+
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
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
bodytag after the
_frameworkreference.
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.
Now add a reference to
ChartJs.Blazorin 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.
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.PieChartto the top of our component.
Then we can add a
Chartcomponent 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
Chartcomponent. We do this in the
@codesection 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.
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.
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.
Client-side Blazor projects are currently affected by a bug in
JSON.NETtracked by this issue. If you run into this issue, use one of these two workarounds:
JSON.NETinvokes via reflection. Make sure to select
BlazorLinkerDescriptionas the build action of the
Linker.xmlfile. In case that your IDE doesn't offer that option, simply edit the
.csprojfile and add this to it:
The content of the
Linker.xmlshould be similar to this (adjust to your project's entry point assembly):
ReferenceConverterconstructor to avoid the linker optimizing it away. Example:
private ReferenceConverter ReferenceConverter = new ReferenceConverter(typeof(Chart));
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.
We really like people helping us with the project. Nevertheless, take your time to read our contributing guidelines here.