Tooltip in ASP.NET MVC HeatMap Chart

Tooltip is used to provide the details of the heatmap cell, and this can be displayed, while hovering the cursor over the cell or performing tap action in touch devices.

Default tooltip

You can enable the tooltip by setting the showTooltip property to true.

@using Syncfusion.EJ2;
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Crude Oil Production of Non-OPEC Countries (in Million barrels per day)").TextStyle(ViewBag.textStyle)).XAxis(
xAxis =>{ xAxis.Labels(ViewBag.xLabels)}).YAxis(yaxis =>
yaxis.Labels(ViewBag.yLabels)).CellSettings(cs =>
cs.ShowLabel(false)).ShowTooltip(true).DataSource(ViewBag.dataSource).Render()
public ActionResult Tooltip()
        {
        ViewBag.textStyle = new
        {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
        };
            string[] xlabels = new string[8] { "Canada", "China", "Egypt", "Mexico", "Norway", "Russia", "UK", "USA"};
            ViewBag.xLabels = xlabels;
            string[] yLabels = new string[11] { "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010" };
            ViewBag.yLabels = yLabels;
            ViewBag.dataSource = GetDataSource();
            return View();
        }

 private int[,] GetDataSource()
 {
     int[,] data = new int[,]
            {
                [0.72, 0.71, 0.71, 0.67, 0.72, 0.53, 0.53, 0.56, 0.58, 0.56],
                [2.28, 2.29, 2.09, 1.84, 1.64, 1.49, 1.49, 1.39, 1.32, 1.23],
                [2.02, 2.17, 2.30, 2.39, 2.36, 2.52, 2.62, 2.57, 2.57, 2.74],
                [3.21, 3.26, 3.45, 3.47, 3.42, 3.34, 3.14, 2.83, 2.64, 2.61],
                [3.22, 3.13, 3.04, 2.95, 2.69, 2.49, 2.27, 2.18, 2.06, 1.87],
                [3.30, 3.39, 3.40, 3.48, 3.60, 3.67, 3.73, 3.79, 3.79, 4.07],
                [5.80, 5.74, 5.64, 5.44, 5.18, 5.08, 5.07, 5.00, 5.35, 5.47],
                [6.91, 7.40, 8.13, 8.80, 9.04, 9.24, 9.43, 9.35, 9.49, 9.69]
            };
            return data;
 }

Tooltip template

In heatmap, you can customize the tooltip using the tooltipRender client side event.

@using Syncfusion.EJ2;
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Crude Oil Production of Non-OPEC Countries (in Million barrels per day)").TextStyle(ViewBag.textStyle)).XAxis(
xAxis =>{ xAxis.Labels(ViewBag.xLabels)}).YAxis(yaxis =>
yaxis.Labels(ViewBag.yLabels)).CellSettings(cs =>
cs.ShowLabel(false)).ShowTooltip(true).TooltipRender("tooltipRender").DataSource(ViewBag.dataSource).Render()

<script>
    var tooltipRender = function (args) {
        args.content = ['In ' + args.yLabel + ', the ' + args.xLabel + ' produced ' + args.value + ' million barrels per day'];
    };
</script>
public ActionResult Tooltiptemplate()
        {
        ViewBag.textStyle = new
        {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
        };
            string[] xlabels = new string[8] { "Canada", "China", "Egypt", "Mexico", "Norway", "Russia", "UK", "USA"};
            ViewBag.xLabels = xlabels;
            string[] yLabels = new string[11] { "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010" };
            ViewBag.yLabels = yLabels;
            ViewBag.dataSource = GetDataSource();
            return View();
        }

 private int[,] GetDataSource()
 {
     int[,] data = new int[,]
            {
                [0.72, 0.71, 0.71, 0.67, 0.72, 0.53, 0.53, 0.56, 0.58, 0.56],
                [2.28, 2.29, 2.09, 1.84, 1.64, 1.49, 1.49, 1.39, 1.32, 1.23],
                [2.02, 2.17, 2.30, 2.39, 2.36, 2.52, 2.62, 2.57, 2.57, 2.74],
                [3.21, 3.26, 3.45, 3.47, 3.42, 3.34, 3.14, 2.83, 2.64, 2.61],
                [3.22, 3.13, 3.04, 2.95, 2.69, 2.49, 2.27, 2.18, 2.06, 1.87],
                [3.30, 3.39, 3.40, 3.48, 3.60, 3.67, 3.73, 3.79, 3.79, 4.07],
                [5.80, 5.74, 5.64, 5.44, 5.18, 5.08, 5.07, 5.00, 5.35, 5.47],
                [6.91, 7.40, 8.13, 8.80, 9.04, 9.24, 9.43, 9.35, 9.49, 9.69]
            };
            return data;
 }

Customize the appearance of Tooltip

The fill and border properties are used to customize the background color and border of the tooltip respectively. The textStyle property in the tooltip is used to customize the font of the tooltip text.

@using Syncfusion.EJ2;
@Html.EJS().HeatMap("container").TitleSettings(ts => ts.Text("Crude Oil Production of Non-OPEC Countries (in Million barrels per day)").TextStyle(ViewBag.textStyle)).XAxis(
xAxis =>{ xAxis.Labels(ViewBag.xLabels)}).YAxis(yaxis =>
yaxis.Labels(ViewBag.yLabels)).CellSettings(cs =>
cs.ShowLabel(false)).ShowTooltip(true).TooltipRender("tooltipRender").PaletteSettings(ps => ps.Palette(palette =>
{
    palette.Color("#F0ADCE").Add();
    palette.Color("#19307B").Add();
}).TooltipSettings(ts =>
            ts.Fill("#265259").Border(br => br.Color("#98BABF").Width(1)).TextStyle(style =>
            style.Color("#FFFFFF").Size("12px"))).DataSource(ViewBag.dataSource).Render()

<script>
    var tooltipRender = function (args) {
        args.content = ['In ' + args.yLabel + ', the ' + args.xLabel + ' produced ' + args.value + ' million barrels per day'];
    };
</script>
public ActionResult Tooltipfill()
        {
        ViewBag.textStyle = new
        {
         size= "15px",
         fontWeight= "500",
         fontStyle= "Normal",
         fontFamily= "Segoe UI"
        };
            string[] xlabels = new string[8] { "Canada", "China", "Egypt", "Mexico", "Norway", "Russia", "UK", "USA"};
            ViewBag.xLabels = xlabels;
            string[] yLabels = new string[11] { "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010" };
            ViewBag.yLabels = yLabels;
            ViewBag.dataSource = GetDataSource();
            return View();
        }

 private int[,] GetDataSource()
 {
     int[,] data = new int[,]
            {
                [0.72, 0.71, 0.71, 0.67, 0.72, 0.53, 0.53, 0.56, 0.58, 0.56],
                [2.28, 2.29, 2.09, 1.84, 1.64, 1.49, 1.49, 1.39, 1.32, 1.23],
                [2.02, 2.17, 2.30, 2.39, 2.36, 2.52, 2.62, 2.57, 2.57, 2.74],
                [3.21, 3.26, 3.45, 3.47, 3.42, 3.34, 3.14, 2.83, 2.64, 2.61],
                [3.22, 3.13, 3.04, 2.95, 2.69, 2.49, 2.27, 2.18, 2.06, 1.87],
                [3.30, 3.39, 3.40, 3.48, 3.60, 3.67, 3.73, 3.79, 3.79, 4.07],
                [5.80, 5.74, 5.64, 5.44, 5.18, 5.08, 5.07, 5.00, 5.35, 5.47],
                [6.91, 7.40, 8.13, 8.80, 9.04, 9.24, 9.43, 9.35, 9.49, 9.69]
            };
            return data;
 }

Access the datasource record in the tooltip

The complete datasource record associated with a hovered HeatMap cell can be accessed using the data property in the tooltipRender event arguments. The value property contains only the rendered cell value, whereas the data property contains the corresponding datasource record. This allows additional mapped or unmapped fields from the datasource to be used when customizing the tooltip content without modifying the underlying datasource.

The value returned through the data property depends on the datasource binding type:

  • In JSON cell binding, it contains the complete JSON object associated with the hovered cell.
  • In JSON table binding, it contains the complete JSON row object associated with the hovered cell.
  • In array cell binding, it contains the complete array associated with the hovered cell.
  • In array table binding, it contains the complete row array associated with the hovered cell.

The following example demonstrates how to access the complete datasource record through the data property and use its additional fields to customize the tooltip content.

@using Syncfusion.EJ2;
@Html.EJS().HeatMap("container").TooltipRender("tooltipRender").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).CellSettings(cs =>
        cs.ShowLabel(true).Format("${value}K")).DataSourceSettings(ds =>
          ds.IsJsonData(true).AdaptorType(Syncfusion.EJ2.HeatMap.AdaptorType.Cell).XDataMapping("Employee").YDataMapping("Month").ValueMapping("Revenue")).Render()

<script>
    function tooltipRender(args) {
        var record = args.data;
        if (record) {
            args.content = [
                'Employee : ' + record.Employee,
                'Month : ' + record.Month,
                'Revenue : $' + record.Revenue + 'K',
                'Target : $' + record.Target + 'K',
                'Region : ' + record.Region
            ];
        }
    }
</script>
public class SalesRecord
{
    public string Employee { get; set; }
    public string Month { get; set; }
    public int Revenue { get; set; }
    public int Target { get; set; }
    public string Region { get; set; }
}

public ActionResult TooltipData()
{
    ViewBag.textStyle = new
    {
        size = "15px",
        fontWeight = "500",
        fontStyle = "Normal",
        fontFamily = "Segoe UI"
    };
    ViewBag.xLabels = new string[] { "Nancy", "Andrew", "Janet" };
    ViewBag.yLabels = new string[] { "Jan", "Feb", "Mar" };
    ViewBag.dataSource = GetDataSource();
    return View();
}

private List<SalesRecord> GetDataSource()
{
    return new List<SalesRecord>
    {
        new SalesRecord { Employee = "Nancy", Month = "Jan", Revenue = 73, Target = 70, Region = "North America" },
        new SalesRecord { Employee = "Nancy", Month = "Feb", Revenue = 39, Target = 50, Region = "North America" },
        new SalesRecord { Employee = "Nancy", Month = "Mar", Revenue = 26, Target = 45, Region = "North America" },
        new SalesRecord { Employee = "Andrew", Month = "Jan", Revenue = 93, Target = 80, Region = "Europe" },
        new SalesRecord { Employee = "Andrew", Month = "Feb", Revenue = 58, Target = 55, Region = "Europe" },
        new SalesRecord { Employee = "Andrew", Month = "Mar", Revenue = 53, Target = 60, Region = "Europe" },
        new SalesRecord { Employee = "Janet", Month = "Jan", Revenue = 99, Target = 90, Region = "Asia Pacific" },
        new SalesRecord { Employee = "Janet", Month = "Feb", Revenue = 28, Target = 40, Region = "Asia Pacific" },
        new SalesRecord { Employee = "Janet", Month = "Mar", Revenue = 22, Target = 35, Region = "Asia Pacific" }
    };
}