Example of Tooltip Template in ASP.NET MVC Charts Control
This example writes the tooltip of a point from your own template, with the fields of the row in it.
${x}
Price: ${y}
Units sold: ${UnitsSold}
Rating: ${Rating} / 5
@Html.UiChart(new UiChartOptions
{
Id = "tooltipTemplateChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "Price of each product",
Legend = new UiChartLegend { Visible = false },
Tooltip = new UiChartTooltip { Template = "#productTooltip" },
PrimaryXAxis = new UiChartAxis { LabelRotation = -45 },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Product", YName = "Price", Name = "Price" }
}
})
<template id="productTooltip">
<div style="min-width: 150px">
<div style="font-weight: 700; border-bottom: 3px solid ${color}; padding-bottom: 4px; margin-bottom: 4px">${x}</div>
<div>Price: <b>${y}</b></div>
<div>Units sold: <b>${UnitsSold}</b></div>
<div>Rating: <b>${Rating} / 5</b></div>
</div>
</template>
@Html.UiChart(new UiChartOptions
{
Id = "tooltipTemplateChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "Price of each product",
Legend = new UiChartLegend { Visible = false },
Tooltip = new UiChartTooltip { Template = "#productTooltip" },
PrimaryXAxis = new UiChartAxis { LabelRotation = -45 },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Product", YName = "Price", Name = "Price" }
}
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ChartController : Controller
{
// The page is Views/Chart/TooltipTemplate.cshtml; it draws what ViewData holds.
public ActionResult TooltipTemplate()
{
ViewData["ChartData"] = ProductStats();
return View();
}
/// <summary>One product: the rows of the Scatter and Bubble samples.</summary>
public class ProductData
{
public string Product { get; set; }
public double Price { get; set; }
public int UnitsSold { get; set; }
public double Revenue { get; set; }
public double Rating { get; set; }
}
private static List<ProductData> ProductStats()
{
var products = new List<ProductData>
{
new ProductData { Product = "Hand drill", Price = 89, UnitsSold = 420, Rating = 4.4 },
new ProductData { Product = "Angle grinder", Price = 129, UnitsSold = 310, Rating = 4.1 },
new ProductData { Product = "Circular saw", Price = 199, UnitsSold = 180, Rating = 4.6 },
new ProductData { Product = "Tool box", Price = 45, UnitsSold = 760, Rating = 3.9 },
new ProductData { Product = "Workbench", Price = 349, UnitsSold = 65, Rating = 4.8 },
new ProductData { Product = "Safety goggles", Price = 12, UnitsSold = 1350, Rating = 3.6 },
new ProductData { Product = "Laser level", Price = 159, UnitsSold = 140, Rating = 4.3 },
new ProductData { Product = "Screwdriver set", Price = 29, UnitsSold = 980, Rating = 4.0 }
};
foreach (ProductData product in products) { product.Revenue = product.Price * product.UnitsSold; }
return products;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Tooltip.Template names a <template> with your own markup for the tooltip of a point. The placeholders are ${x}, ${y}, ${series}, ${color} and any field of the row, here ${UnitsSold} and ${Rating}. Hover a column.