Tooltip for Telerik Blazor Chart
The Telerik Chart provides a tooltip for its data points. You can have settings specific to each <ChartSeries>
, common tooltip settings for all series, or a shared tooltip for all categories.
In this article:
Basics
By default the value of the point will be presented when hovered over.
To enable tooltips for the data points of each individual series:
- Inside the
<ChartSeries>
, include the<ChartSeriesTooltip>
tag. - Set its
Visible
parameter totrue
.
@* Enable tooltip for a single Chart Series *@
<TelerikChart>
<ChartSeriesItems>
<ChartSeries Type="ChartSeriesType.Column" Name="Series 1" Data="@data1">
</ChartSeries>
<ChartSeries Type="ChartSeriesType.Column" Name="Series 2" Data="@data2">
<ChartSeriesTooltip Visible="true"></ChartSeriesTooltip>
</ChartSeries>
</ChartSeriesItems>
<ChartCategoryAxes>
<ChartCategoryAxis Categories="@xAxisItems"></ChartCategoryAxis>
</ChartCategoryAxes>
</TelerikChart>
@code {
public List<object> data1 = new List<object>() { 1, 2, 3, 4 };
public List<object> data2 = new List<object>() { 2, 3, 4, 5 };
public string[] xAxisItems = new string[] { "Q1", "Q2", "Q3", "Q4" };
}
Common Tooltip
The Chart allows you to enable and define common tooltip settings for all series at once. It looks like the individual tooltips (the value of the point will be presented when hovered over), but you declare it only once.
A tooltip set to a specific <ChartSeries>
will take precedence over the common tooltip settings.
To enable the same tooltip for all series:
- Inside the
<TelerikChart>
, add the<ChartTooltip>
. - Set its
Visible
parameter totrue
.
@* This example shows you how to create a common tooltip for all data points *@
<TelerikChart>
<ChartTooltip Visible="true">
</ChartTooltip>
<ChartSeriesItems>
<ChartSeries Type="ChartSeriesType.Bar" Name="Product 1" Data="@series1Data">
</ChartSeries>
<ChartSeries Type="ChartSeriesType.Bar" Name="Product 2" Data="@series2Data">
</ChartSeries>
</ChartSeriesItems>
<ChartCategoryAxes>
<ChartCategoryAxis Categories="@xAxisItems"></ChartCategoryAxis>
</ChartCategoryAxes>
<ChartTitle Text="Quarterly revenue per product"></ChartTitle>
<ChartLegend Position="ChartLegendPosition.Right">
</ChartLegend>
</TelerikChart>
@code {
public List<object> series1Data = new List<object>() { 10, 2, 5, 6 };
public List<object> series2Data = new List<object>() { 5, 8, 2, 7 };
public string[] xAxisItems = new string[] { "Q1", "Q2", "Q3", "Q4" };
}
Customization
There are two types of customizations you can do for the tooltips:
- Parameter Settings - lets you alter cosmetic settings such as borders, colors and padding through simple parameters
- Template - lets you control the entire content
Parameter Settings
You can customize the appearance of the individual series tooltip by using:
Background
- control the background color by applying a CSS color string, including HEX and RGB. By default the it will match the color for the category.Color
- control the text color by applying a CSS color string, including HEX and RGB.Use the Template to take control over what is rendered in the tooltip.
@* This example shows how to customize the tooltip *@
<TelerikChart>
<ChartSeriesItems>
<ChartSeries Type="ChartSeriesType.Line" Name="Product 1 (bound to simple data)" Data="@simpleData">
</ChartSeries>
<ChartSeries Type="ChartSeriesType.Line" Name="Product 2 (bound to model)"
Data="@modelData" Field="@nameof(MyDataModel.SecondSeriesValue)">
<ChartSeriesTooltip Visible="true"
Background="#0000FF"
Color="#D3D3D3">
</ChartSeriesTooltip>
</ChartSeries>
</ChartSeriesItems>
<ChartValueAxes>
<ChartValueAxis Color="red"></ChartValueAxis>
</ChartValueAxes>
<ChartCategoryAxes>
<ChartCategoryAxis Categories="@xAxisItems"></ChartCategoryAxis>
</ChartCategoryAxes>
<ChartTitle Text="Quarterly sales trend"></ChartTitle>
<ChartLegend Position="Telerik.Blazor.ChartLegendPosition.Bottom">
</ChartLegend>
</TelerikChart>
@code {
public class MyDataModel
{
public int SecondSeriesValue { get; set; }
public string ExtraData { get; set; }
}
public List<MyDataModel> modelData = new List<MyDataModel>()
{
new MyDataModel() { SecondSeriesValue = 1, ExtraData = "first" },
new MyDataModel() { SecondSeriesValue = 5, ExtraData = "second" },
new MyDataModel() { SecondSeriesValue = 3, ExtraData = "third" },
new MyDataModel() { SecondSeriesValue = 2, ExtraData = "fourth" },
};
public List<object> simpleData = new List<object>() { 10, 2, 7, 5 };
public string[] xAxisItems = new string[] { "Q1", "Q2", "Q3", "Q4" };
}
Common Tooltip Settings
In a similar fashion, you can declare these settings in the common tooltip section:
Background
- control the background color by applying a CSS color string, including HEX and RGB. By default the it will match the color for the category.Color
- control the text color by applying a CSS color string, including HEX and RGB.Opacity
- control the opacity of the tooltip.Use the Template to take control over what is rendered in the tooltip - the tag name and data it provides is the same as for the specific tooltip, but it affects all series at once.
Template
The Template
allows you to take control over the rendering of the tooltip and include additional information to the user.
In the template you can:
Use business logic and render HTML
Use the
context
parameter that provides information about the current data point.
The available series data point information in the context
is:
-
FormattedValue
- maps to the default rendering of the tooltip, formatted as a string.- Use this when the chart's data is bound by Independent Series Binding. You can parse this to a numerical value (
int
,double
, etc.) in order to apply formatting. Otherwise, use theDataItem
to get the value of the point.
- Use this when the chart's data is bound by Independent Series Binding. You can parse this to a numerical value (
-
DataItem
- provides the data model of the current series item. You need to cast it to the type from your datasource, which needs to be serializable.- If you are using a Date Axis, the
DataItem
will contain the only the aggregated value in the corresponding y-value field, because it is a collection of more than one items. See theCategory
below for details.
- If you are using a Date Axis, the
-
Category
- provides information on the category the data point is located in. You need to cast it to the type in your data source, for exampleDateTime
,string
,int
or another type. TheCategory
parameter is applicable to Categorical Charts.- When using a Date Axis, you can use it, together with the
BaseUnit
value of the axis, to filter the data source and obtain the actual data items from the data source in case you want to provide extra information about them.
- When using a Date Axis, you can use it, together with the
Percentage
- applicable to Donut, Pie and Stacked 100% Charts - the percentage value of the current data point from the whole.SeriesIndex
- provides the index of the<ChartSeries>
the data point belongs to.SeriesName
- bound to theName
parameter of the<ChartSeries>
the data point belongs to.SeriesColor
- shows the RGB color of the Series the data point belongs to.CategoryIndex
- shows the index of the data point's x-axis category.
@* This example shows how to use the Template to provide an Icon and additional information from the model *@
<TelerikChart>
<ChartSeriesItems>
<ChartSeries Type="ChartSeriesType.Line" Name="Product 1 (bound to simple data)" Data="@simpleData">
</ChartSeries>
<ChartSeries Type="ChartSeriesType.Line" Name="Product 2 (bound to model)"
Data="@modelData" Field="@nameof(MyDataModel.SecondSeriesValue)">
<ChartSeriesTooltip Visible="true">
<Template>
<TelerikSvgIcon Icon="SvgIcon.InfoCircle" />
@((context.DataItem as MyDataModel).SecondSeriesValue) for @((context.DataItem as MyDataModel).ExtraData)
</Template>
</ChartSeriesTooltip>
</ChartSeries>
</ChartSeriesItems>
<ChartValueAxes>
<ChartValueAxis Color="red"></ChartValueAxis>
</ChartValueAxes>
<ChartCategoryAxes>
<ChartCategoryAxis Categories="@xAxisItems"></ChartCategoryAxis>
</ChartCategoryAxes>
<ChartTitle Text="Quarterly sales trend"></ChartTitle>
<ChartLegend Position="Telerik.Blazor.ChartLegendPosition.Bottom">
</ChartLegend>
</TelerikChart>
@code {
public class MyDataModel
{
public int SecondSeriesValue { get; set; }
public string ExtraData { get; set; }
}
public List<MyDataModel> modelData = new List<MyDataModel>()
{
new MyDataModel() { SecondSeriesValue = 1, ExtraData = "first" },
new MyDataModel() { SecondSeriesValue = 5, ExtraData = "second" },
new MyDataModel() { SecondSeriesValue = 3, ExtraData = "third" },
new MyDataModel() { SecondSeriesValue = 2, ExtraData = "fourth" },
};
public List<object> simpleData = new List<object>() { 10, 2, 7, 5 };
public string[] xAxisItems = new string[] { "Q1", "Q2", "Q3", "Q4" };
}