Search results

Getting started with ASP.NET MVC HeatMap Chart control

22 Jul 2021 / 3 minutes to read

Prerequisites

To get start with ASP.NET MVC application, need to ensure the following software to be installed on the machine.

  1. .Net Framework 4.5 and above.
  2. ASP.NET MVC 4 or ASP.NET MVC 5
  3. Visual Studio

Preparing ASP.NET MVC application

The following steps to create ASP.NET MVC Application.

Step 1: Create ASP.NET MVC Application with default template project in Visual Studio.

Default Template

Step 2: Once your project created. We need to add Syncfusion EJ2 package into your application by using NuGet Package Manager.

Open the NuGet package manager.

Solution Explorer

Install the Syncfusion.EJ2.MVC4 package to the application.

Nuget Demo

After installation complete, this will be included in the project. You can refer it from the Project Assembly Reference.

We need to install NewtonSoft.JSON as a dependency, since Syncfusion.EJ2 dependent to NewtonSoft.JSON package.

Step 3: Add Syncfusion.EJ2 namespace reference in Web.config

Copied to clipboard
<namespaces>
<add namespace="Syncfusion.EJ2"/>
</namespaces>
Copied to clipboard
<system.web>
<compilation>
  <assemblies>
    <add assembly="Syncfusion.EJ2"/>
  </assemblies>
</compilation>
  </system.web>

Step 4: Add client side resource through CDN or local package in the layout page _Layout.cshtml.

Copied to clipboard
<head>
  @* Syncfusion Essential JS 2 Styles *@
  <link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/material.css" />

  @* Syncfusion Essential JS 2 Scripts *@
  <script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js"></script>
</head>

Step 5: Adding Script Manager in layout page _Layout.cshtml.

Copied to clipboard
@Html.EJS().ScriptManager()

Step 6:

Add the below code to your Index.cshtml view page which is present under Views/Home folder, to initialize the heatmap.

Copied to clipboard
@using Syncfusion.EJ2;
@Html.EJS().HeatMap("container").Render()

Populate heat map with data

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

razor
data.cs
Copied to clipboard
@Html.EJS().HeatMap("container").DataSource(ViewBag.dataSource).Render()
Copied to clipboard
public ActionResult Data()
{
    ViewBag.dataSource = GetDataSource();
    return View();
}

 private int[,] GetDataSource()
 {
     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 data;
 }

Enable axis labels

You can add axis labels to the heat map and format those labels using the xAxis and yAxis properties. Axis labels provide additional information about the data points populated in the heat map.

razor
axis.cs
Copied to clipboard
@Html.EJS().HeatMap("container").XAxis(
            xAxis =>
            {
                xAxis.Labels(ViewBag.xLabels);
            }).YAxis(yaxis =>
            {
                yaxis.Labels(ViewBag.yLabels);
            }).DataSource(ViewBag.dataSource).Render()
Copied to clipboard
public ActionResult Axis()
{
    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;
    ViewBag.dataSource = GetDataSource();
    return View();
}

 private int[,] GetDataSource()
 {
     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 data;
 }

Add heat map title

Add a title using the titleSettings property to the heat map to provide quick information to the user about the data populated in the heat map.

razor
title.cs
Copied to clipboard
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Sales Revenue per Employee (in 1000 US$)").TextStyle(ViewBag.textStyle)).XAxis(
            xAxis =>
            {
                xAxis.Labels(ViewBag.xLabels);
            }).YAxis(yaxis =>
            {
                yaxis.Labels(ViewBag.yLabels);
            }).DataSource(ViewBag.dataSource).Render()
Copied to clipboard
public ActionResult Title()
{
    ViewBag.textStyle = new
     {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
         };
         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;
        ViewBag.dataSource = GetDataSource();
        return View();
}

 private int[,] GetDataSource()
 {
     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 data;
 }

Enable legend

Use a legend for the heat map in the legendSettings object by setting the visible property to true.

razor
legend.cs
Copied to clipboard
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Hourly Weather Forecast (in Celsius)").TextStyle(ViewBag.textStyle)).XAxis(xAxis =>
{
xAxis.Labels(ViewBag.xLabels);
}).YAxis(yaxis =>
{
    yaxis.Labels(ViewBag.yLabels);
}).LegendSettings(ls =>ls.Visible(true).Position(Syncfusion.EJ2.HeatMap.LegendPosition.Right).ShowLabel(true).Height("150px")).DataSource(ViewBag.dataSource).Render()
Copied to clipboard
public ActionResult Legend()
{
    ViewBag.textStyle = new
     {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
         };
         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;
        ViewBag.dataSource = GetDataSource();
        return View();
}

 private int[,] GetDataSource()
 {
     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 data;
 }

Add data label

Add data labels to improve the readability of the heat map. This can be achieved by setting the showLabel property to true in the cellSettings object.

razor
label.cs
Copied to clipboard
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Hourly Weather Forecast (in Celsius)").TextStyle(ViewBag.textStyle)).XAxis(xAxis =>
{
xAxis.Labels(ViewBag.xLabels);
}).YAxis(yaxis =>
{
    yaxis.Labels(ViewBag.yLabels);
}).LegendSettings(ls =>ls.Visible(true).Position(Syncfusion.EJ2.HeatMap.LegendPosition.Right).ShowLabel(true).Height("150px")).CellSettings(cs =>cs.ShowLabel(true)).DataSource(ViewBag.dataSource).Render()
Copied to clipboard
public ActionResult Label()
{
    ViewBag.textStyle = new
     {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
         };
         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;
        ViewBag.dataSource = GetDataSource();
        return View();
}

 private int[,] GetDataSource()
 {
     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 data;
 }

Add custom cell palette

The default palette settings of the heat map cells can be customized by using the paletteSettings property. Using the palette property in paletteSettings object, you can change the color set for the cells. You can change the color mode of the cells to fixed or gradient mode using the type property.

razor
palette.cs
Copied to clipboard
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Hourly Weather Forecast (in Celsius)").TextStyle(ViewBag.textStyle)).XAxis(xAxis =>
{
xAxis.Labels(ViewBag.xLabels);
}).YAxis(yaxis =>
{
    yaxis.Labels(ViewBag.yLabels);
})
LegendSettings(ls =>ls.Visible(true).Position(Syncfusion.EJ2.HeatMap.LegendPosition.Right).ShowLabel(true).Height("150px")).
CellSettings(cs =>cs.ShowLabel(true)).
PaletteSettings(ps => ps.Palette(palette =>
             {
                 palette.Color("#C06C84").Value(0).Add();
                 palette.Color("#6C5B7B").Value(50).Add();
                 palette.Color("#355C7D").Value(100).Add();
             }).Type(Syncfusion.EJ2.HeatMap.PaletteType.Gradient)).DataSource(ViewBag.dataSource).Render()
Copied to clipboard
public ActionResult Label()
{
    ViewBag.textStyle = new
     {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
         };
         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;
        ViewBag.dataSource = GetDataSource();
        return View();
}

 private int[,] GetDataSource()
 {
     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 data;
 }

Enable tooltip

The tooltip is used when you cannot display information by using the data labels due to space constraints. You can enable the tooltip by setting the showTooltip property to true.

razor
tooltip.cs
Copied to clipboard
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Hourly Weather Forecast (in Celsius)").TextStyle(ViewBag.textStyle)).XAxis(xAxis =>
{
xAxis.Labels(ViewBag.xLabels);
}).YAxis(yaxis =>
{
    yaxis.Labels(ViewBag.yLabels);
}).LegendSettings(ls =>ls.Visible(true).Position(Syncfusion.EJ2.HeatMap.LegendPosition.Right).ShowLabel(true).Height("150px")).CellSettings(cs =>cs.ShowLabel(true)).ShowTooltip(true).PaletteSettings(ps => ps.Palette(palette =>
             {
                 palette.Color("#C06C84").Value(0).Add();
                 palette.Color("#6C5B7B").Value(50).Add();
                 palette.Color("#355C7D").Value(100).Add();
             }).Type(Syncfusion.EJ2.HeatMap.PaletteType.Gradient)).DataSource(ViewBag.dataSource).Render()
Copied to clipboard
public ActionResult Label()
{
    ViewBag.textStyle = new
     {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
         };
         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;
        ViewBag.dataSource = GetDataSource();
        return View();
}

 private int[,] GetDataSource()
 {
     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 data;
 }