9 Answers, 1 is accepted
First you should set the SeriesItemLabel to be HitTestVisible by using a style. Next subscribe to the MouseUp event of the ChartArea and recursively walk up the visual tree until you find a SeriesItemLabel. This should be done to insure the user has specifically clicked it instead of something else (a grid line for example). The recursive method can look like this:
private
T GetParent<T>(DependencyObject obj)
where T : DependencyObject
{
if
(obj ==
null
)
{
return
null
;
}
else
if
(obj
is
T)
{
return
(T)obj;
}
DependencyObject parent = VisualTreeHelper.GetParent(obj);
return
GetParent<T>(parent);
}
void
ChartArea_MouseLeftButtonUp(
object
sender, MouseButtonEventArgs e)
{
SeriesItemLabel clickedLabel = GetParent<SeriesItemLabel>(e.OriginalSource
as
DependencyObject);
if
(clickedLabel !=
null
)
{
SeriesItemLabel_Click(clickedLabel, e);
}
}
The solution above is demonstrated in a sample application that is attached. Please review it and if you have any more questions don't hesitate to contact us!
Yavor Ivanov
the Telerik team
I've attempted to use the same technique (walking the VisualTree based on a mouse event). Specifically, I used the ChartArea.MouseMove event to detect a SeriesitemLabel and set the tooltip if not already set. Unfortunately, this has the glitch of only working on subsequent hovers. You have to hover over the label, move off, and then hover again before the tooltip will show. I am sure some user will simply think it only works sporadically
Is there a better way to set the SeriesItemLabel Tooltip. If so, I'd appreciate an example. Thanks.
RadChart supports tool tips for the series items. You can enable them by setting the SeriesDefinition.ShowItemToolTips to True.
More information on how do display and customize tool tips can be found in this topic in our help system. Please review this approach and let us know if it achieves the expected behavior.
All the best,
Yavor Ivanov
the Telerik team
You can achieve this behavior by setting a custom template for the SeriesItemLabel and using ToolTipService like this:
<
Style
x:Key
=
"SeriesItemLabelStyle"
TargetType
=
"telerik:SeriesItemLabel"
>
<
Setter
Property
=
"HorizontalContentAlignment"
Value
=
"Center"
/>
<
Setter
Property
=
"Padding"
Value
=
"2,0"
/>
<
Setter
Property
=
"IsHitTestVisible"
Value
=
"True"
/>
<
Setter
Property
=
"Foreground"
Value
=
"Black"
/>
<
Setter
Property
=
"Template"
>
<
Setter.Value
>
<
ControlTemplate
TargetType
=
"telerik:SeriesItemLabel"
>
<
Canvas
x:Name
=
"PART_MainContainer"
>
<
Path
Visibility
=
"{TemplateBinding ConnectorVisibility}"
Style
=
"{TemplateBinding ConnectorStyle}"
Stroke
=
"{TemplateBinding Stroke}"
StrokeThickness
=
"{TemplateBinding StrokeThickness}"
>
<
Path.Data
>
<
PathGeometry
>
<
PathGeometry.Figures
>
<
PathFigure
x:Name
=
"PART_Connector"
>
<
PathFigure.Segments
>
<
PolyLineSegment
/>
</
PathFigure.Segments
>
</
PathFigure
>
</
PathGeometry.Figures
>
</
PathGeometry
>
</
Path.Data
>
</
Path
>
<
Border
x:Name
=
"PART_TextContainer"
Style
=
"{TemplateBinding LabelStyle}"
BorderBrush
=
"{TemplateBinding Stroke}"
>
<
TextBlock
TextAlignment
=
"{TemplateBinding HorizontalContentAlignment}"
Margin
=
"{TemplateBinding Padding}"
Text
=
"{TemplateBinding Content}"
ToolTipService.ToolTip
=
"{TemplateBinding Content}"
>
</
TextBlock
>
</
Border
>
</
Canvas
>
</
ControlTemplate
>
</
Setter.Value
>
</
Setter
>
</
Style
>
Then you have only to specify the new style in the series definition like this:
<
telerik:LineSeriesDefinition
SeriesItemLabelStyle
=
"{StaticResource SeriesItemLabelStyle}"
>
You can read more about the ToolTipService in this MSDN topic. Regards,
Yavor Ivanov
the Telerik team
Please provide an example that binds to the SeriesItem.Tooltip Property or any mechanism which is settable via a databinding to the controls DataContext. Thanks,
public
static
class
SeriesLabelTooltipBehavior
{
public
static
readonly
DependencyProperty TooltipProperty = DependencyProperty.RegisterAttached(
"Tooltip"
,
typeof
(
string
),
typeof
(SeriesLabelTooltipBehavior),
new
PropertyMetadata(OnTooltipChanged));
public
static
string
GetTooltip(DependencyObject d)
{
return
(
string
)d.GetValue(TooltipProperty);
}
public
static
void
SetTooltip(DependencyObject d,
string
value)
{
d.SetValue(TooltipProperty, value);
}
private
static
void
OnTooltipChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = d
as
FrameworkElement;
if
(element !=
null
)
{
element.Loaded +=
new
RoutedEventHandler(element_Loaded);
}
}
static
void
element_Loaded(
object
sender, RoutedEventArgs e)
{
FrameworkElement element = (FrameworkElement) sender;
SeriesItemLabel clickedLabel = GetParent<SeriesItemLabel>(element);
if
(clickedLabel !=
null
)
{
if
(ToolTipService.GetToolTip(clickedLabel) ==
null
)
{
string
toolTip = (clickedLabel.DataPoint.DataItem
as
YourDataContext).ToolTip;
ToolTipService.SetToolTip(clickedLabel, toolTip);
}
}
}
private
static
T GetParent<T>(DependencyObject obj) where T : DependencyObject
{
T parent =
null
;
if
(obj !=
null
)
{
if
(obj
is
T)
{
parent = (T)obj;
}
else
{
DependencyObject nextParent = VisualTreeHelper.GetParent(obj);
parent = GetParent<T>(nextParent);
}
}
return
parent;
}
}
The DataContext of the SeriesItemLabel is set to its corresponding DataPoint. DataPoints have a DataItem property that is the source of the binding in databinding scenarios. You can bind the tooltip to any property of your business object like this:
ToolTipService.ToolTip="{Binding Path=DataItem.<
MyProperty
>}"
I hope this information gets you started properly.
Regards,Yavor Ivanov
the Telerik team