Getting Started with the ASP.NET MVC HeatMap Chart Control

22 Jul 20267 minutes to read

This section briefly explains how to add the Syncfusion® ASP.NET MVC HeatMap Chart control to an ASP.NET MVC 5 (.NET Framework) application using Visual Studio.

Prerequisites

Refer to the System requirements for ASP.NET MVC controls before creating the application.

Create an ASP.NET MVC application with HTML helper

You can create an ASP.NET MVC application using either of the following options:

Install the ASP.NET MVC NuGet package

To add Syncfusion® ASP.NET MVC controls in the application, open the NuGet Package Manager in Visual Studio by selecting Tools → NuGet Package Manager → Manage NuGet Packages for Solution. Search for Syncfusion.EJ2.MVC5 and install it.

Alternatively, you can use the Package Manager Console by navigating to Tools → NuGet Package Manager → Package Manager Console, and then run the following command:

Install-Package Syncfusion.EJ2.MVC5 -Version 34.1.29

NOTE

Syncfusion® ASP.NET MVC controls are available on nuget.org. Refer to the NuGet packages topic to learn more about installing NuGet packages in various operating system environments. The Syncfusion.EJ2.MVC5 NuGet package depends on Newtonsoft.Json (≥ 12.0.2) for JSON serialization and Syncfusion.Licensing for validating the Syncfusion® license key.

Add the namespace

Add the Syncfusion.EJ2 namespace reference inside the <system.web.webPages.razor><pages><namespaces> element of the ~/Views/Web.config file.

<namespaces>
    <add namespace="Syncfusion.EJ2" />
</namespaces>

Add script resources

Add the script reference inside the <head> element of the ~/Views/Shared/_Layout.cshtml file as follows. The reference loads the base ej2.min.js bundle, which contains the Syncfusion® ASP.NET MVC helper scripts.

<head>
    ...
    <!-- Syncfusion ASP.NET MVC controls scripts -->
    <script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js"></script>
</head>

NOTE

Refer to the Adding Script Reference topic to learn different approaches for adding script references in an ASP.NET MVC application.

Register the Syncfusion® Script Manager

Register the script manager EJS().ScriptManager() at the end of the <body> element in the ~/Views/Shared/_Layout.cshtml file as follows. The Script Manager resolves and loads control-specific scripts automatically.

<body>
    ...
    <!-- Syncfusion ASP.NET MVC Script Manager -->
    @Html.EJS().ScriptManager()
</body>

Build the HeatMap Chart

This section walks through adding the HeatMap Chart control to ~/Views/Home/Index.cshtml and progressively populating it with data, axis labels, a title, a legend, data labels, a custom palette, and a tooltip.

Step 1: Add the HeatMap Chart control

Add the Syncfusion® ASP.NET MVC HeatMap Chart control to the ~/Views/Home/Index.cshtml page.

@Html.EJS().HeatMap("container").Render()

Press Ctrl + F5 on Windows or + F5 on macOS to run the application. The HeatMap Chart control will be rendered as an empty placeholder in the default web browser.

Step 2: Populate the heat map with data

This section explains how to populate the following two-dimensional array data to the heat map.

@model int[,]

@Html.EJS().HeatMap("container").DataSource(Model).Render()
public class HomeController : Controller
{
    public ActionResult Index()
    {
        string[] xlabels = new string[12] { "Nancy", "Andrew", "Janet", "Margaret", "Steven", "Michael", "Robert", "Laura", "Anne", "Paul", "Karin", "Mario" };
        ViewBag.xLabels = xlabels;
        string[] yLabels = new string[6] { "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
        ViewBag.yLabels = yLabels;
        int[,] data = new int[,]
        {
            {73, 39, 26, 39, 94, 0},
            {93, 58, 53, 38, 26, 68},
            {99, 28, 22, 4, 66, 90},
            {14, 26, 97, 69, 69, 3},
            {7, 46, 47, 47, 88, 6},
            {41, 55, 73, 23, 3, 79},
            {56, 69, 21, 86, 3, 33},
            {45, 7, 53, 81, 95, 79},
            {60, 77, 74, 68, 88, 51},
            {25, 25, 10, 12, 78, 14},
            {25, 56, 55, 58, 12, 82},
            {74, 33, 88, 23, 86, 59}
        };
        return View(data);
    }
}

Press Ctrl + F5 on Windows or + F5 on macOS to run the application. The Syncfusion® ASP.NET MVC HeatMap Chart control will be rendered with the bound data in the default web browser.

ASP.NET MVC HeatMap Chart with Data

NOTE

Explore the ASP.NET MVC HeatMap Chart sample on GitHub to understand how this getting started example works.

Troubleshooting

If the HeatMap Chart control does not render as expected, review the following common issues and their resolutions.

  • “Script Manager is not defined” or scripts run twice@Html.EJS().ScriptManager() was not added, or was added more than once. Ensure ScriptManager is registered exactly once at the end of <body> in _Layout.cshtml.

  • NullReferenceException in the viewViewBag.xLabels or ViewBag.yLabels was not set in the controller. Both arrays must be assigned before return View(data).

  • Newtonsoft.Json reference error after upgradeSyncfusion.EJ2.MVC5 requires Newtonsoft.Json ≥ 12.0.2. Install a compatible version via NuGet.

See also