# TrackBall

Along with [tooltip](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/behaviors/tooltip) and [pan/zoom](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/behaviors/panzoom) behaviors, RadChartView provides a trackball visualization through the **ChartTrackBallBehavior** class.

This behavior can display a vertical line across the chart plot area and little visual indicators (circles by default) at points where the trackball line crosses the visualization of a series object. For example, when the trackball line crosses a line series line segment, a small circle is drawn highlighting the value of the series at this point.

The behavior also displays a small popup (the trackball info), similar to the tooltip, in order to provide more detailed information about the points closest to the trackball line's cross section (see Figure 1).

## Enable the TrackBall

To enable the trackball visualization, add a new **ChartTrackBallBehavior** instance in the **Behaviors** collection of the RadCartesianChart control.

**Example 1: Enabling the trackball**

```XAML
<telerik:RadCartesianChart Palette="Windows8">
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior />
    </telerik:RadCartesianChart.Behaviors>

    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis/>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis/>
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.Series>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="4"/>
                <telerik:CategoricalDataPoint Category="B" Value="8"/>
                <telerik:CategoricalDataPoint Category="C" Value="7"/>
                <telerik:CategoricalDataPoint Category="D" Value="9"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="7"/>
                <telerik:CategoricalDataPoint Category="B" Value="2"/>
                <telerik:CategoricalDataPoint Category="C" Value="18"/>
                <telerik:CategoricalDataPoint Category="D" Value="15"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
    </telerik:RadCartesianChart.Series>
</telerik:RadCartesianChart>
```

> caption RadCartesianChart with trackball

![Telerik UI for WPF RadChartView with the trackball behavior enabled](https://www.telerik.com/products/wpf/documentation/assets/a265918da05d8a290bfdb1a679480ab8/radchartview-features-trackball-0.png)

> By default the trackball shows the preview line that follows the data points closest to the mouse position and also the track info control (the information tooltip). The ellipses that snap to the data points are not displayed until explicitly enabled. Read more in the [Toggle the TrackBall Visual Elements](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/behaviors/trackball#toggle-the-trackball-visual-elements) section of this article.

## Snap Mode

The **SnapMode** property of ChartTrackBallBehavior determines how the trackball **line** will be snapped to the data points found by the trackball behavior. The property is enum accepting the following values:

* **None**: This mode disables the snapping of the line.

> caption SnapMode: none

```
![Telerik UI for WPF RadChartView trackball with SnapMode set to None](images/radchartview-features-trackball-1.png)
```

* **ClosestPoint** (default): This mode snaps the trackball line to the closest point of all data points in the chart.

> caption SnapMode: ClosestPoint

```
![Telerik UI for WPF RadChartView trackball snapped to the closest point](images/radchartview-features-trackball-2.png)
```

* **AllClosestPoints**: This mode snaps the trackball line to the closest point from each series object in the chart.

> caption SnapMode: AllClosestPoints

```
![Telerik UI for WPF RadChartView trackball snapped to the closest point in each series](images/radchartview-features-trackball-3.png)
```

**Example 2: Setting the SnapMode**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior SnapMode="None"/>
    </telerik:RadCartesianChart.Behaviors>
</telerik:RadCartesianChart>
```

## Snap Multiple Points Per Series

By default the trackball will display information only for a single data point per series. To change this set the **SnapSinglePointPerSeries** property to False. This way if you have multiple points in a single series, that are considered close to the mouse, all of them will be shown by the trackball.

**Example 3: Setting the SnapSinglePointPerSeries property**

```XAML
<telerik:RadCartesianChart Palette="Windows8">
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior ShowIntersectionPoints="True" SnapSinglePointPerSeries="False" />
    </telerik:RadCartesianChart.Behaviors>

    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis/>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis/>
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.Series>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="4"/>
                <telerik:CategoricalDataPoint Category="B" Value="8"/>
                <telerik:CategoricalDataPoint Category="B" Value="5"/>
                <telerik:CategoricalDataPoint Category="C" Value="7"/>
                <telerik:CategoricalDataPoint Category="D" Value="9"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="7"/>
                <telerik:CategoricalDataPoint Category="B" Value="2"/>
                <telerik:CategoricalDataPoint Category="D" Value="15"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
    </telerik:RadCartesianChart.Series>
</telerik:RadCartesianChart>
```

> caption Snap to multiple points series

![Telerik UI for WPF RadChartView trackball snapped to multiple series points](https://www.telerik.com/products/wpf/documentation/assets/bf4f58de34d944caaec3bb6cf403cfd4/radchartview-features-trackball-4.png)

## Toggle the TrackBall Visual Elements

The trackball consists of three main visuals - line, info tooltip and intersection points. You can toggle their visibility via few properties.

To show/hide the info tooltip (the trackball info control), set the **ShowTrackInfo** property.

To show/hide the intersection points, set the **ShowIntersectionPoints** property.

To show/hide the line set its Visibility via the **TrackBallLineStyle** property of the RadCartesianChart.

**Example 4: Toggle the trackball visuals**

```XAML
<telerik:RadCartesianChart Palette="Windows8">
    <telerik:RadCartesianChart.TrackBallLineStyle>
        <Style TargetType="Polyline">
            <Setter Property="Visibility" Value="Collapsed" />
        </Style>
    </telerik:RadCartesianChart.TrackBallLineStyle>
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior ShowIntersectionPoints="True" ShowTrackInfo="False"/>
    </telerik:RadCartesianChart.Behaviors>

    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis/>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis/>
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.Series>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="4"/>
                <telerik:CategoricalDataPoint Category="B" Value="8"/>
                <telerik:CategoricalDataPoint Category="C" Value="7"/>
                <telerik:CategoricalDataPoint Category="D" Value="9"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="7"/>
                <telerik:CategoricalDataPoint Category="B" Value="2"/>
                <telerik:CategoricalDataPoint Category="D" Value="15"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
    </telerik:RadCartesianChart.Series>
</telerik:RadCartesianChart>
```

> caption TrackBall with only intersection points shown

![Telerik UI for WPF RadChartView trackball showing only intersection points](https://www.telerik.com/products/wpf/documentation/assets/bd6ee69420eefe93687649bfbac13fd1/radchartview-features-trackball-5.png)

## Customize the TrackBall Visual Elements

The chart provides several properties that allows customizing the trackball.

To customize the trackball line, set the **TrackBallLineStyle** property. The property accepts a Style object with its TargetType set to **Polyline**.

To customize the trackball info, set the **TrackBallInfoStyle** property. The property accepts a Style object with its TargetType set to **TrackBallInfoControl**.

**Example 5: Setting the line and trackball info styles**

```XAML
<telerik:RadCartesianChart Palette="Windows8">
    <telerik:RadCartesianChart.TrackBallInfoStyle>
        <Style TargetType="telerik:TrackBallInfoControl">
            <Setter Property="Background" Value="Bisque" />
            <Setter Property="Header" Value="My Trackball Header" />
        </Style>
    </telerik:RadCartesianChart.TrackBallInfoStyle>
    <telerik:RadCartesianChart.TrackBallLineStyle>
        <Style TargetType="Polyline">
            <Setter Property="Stroke" Value="Red" />
        </Style>
    </telerik:RadCartesianChart.TrackBallLineStyle>
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior />
    </telerik:RadCartesianChart.Behaviors>

    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis/>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis/>
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.Series>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="4"/>
                <telerik:CategoricalDataPoint Category="B" Value="8"/>
                <telerik:CategoricalDataPoint Category="C" Value="7"/>
                <telerik:CategoricalDataPoint Category="D" Value="9"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
        <telerik:LineSeries>
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="7"/>
                <telerik:CategoricalDataPoint Category="B" Value="2"/>
                <telerik:CategoricalDataPoint Category="D" Value="15"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
    </telerik:RadCartesianChart.Series>
</telerik:RadCartesianChart>
```

> caption Customized line and trackball info

![Telerik UI for WPF RadChartView trackball showing the preview line and information tooltip](https://www.telerik.com/products/wpf/documentation/assets/a5eadcb12aa2a91bb024dd118af47a2e/radchartview-features-trackball-6.png)

The trackball intersection points can be customized per series via the **TrackBallTemplate** property of the corresponding chart series.

Additionally, you can customize the information for each series via the **TrackBallInfoTemplate** property of the corresponding chart series.

**Example 6: Setting the line and trackball templates**

```XAML
<telerik:RadCartesianChart Palette="Windows8">
    <telerik:RadCartesianChart.Resources>
        <DataTemplate x:Key="trackBallTemplate">
            <Rectangle Width="16" Height="16" Fill="Bisque" Stroke="Black" StrokeThickness="1">
                <Rectangle.LayoutTransform>
                    <RotateTransform Angle="45" />
                </Rectangle.LayoutTransform>
            </Rectangle>
        </DataTemplate>
        <DataTemplate x:Key="trackBallInfoTemplate">
            <StackPanel Background="Wheat" Margin="3" Width="100">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="X=" FontWeight="Bold" />
                    <TextBlock Text="{Binding DataPoint.Category}" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Y=" FontWeight="Bold" />
                    <TextBlock Text="{Binding DataPoint.Value}" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </telerik:RadCartesianChart.Resources>
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior ShowIntersectionPoints="True" />
    </telerik:RadCartesianChart.Behaviors>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis/>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis/>
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.Series>
        <telerik:LineSeries TrackBallTemplate="{StaticResource trackBallTemplate}" 
							TrackBallInfoTemplate="{StaticResource trackBallInfoTemplate}">
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="4"/>
                <telerik:CategoricalDataPoint Category="B" Value="8"/>
                <telerik:CategoricalDataPoint Category="C" Value="7"/>
                <telerik:CategoricalDataPoint Category="D" Value="9"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
        <telerik:LineSeries TrackBallTemplate="{StaticResource trackBallTemplate}" 
							TrackBallInfoTemplate="{StaticResource trackBallInfoTemplate}">
            <telerik:LineSeries.DataPoints>
                <telerik:CategoricalDataPoint Category="A" Value="7"/>
                <telerik:CategoricalDataPoint Category="B" Value="2"/>
                <telerik:CategoricalDataPoint Category="D" Value="15"/>
            </telerik:LineSeries.DataPoints>
        </telerik:LineSeries>
    </telerik:RadCartesianChart.Series>
</telerik:RadCartesianChart>
```

> caption Customized line and trackball templates

![Telerik UI for WPF RadChartView trackball with customized visual elements](https://www.telerik.com/products/wpf/documentation/assets/d7cc45a6a0ec55bf32feac33b4798a85/radchartview-features-trackball-7.png)

## Update the TrackBall Position

ChartTrackBallBehavior exposes a **Position** property that can be used to manually change the position of the trackball. The property accepts a value of type **Sytem.Windows.Point**. Setting the Position will display and snap the trackball to the data points that are plotted closest to the set position. The position is defined in pixels relative to the chart.

For example, if the chart is 500px wide and 300px high, and the Position property is set to new Point(250, 150) the trackball will be placed at the data points plotted closest to the 250th horizontal and 150th vertical pixels of the chart.

**Example 7: Updating the trackball position**

```C#
trackballBehavior.Position = new Point(250, 150);
```

```VB.NET
trackballBehavior.Position = New Point(250, 150)
```

This behavior also provides the **PositionChanging** event, which is called on each change of the Position property. The event arguments expose a couple of helpful properties:

* **PreviousPosition**: Gets the previous position of the trackball.
* **NewPosition**: Gets or sets the new position of the trackball.

> The PositionChanging event will be called only if the old and the new positions are different.



> You can find a runnable project that demonstrates manipulating the position of the trackball in the [TrackBallSyncedCharts](https://github.com/telerik/xaml-sdk/tree/master/ChartView/WPF/TrackBallSyncedCharts)

SDK example.

## Events

The behavior exposes the **TrackInfoUpdated** event that is useful if you want to modify the trackball info that is displayed in the tooltip. The **TrackBallInfoEventArgs** contain information about the data points near the mouse position.

**Example 8: Subscribing to the TrackInfoUpdated event**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior TrackInfoUpdated="ChartTrackBallBehavior_TrackInfoUpdated"/>
    </telerik:RadCartesianChart.Behaviors>
</telerik:RadCartesianChart>
```

**Example 9: Canceling the trackball showing when the mouse is close to a specific category**

```C#
private void ChartTrackBallBehavior_TrackInfoUpdated(object sender, Telerik.Windows.Controls.ChartView.TrackBallInfoEventArgs e)
{
    var closestDataPoint = (CategoricalDataPoint)e.Context.ClosestDataPoint.DataPoint;
    if (closestDataPoint.Category.Equals("C"))
    {
        e.Context.DataPointInfos.Clear();
    }
}
```

```VB.NET
Private Sub ChartTrackBallBehavior_TrackInfoUpdated(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.ChartView.TrackBallInfoEventArgs)
	Dim closestDataPoint = CType(e.Context.ClosestDataPoint.DataPoint, CategoricalDataPoint)

	If closestDataPoint.Category.Equals("C") Then
		e.Context.DataPointInfos.Clear()
	End If
End Sub
```

## See Also

* [Tooltip](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/behaviors/tooltip)
* [Scroll and Zoom](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/behaviors/panzoom)
