Skip to content

Commit f5b0cfe

Browse files
authored
Update README.md
1 parent 3945e21 commit f5b0cfe

File tree

1 file changed

+132
-2
lines changed

1 file changed

+132
-2
lines changed

README.md

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,132 @@
1-
# How-to-Add-Multiple-Trackballs-in-a-WPF-SfChart.
2-
Learn how to add multiple trackballs to a single WPF SfChart and drag them independently to view the information of different data points at the same time.
1+
# How to add multiple trackballs in a WPF SfChart.
2+
This article provides a detailed walkthrough on how to add multiple trackballs in an [SfChart](https://help.syncfusion.com/wpf/charts/getting-started) in WPF, allowing you to hover over the trackball with your mouse and move them independently to view the information of different data points simultaneously.
3+
4+
Learn step-by-step instructions and gain insights to add multiple trackballs in a WPF SfChart.
5+
6+
**Step 1:** Create a custom ChartTrackBallBehaviorExt class, which is inherited from [ChartTrackballBehavior](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.ChartTrackBallBehavior.html#).
7+
8+
C#
9+
10+
```csharp
11+
public class ChartTrackBallBehaviorExt : ChartTrackBallBehavior
12+
{
13+
14+
}
15+
```
16+
17+
**Step 2:** Create instances of ChartTrackBallBehaviorExt, and add them to the [Behaviors](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.SfChart.html#Syncfusion_UI_Xaml_Charts_SfChart_Behaviors) collection, assigning specific names to each.
18+
19+
XAML
20+
21+
```XAML
22+
<chart:SfChart.Behaviors>
23+
<local:ChartTrackBallBehaviorExt x:Name="trackball1"/>
24+
<local:ChartTrackBallBehaviorExt x:Name="trackball2"/>
25+
</chart:SfChart.Behaviors>
26+
```
27+
28+
**Step 3:** Override the **OnContentRendered** method to run the asynchronous task that calls ShowTrackball().
29+
30+
C#
31+
32+
```csharp
33+
protected override void OnContentRendered(EventArgs e)
34+
{
35+
base.OnContentRendered(e);
36+
37+
Task.Run(async () =>
38+
{
39+
await ShowTrackball();
40+
});
41+
}
42+
```
43+
44+
**Step 4:** Implement the **ShowTrackball** method to calculate positions and display the trackballs at load time by using the Show method.
45+
46+
C#
47+
48+
```csharp
49+
async Task ShowTrackball()
50+
{
51+
await Task.Delay(1000);
52+
Application.Current.Dispatcher.Invoke(() =>
53+
{
54+
float xPosition = (float)chart.ValueToPoint(chart.PrimaryAxis, 1);
55+
float yPosition = (float)chart.ValueToPoint(chart.SecondaryAxis, 169);
56+
57+
float xPosition1 = (float)chart.ValueToPoint(chart.PrimaryAxis, 6);
58+
float yPosition1 = (float)chart.ValueToPoint(chart.SecondaryAxis, 170);
59+
60+
// Show the first trackball
61+
trackball1.Show(xPosition, yPosition);
62+
63+
// Show the second trackball
64+
trackball2.Show(xPosition1, yPosition1);
65+
});
66+
}
67+
```
68+
69+
**Step 5:** Interact with multiple trackballs by overriding the **Mouse Event handlers** of [ChartTrackBallBehavior](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.ChartTrackBallBehavior.html) class. The **isActivated** variable is used to track whether a specific trackball is currently active, ensuring that only the relevant trackball responds to mouse events.
70+
71+
C#
72+
73+
```csharp
74+
protected override void OnMouseEnter(MouseEventArgs e)
75+
{
76+
var touchPoint = e.GetPosition(null);
77+
var trackball = FindNearestTrackball(touchPoint);
78+
79+
if (trackball == this)
80+
{
81+
isActivated = true;
82+
base.OnMouseEnter(e);
83+
}
84+
}
85+
86+
protected override void OnMouseMove(MouseEventArgs e)
87+
{
88+
if (isActivated)
89+
{
90+
var touchPoint = e.GetPosition(null);
91+
Show((float)touchPoint.X, (float)touchPoint.Y);
92+
base.OnMouseMove(e);
93+
}
94+
}
95+
96+
protected override void OnMouseLeave(MouseEventArgs e)
97+
{
98+
isActivated = false;
99+
}
100+
```
101+
102+
**Step 6:** The **FindNearestTrackball** method identifies the trackball closest to the user’s touch point, determining which trackball should be activated or moved based on user interaction.
103+
104+
C#
105+
106+
```csharp
107+
private ChartTrackBallBehavior FindNearestTrackball(Point touchPoint)
108+
{
109+
ChartTrackBallBehavior nearestTrackball = null;
110+
double minDistance = double.MaxValue;
111+
112+
foreach (var trackballBehavior in sfChart.Behaviors)
113+
{
114+
if (trackballBehavior is ChartTrackBallBehaviorExt trackballExt)
115+
{
116+
var distance = Math.Sqrt(Math.Pow(trackballExt.X - touchPoint.X, 2) + Math.Pow(trackballExt.Y - touchPoint.Y, 2));
117+
118+
if (distance < minDistance)
119+
{
120+
minDistance = distance;
121+
nearestTrackball = trackballExt;
122+
}
123+
}
124+
}
125+
return nearestTrackball;
126+
}
127+
```
128+
129+
**Step 7:** To control the trackballs, simply hover over them with your mouse. As you move the mouse within the chart area, the trackball will follow the cursor, allowing you to inspect different data points interactively.
130+
131+
For a complete example of this implementation, you can refer to the sample project on GitHub [here](https://github.com/SyncfusionExamples/How-to-Add-Multiple-Trackballs-in-a-WPF-SfChart). This sample demonstrates how to add and interact with multiple trackballs in a WPF SfChart, providing a practical reference to complement the steps outlined in this article.
132+

0 commit comments

Comments
 (0)