Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.
How to: Provide a Custom Tooltip for a Series Point
6 minutes to read
This example shows how to implement a custom tooltip that displays another chart with a GDP history for the selected country when hovering over a bar.
To accomplish this, it is necessary to create the DataTemplate object that specifies the custom tooltip appearance, and assign it to the Series.ToolTipPointTemplate property.
You also need to bind both charts to the GDP datasource and write the GetDataSource() and GetGDPs() methods. These methods allow you to get the GDP data from a datasource for each selected country to display it on a chart tooltip.
<Windowx:Class="ToolTipPointTemplate.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"Title="MainWindow"Height="500"Width="800" ><Grid><dxc:ChartControlName="chart"Width="758"Height="440"ToolTipEnabled="True"CrosshairEnabled="False"><dxc:ChartControl.ToolTipController><dxc:ChartToolTipControllerToolTipOpening="ChartToolTipController_ToolTipOpening" /></dxc:ChartControl.ToolTipController><dxc:XYDiagram2D><dxc:BarSideBySideSeries2DColorEach="True"DisplayName="G8 GDP in 2010"ArgumentDataMember="CountryName"ValueDataMember="GDPin2010"ValueScaleType="Numerical"ToolTipHintDataMember="ToolTipData"><dxc:BarSideBySideSeries2D.ToolTipPointTemplate><DataTemplate><dxc:ChartControlWidth="300"Height="150"EnableAnimation="True"DataSource="{Binding Hint.GDPs}"><dxc:XYDiagram2D><dxc:LineSeries2DArgumentDataMember="Year"ValueDataMember="Product"ArgumentScaleType="Numerical"ValueScaleType="Numerical"Brush="{Binding Hint.SeriesBrush}"MarkerSize="5"></dxc:LineSeries2D><dxc:XYDiagram2D.AxisX><dxc:AxisX2D><dxc:AxisX2D.Range><dxc:AxisRangeMinValue="2000"MaxValue="2011"/></dxc:AxisX2D.Range></dxc:AxisX2D></dxc:XYDiagram2D.AxisX><dxc:XYDiagram2D.AxisY><dxc:AxisY2DMinorCount="1"><dxc:AxisY2D.Range ><dxc:AxisRange /></dxc:AxisY2D.Range></dxc:AxisY2D></dxc:XYDiagram2D.AxisY></dxc:XYDiagram2D><dxc:ChartControl.Titles><dxc:TitleMargin="0"Padding="0"Dock="Top"FontSize="14"HorizontalAlignment="Center"VerticalAlignment="Top"Content="{Binding Hint.Title}" /></dxc:ChartControl.Titles></dxc:ChartControl></DataTemplate></dxc:BarSideBySideSeries2D.ToolTipPointTemplate></dxc:BarSideBySideSeries2D></dxc:XYDiagram2D></dxc:ChartControl></Grid></Window>
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Windows;
usingSystem.Windows.Documents;
usingSystem.Windows.Media;
usingSystem.Xml.Linq;
usingSystem.Windows.Resources;
usingSystem.Globalization;
namespaceToolTipPointTemplate {
publicpartialclassMainWindow : Window {
publicMainWindow() {
InitializeComponent();
chart.DataSource = GetDataSource();
}
List<G8Member> GetDataSource() {
List<GDP> GDPs = GetGDPs();
List<G8Member> countries = new List<G8Member>();
constint yearsInDecade = 10;
for (int countryCounter = 0; countryCounter < 8; countryCounter++) {
List<GDP> countryGDPs = new List<GDP>();
for (int countryValuesCounter = 0; countryValuesCounter < yearsInDecade; countryValuesCounter++) {
countryGDPs.Add(GDPs[countryCounter * yearsInDecade + countryValuesCounter]);
}
countries.Add(new G8Member(countryGDPs));
}
return countries;
}
List<GDP> GetGDPs() {
XDocument document = DataLoader.LoadXmlFromResources("/Data/GDPofG8.xml");
List<GDP> result = new List<GDP>();
if (document != null) {
foreach (XElement element in document.Element("G8GDPs").Elements()) {
string country = element.Element("Country").Value;
int year = int.Parse(element.Element("Year").Value);
decimal product = Convert.ToDecimal(element.Element("Product").Value, CultureInfo.InvariantCulture);
result.Add(new GDP(country, year, product));
}
}
return result;
}
publicstaticclassDataLoader {
publicstatic XDocument LoadXmlFromResources(string fileName) {
try {
fileName = "/ToolTipPointTemplate;component" + fileName;
Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute);
StreamResourceInfo info = Application.GetResourceStream(uri);
return XDocument.Load(info.Stream);
}
catch {
returnnull;
}
}
}
privatevoidChartToolTipController_ToolTipOpening(object sender, DevExpress.Xpf.Charts.ChartToolTipEventArgs e) {
ToolTipData toolTipData = e.Hint as ToolTipData;
int colorNumber = e.Series.Points.IndexOf(e.SeriesPoint);
Color seriesColor = e.ChartControl.Palette[colorNumber];
toolTipData.SeriesBrush = new SolidColorBrush(seriesColor);
}
}
publicclassGDP {
publicstring Country { get; privateset; }
publicint Year { get; privateset; }
publicdecimal Product { get; privateset; }
publicGDP(string country, int year, decimal product) {
Country = country;
Year = year;
Product = product;
}
}
publicclassG8Member {
publicdecimal GDPin2010 { get; privateset; }
publicstring CountryName { get; privateset; }
public ToolTipData ToolTipData { get; set; }
publicG8Member(List<GDP> GDPs) {
ToolTipData = new ToolTipData(GDPs, GDPs[0].Country);
CountryName = GDPs[0].Country;
GDPin2010 = GDPs[9].Product;
}
}
publicclassToolTipData {
public List<GDP> GDPs { get; privateset; }
public SolidColorBrush SeriesBrush { get; set; }
publicstring Title { get; privateset; }
publicToolTipData(List<GDP> gdps, string countryName) {
GDPs = gdps;
Title = countryName + " GDP History";
}
}
}
ImportsMicrosoft.VisualBasicImportsSystemImportsSystem.Collections.GenericImportsSystem.WindowsImportsSystem.Windows.DocumentsImportsSystem.Windows.MediaImportsSystem.Xml.LinqImportsSystem.Windows.ResourcesImportsSystem.GlobalizationNamespace ToolTipPointTemplate
PartialPublicClass MainWindow
Inherits Window
PublicSubNew()
InitializeComponent()
chart.DataSource = GetDataSource()
EndSubPrivateFunction GetDataSource() As List(Of G8Member)
Dim GDPs As List(Of GDP) = GetGDPs()
Dim countries AsNew List(Of G8Member)()
Const yearsInDecade AsInteger = 10For countryCounter AsInteger = 0To7Dim countryGDPs AsNew List(Of GDP)()
For countryValuesCounter AsInteger = 0To yearsInDecade - 1
countryGDPs.Add(GDPs(countryCounter * yearsInDecade + countryValuesCounter))
Next countryValuesCounter
countries.Add(New G8Member(countryGDPs))
Next countryCounter
Return countries
EndFunctionPrivateFunction GetGDPs() As List(Of GDP)
Dim document As XDocument = DataLoader.LoadXmlFromResources("/Data/GDPofG8.xml")
Dim result AsNew List(Of GDP)()
If document IsNotNothingThenForEach element As XElement In document.Element("G8GDPs").Elements()
Dim country AsString = element.Element("Country").Value
Dim year AsInteger = Integer.Parse(element.Element("Year").Value)
Dim product AsDecimal = Convert.ToDecimal(element.Element("Product").Value, CultureInfo.InvariantCulture)
result.Add(New GDP(country, year, product))
Next element
EndIfReturn result
EndFunctionPublicNotInheritableClass DataLoader
PrivateSubNew()
EndSubPublicSharedFunction LoadXmlFromResources(ByVal fileName AsString) As XDocument
Try
fileName = "/ToolTipPointTemplate;component" & fileName
Dim uri AsNew Uri(fileName, UriKind.RelativeOrAbsolute)
Dim info As StreamResourceInfo = Application.GetResourceStream(uri)
Return XDocument.Load(info.Stream)
CatchReturnNothingEndTryEndFunctionEndClassPrivateSub ChartToolTipController_ToolTipOpening(ByVal sender AsObject, ByVal e As DevExpress.Xpf.Charts.ChartToolTipEventArgs)
Dim toolTipData As ToolTipData = TryCast(e.Hint, ToolTipData)
Dim colorNumber AsInteger = e.Series.Points.IndexOf(e.SeriesPoint)
Dim seriesColor As Color = e.ChartControl.Palette(colorNumber)
toolTipData.SeriesBrush = New SolidColorBrush(seriesColor)
EndSubEndClassPublicClass GDP
Private privateCountry AsStringPublicProperty Country() AsStringGetReturn privateCountry
EndGetPrivateSet(ByVal value AsString)
privateCountry = value
EndSetEndPropertyPrivate privateYear AsIntegerPublicProperty Year() AsIntegerGetReturn privateYear
EndGetPrivateSet(ByVal value AsInteger)
privateYear = value
EndSetEndPropertyPrivate privateProduct AsDecimalPublicProperty Product() AsDecimalGetReturn privateProduct
EndGetPrivateSet(ByVal value AsDecimal)
privateProduct = value
EndSetEndPropertyPublicSubNew(ByVal country AsString, ByVal year AsInteger, ByVal product AsDecimal)
Country = country
Year = year
Product = product
EndSubEndClassPublicClass G8Member
Private privateGDPin2010 AsDecimalPublicProperty GDPin2010() AsDecimalGetReturn privateGDPin2010
EndGetPrivateSet(ByVal value AsDecimal)
privateGDPin2010 = value
EndSetEndPropertyPrivate privateCountryName AsStringPublicProperty CountryName() AsStringGetReturn privateCountryName
EndGetPrivateSet(ByVal value AsString)
privateCountryName = value
EndSetEndPropertyPrivate privateToolTipData As ToolTipData
PublicProperty ToolTipData() As ToolTipData
GetReturn privateToolTipData
EndGetSet(ByVal value As ToolTipData)
privateToolTipData = value
EndSetEndPropertyPublicSubNew(ByVal GDPs As List(Of GDP))
ToolTipData = New ToolTipData(GDPs, GDPs(0).Country)
CountryName = GDPs(0).Country
GDPin2010 = GDPs(9).Product
EndSubEndClassPublicClass ToolTipData
Private privateGDPs As List(Of GDP)
PublicProperty GDPs() As List(Of GDP)
GetReturn privateGDPs
EndGetPrivateSet(ByVal value As List(Of GDP))
privateGDPs = value
EndSetEndPropertyPrivate privateSeriesBrush As SolidColorBrush
PublicProperty SeriesBrush() As SolidColorBrush
GetReturn privateSeriesBrush
EndGetSet(ByVal value As SolidColorBrush)
privateSeriesBrush = value
EndSetEndPropertyPrivate privateTitle AsStringPublicProperty Title() AsStringGetReturn privateTitle
EndGetPrivateSet(ByVal value AsString)
privateTitle = value
EndSetEndPropertyPublicSubNew(ByVal gdps As List(Of GDP), ByVal countryName AsString)
GDPs = gdps
Title = countryName & " GDP History"EndSubEndClassEndNamespace
Was this page helpful?
Thanks for your feedback!
How can we improve this help topic?
Additional comments/thoughts:
If you have any questions, submit a ticket to our Support Center.