ChartJSCore 3.12.0

dotnet add package ChartJSCore --version 3.12.0                
NuGet\Install-Package ChartJSCore -Version 3.12.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ChartJSCore" Version="3.12.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ChartJSCore --version 3.12.0                
#r "nuget: ChartJSCore, 3.12.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ChartJSCore as a Cake Addin
#addin nuget:?package=ChartJSCore&version=3.12.0

// Install ChartJSCore as a Cake Tool
#tool nuget:?package=ChartJSCore&version=3.12.0                

ChartJSCore v3.9.1

Implementation of Chart.js for use with ASP.NET MVC. This library allows Chart.js code to be generated in an MVC controller from a .NET object and injected into the desired view.

ChartJSCore is a .NET Standard 2.0 library, and is compatible with all .NET versions that support .NET Standard 2.0 libraries.

Build status

This library has been updated to work with v3 of Chart.js, I have done minimal testing so there are likely to be bugs. If something doesn't work as expected please open a new issue with an example of the failing code and alink to the relevant Chart.js documentation.

Installation

To use ChartJSCore in your C# project, you can either download the ChartJSCore C# .NET libraries directly from the Github repository or, if you have the NuGet package manager installed, you can grab them automatically.

PM> Install-Package ChartJSCore

Once you have the ChartJSCore libraries properly referenced in your project, you can include calls to them in your code.

Add the following namespaces to use the library:

using ChartJSCore.Models;

Dependencies

This produces code for generating chart using Chart.js so Chart.js is required to render them, Chart.js also uses require.js so this is also needed. To install Chart.js using Bower on Visual Studio it is needed to update the registry in .bowerrc file, because the old heroku repository is deprecated as stated in here. Otherwise you can download it directly to the project root folder wwwroot\lib\Chart.js.

If you don't feel like using a package manager, Chart.JS can also be linked directly to your page and hosted from cdnjs.

This package has been created and tested with version 3.9.1 of Chart.js, earlier versions may well be incompatible.

Require.js isn't available in the Bower package manager but can be added by directly updating the bower.json file.

{
  "name": "asp.net",
  "private": true,
  "dependencies": {
    "bootstrap": "3.3.7",
    "jquery": "3.1.1",
    "jquery-validation": "1.16.0",
    "jquery-validation-unobtrusive": "3.2.6",
    "requirejs": "2.3.3",
    "chart.js": "2.5.0"
  }
}

Usage

Once a project has been created a new Chart object can be created by using code similar to that below. In this example the Index method of the Home controller has been updated to generate a Chart and pass it through to the relevant view.

        public IActionResult Index()
        {
            Chart chart = new Chart();

            chart.Type = Enums.ChartType.Line;

            ChartJSCore.Models.Data data = new ChartJSCore.Models.Data();
            data.Labels = new List<string>() { "January", "February", "March", "April", "May", "June", "July" };

            LineDataset dataset = new LineDataset()
            {
                Label = "My First dataset",
                Data = new List<double?> { 65, 59, 80, 81, 56, 55, 40 },
                Fill = "false",
                Tension = 0.1,
                BackgroundColor = new List<ChartColor> { ChartColor.FromRgba(75, 192, 192, 0.4) },
                BorderColor = new List<ChartColor> { ChartColor.FromRgb(75,192,192) },
                BorderCapStyle = "butt",
                BorderDash = new List<int> { },
                BorderDashOffset = 0.0,
                BorderJoinStyle = "miter",
                PointBorderColor = new List<ChartColor> { ChartColor.FromRgb(75,192,192) },
                PointBackgroundColor = new List<ChartColor> { ChartColor.FromHexString("#ffffff") },
                PointBorderWidth = new List<int> { 1 },
                PointHoverRadius = new List<int> { 5 },
                PointHoverBackgroundColor = new List<ChartColor> { ChartColor.FromRgb(75,192,192) },
                PointHoverBorderColor = new List<ChartColor> { ChartColor.FromRgb(220,220,220) },
                PointHoverBorderWidth = new List<int> { 2 },
                PointRadius = new List<int> { 1 },
                PointHitRadius = new List<int> { 10 },
                SpanGaps = false
            };

            data.Datasets = new List<Dataset>();
            data.Datasets.Add(dataset);

            chart.Data = data;

            ViewData["chart"] = chart;

            return View();
        }

See the demo project for further examples.

Within the Index view the Chart can then be accessed and rendered.

@{
    ViewData["Title"] = "Home Page";
}

<div><canvas id="lineChart"></canvas></div>

@{ChartJSCore.Models.Chart chart = (ChartJSCore.Models.Chart)ViewData["chart"]; }

@section Scripts {
    <script src="~/lib/Chart.js/dist/Chart.js"></script>
    <script>
        @Html.Raw(chart.CreateChartCode("lineChart"));
    </script>
}

Plugins

ChartJSCore has built-in support for the popular chartjs-plugin-zoom plugin. See the demo project for an example of how to use this.

ChartJSCore als has built-in support for the color plugin that comes as part of ChartJS (https://www.chartjs.org/docs/latest/general/colors.html). This plugin is enabled by default within ChartJS and the plug-in allows to disable it using:

Options = new Options()
{
    Plugins = new Plugins()
    {                        
        Colors = new ColorPlugin()
        {
            Enabled = false // enabled by default
        },

You can also force using the pre-defined color scheme using:

Options = new Options()
{
    Plugins = new Plugins()
    {                        
        Colors = new ColorPlugin()
        {
            ForceOverride = true   // disabled by default
        },

Generally, the option 'ForceOverride' is the option you want to set for proper coloring of your datasets.

As there's a large number of plugins available for Chart.js and it's not feasible to create object representations of them all, I've added the "PluginDynamic" property to all chart objects to allow customisation. This is a Dictionary<string, object> in which the string is the name of the property and the object contains the object to be serialized, these will be added to the parent object as distinct properties.

DataLabels plugin

You can easily integrate the DataLabels plugin (https://chartjs-plugin-datalabels.netlify.app/) using this code:

DataLabelsPlugin dataLabels = new DataLabelsPlugin()
{
  /// set properties
};

Options = new Options()
{
    PluginDynamic = new Dictionary<string, object>()
{ 
    { 
        "plugins",
        new Dictionary<string, object>()
        {
            { "datalabels",
              dataLabels
            }
        }
    } 
}

Annotation plugin

You can easily integrate the Annotation plugin (https://chartjs-plugin-datalabels.netlify.app/) using this code:

AnnotationPlugin annotation = new AnnotationPlugin()
{
Annotations = new Dictionary<string, ChartJSCore.Plugins.Annotation.Annotation>()
{
    {
        "line1",
        new ChartJSCore.Plugins.Annotation.LineAnnotation()
        {
            /// set properties
        }
    }
};

Options = new Options()
{
    PluginDynamic = new Dictionary<string, object>()
{ 
    { 
        "plugins",
        new Dictionary<string, object>()
        {
            { "annotation",
              annotation
            }
        }
    } 
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on ChartJSCore:

Package Downloads
GD6.Common

Package Description

EasyChartJSCore

Helper library for the ChartJSCore package.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.12.0 586 12/6/2024
3.11.0 395 11/26/2024
3.10.1 33,725 7/31/2023
3.10.0 5,525 3/27/2023
3.9.1 27,086 10/27/2022
3.9.0 1,587 10/20/2022
3.3.0 12,912 4/20/2022
3.2.0 1,865 4/1/2022
3.1.0 1,115 3/11/2022
3.0.2 1,567 3/3/2022
3.0.1 1,060 2/16/2022
3.0.0 566 2/13/2022
1.5.8 65,507 7/1/2020
1.5.7 33,950 2/3/2020
1.5.6 5,779 11/29/2019
1.5.5 24,033 8/12/2019
1.5.4 796 8/7/2019
1.5.3 827 8/6/2019
1.5.2 1,274 7/26/2019
1.5.1 1,150 7/17/2019
1.5.0 859 7/8/2019
1.4.3 9,104 5/2/2019
1.4.2 1,011 4/21/2019
1.4.1 2,119 4/1/2019
1.4.0 932 3/29/2019
1.3.2 2,174 1/8/2019
1.3.1 10,473 9/11/2018
1.2.0 1,959 12/18/2017
1.1.6 1,505 11/29/2017
1.0.5 1,318 9/25/2017
1.0.4 1,626 7/28/2017

Version 3.12.0 - Update DataLabels and Annotation plugins default values