Tooltip in ASP.NET MVC Chart Component
19 Dec 202420 minutes to read
Chart will display details about the points through tooltip, when the mouse is moved over the point.
Enable tooltip
The tooltip is useful when you cannot display information by using the data labels due to space constraints. You can enable the tooltip by setting Enable
property as true in Tooltip
object.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).
Title("Olympic Medal Counts - RIO").Tooltip(tt => tt.Enable(true)).Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46 },
new ColumnChartData { x= "GBR", yValue= 27 },
new ColumnChartData { x= "CHN", yValue= 26 },
new ColumnChartData { x= "UK", yValue= 26 },
new ColumnChartData { x= "AUS", yValue= 26 },
new ColumnChartData { x= "IND", yValue= 26 },
new ColumnChartData { x= "DEN", yValue= 26 },
new ColumnChartData { x= "MEX", yValue= 26 },
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
}
Fixed tooltip
By default, tooltip track the mouse movement, but you can set a fixed position for the tooltip by using the Location
property.
@(Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
XName("xValue").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).LegendSettings(ls => ls.Visible(false))
.Title("Olympic Medal Counts - RIO").Tooltip(tt => tt.Enable(true).Location(lc => lc.X(120).Y(20))).Render())
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { xValue= "USA", yValue= 46 },
new ColumnChartData { xValue= "GBR", yValue= 27 },
new ColumnChartData { xValue= "CHN", yValue= 26 },
new ColumnChartData { xValue= "UK", yValue= 26 },
new ColumnChartData { xValue= "AUS", yValue= 26 },
new ColumnChartData { xValue= "IND", yValue= 26 },
new ColumnChartData { xValue= "DEN", yValue= 26 },
new ColumnChartData { xValue= "MEX", yValue= 26 }
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string xValue;
public double yValue;
}
Format the tooltip
By default, tooltip shows information of x and y value in points. In addition to that, you can show more information in tooltip. For example the format ${series.name} ${point.x}
shows series name and point x value.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).
Title("Olympic Medal Counts - RIO").
Tooltip(tt => tt.Enable(true).
Header("medal count").
Format("${series.name} ${point.x} : ${point.y}")).Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46 },
new ColumnChartData { x= "GBR", yValue= 27 },
new ColumnChartData { x= "CHN", yValue= 26 },
new ColumnChartData { x= "UK", yValue= 26 },
new ColumnChartData { x= "AUS", yValue= 26 },
new ColumnChartData { x= "IND", yValue= 26 },
new ColumnChartData { x= "DEN", yValue= 26 },
new ColumnChartData { x= "MEX", yValue= 26 },
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
}
Tooltip template
Any HTML elements can be displayed in the tooltip by using the Template
property of the tooltip. You can use the ${x} and ${y} as place holders in the HTML element to display the x and y values of the corresponding data point.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).
Title("Olympic Medal Counts - RIO").
Tooltip(tt => tt.Enable(true).
header("medal count").
template=("#Medal")).Render()
<script id="Medal" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Medal</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46 },
new ColumnChartData { x= "GBR", yValue= 27 },
new ColumnChartData { x= "CHN", yValue= 26 },
new ColumnChartData { x= "UK", yValue= 26 },
new ColumnChartData { x= "AUS", yValue= 26 },
new ColumnChartData { x= "IND", yValue= 26 },
new ColumnChartData { x= "DEN", yValue= 26 },
new ColumnChartData { x= "MEX", yValue= 26 },
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
}
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. The HighlightColor
property is used to customize the point color while hovering for tooltip.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
HighlightColor("red").
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).
Title("Olympic Medal Counts - RIO").Tooltip(tt => tt.Enable(true).
Format("<b>${point.x} : ${point.y}</b>").
Fill('#7bb4eb')).Render()
}
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46 },
new ColumnChartData { x= "GBR", yValue= 27 },
new ColumnChartData { x= "CHN", yValue= 26 },
new ColumnChartData { x= "UK", yValue= 26 },
new ColumnChartData { x= "AUS", yValue= 26 },
new ColumnChartData { x= "IND", yValue= 26 },
new ColumnChartData { x= "DEN", yValue= 26 },
new ColumnChartData { x= "MEX", yValue= 26 },
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
}
Tooltip mapping name
By default, tooltip shows information of x and y value in points. You can show more information from data source in tooltip by using the TooltipMappingName
property of the tooltip. You can use the ${point.tooltip}
as place holders to display the specified tooltip content.
@(Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
XName("xValue").
YName("yValue").
TooltipMappingName("text").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.Category).Interval(1))
.Tooltip(tp => tp.Enable(true).Format("${point.tooltip}"))
.Render()
)
public ActionResult Index()
{
List<GroupingChartData> chartData = new List<GroupingChartData>
{
new GroupingChartData { xValue = "China", yValue = 26, text = "China: 26" },
new GroupingChartData { xValue = "Russia", yValue = 19, text = "Russia: 19" },
new GroupingChartData { xValue = "Germany", yValue = 17, text = "Germany: 17" },
new GroupingChartData { xValue = "Japan", yValue = 12, text = "Japan: 12" },
new GroupingChartData { xValue = "France", yValue = 10, text = "France: 10" },
new GroupingChartData { xValue = "South Korea", yValue = 9, text = "South Korea: 9" },
new GroupingChartData { xValue = "Great Britain", yValue = 27, text = "Great Britain: 27" },
new GroupingChartData { xValue = "Italy", yValue = 8, text = "Italy: 8" },
new GroupingChartData { xValue = "Australia", yValue = 8, text = "Australia: 8" },
new GroupingChartData { xValue = "Netherlands", yValue = 8, text = "Netherlands: 8" },
new GroupingChartData { xValue = "Hungary", yValue = 8, text = "Hungary: 8" },
new GroupingChartData { xValue = "Brazil", yValue = 7, text = "Brazil: 7" },
new GroupingChartData { xValue = "Spain", yValue = 7, text = "Spain: 7" },
new GroupingChartData { xValue = "Kenya", yValue = 6, text = "Kenya: 6" }
};
ViewBag.dataSource = chartData;
return View();
}
public class GroupingChartData
{
public string xValue;
public double yValue;
public string text;
}
Enable highlight
By setting the EnableHighlight
property to true, you can highlight all points in the hovered series while dimming points in other series, enhancing focus and clarity.
<ejs-chart id="container">
<e-chart-tooltipsettings enable="true" format="${point.tooltip}" enableHighlight="true">
</e-chart-tooltipsettings>
<e-chart-legendsettings visible="true"></e-chart-legendsettings>
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" tooltipMappingName="text" name="Germany" xName="xValue" yName="yValue" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
</e-series>
</e-series-collection>
</ejs-chart>
...
public class GroupingChartData
{
public string xValue;
public double yValue;
public string text;
}