How to show the trackball programmatically in WPF Chart?
The trackball feature in the WPF Chart (SfChart) allows you to track the data point closest to the cursor position.
The following steps describe how to programmatically show or hide the trackball when the chart loads.
Step 1: Create a custom ChartTrackBallBehaviorExt class, which is inherited from the ChartTrackBallBehavior.
Step 2: Create an instance of ChartTrackBallBehaviorExt, and add it to the Behaviors collection.
<syncfusion:SfChart.Behaviors> <local:ChartTrackBallBehaviorExt x:Name="trackball" /> </syncfusion:SfChart.Behaviors>
Step 3: Add the Show and Hide method in the extended class, which is used to activate or deactivate the trackball at the specified location.
public class ChartTrackBallBehaviorExt : ChartTrackBallBehavior
{
public void Show(float x, float y)
{
IsActivated = true;
var point = new Point(x, y);
SetInternalProperty(typeof(ChartTrackBallBehavior), this, point, "CurrentPoint");
base.OnPointerPositionChanged();
InvokeInternalMethod(typeof(ChartTrackBallBehavior), this, "Activate", IsActivated);
}
public void Hide()
{
IsActivated = false;
}
internal static void SetInternalProperty(Type type, object obj, object value, string propertyName)
{
var properties = type.GetRuntimeProperties();
foreach (var item in properties)
{
if (item.Name == propertyName)
{
item.SetValue(obj, value);
break;
}
}
}
internal static object InvokeInternalMethod(Type type, object obj, string methodName, params object[] args)
{
var method = type.GetTypeInfo().GetDeclaredMethod(methodName);
return method != null ? method.Invoke(obj, args) : null;
}
}
Step 4: You can show the trackball at the load time by calling the Show method in the LayoutUpdated event of the chart.
<syncfusion:SfChart x:Name="chart" LayoutUpdated="chart_LayoutUpdated" Header="Inflation - Consumer Price"> ... <syncfusion:SfChart.Behaviors> <local:ChartTrackBallBehaviorExt x:Name="trackball" /> </syncfusion:SfChart.Behaviors> </syncfusion:SfChart>
Step 5: In the LayoutUpdated event method, convert the data point values to the pixel values and pass to the Show method where you need to show the trackball.
public partial class MainWindow : Window
{
…
private void chart_LayoutUpdated(object sender, EventArgs e)
{
//Passing x, y position to display the trackball with the specified location.
float xPosition = (float)chart.ValueToPoint(chart.PrimaryAxis, 2);
float yPosition = (float)chart.ValueToPoint(chart.SecondaryAxis, 48);
trackball.Show(xPosition, yPosition);
}
}
Step 6: If you want to hide the trackball programmatically, invoke the Hide method of the trackball extended class where you need.
… trackball.Hide();
Output

Conclusion
I hope you enjoyed learning how to show the trackball programmatically in WPF Chart.
You can refer to our WPF Chart feature tour page know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart Examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!