Skip to content

Commit 727e8e9

Browse files
Updated the trackball sample
1 parent 85de038 commit 727e8e9

File tree

9 files changed

+364
-0
lines changed

9 files changed

+364
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"Version": 1,
3+
"WorkspaceRootPath": "D:\\WPF Samples\\Synchronize_Trackball\\",
4+
"Documents": [
5+
{
6+
"AbsoluteMoniker": "D:0:0:{2AE8056D-FC80-4398-A603-35E59086C4CA}|Synchronize_Trackball.csproj|d:\\wpf samples\\synchronize_trackball\\mainwindow.xaml||{F11ACC28-31D1-4C80-A34B-F4E09D3D753C}",
7+
"RelativeMoniker": "D:0:0:{2AE8056D-FC80-4398-A603-35E59086C4CA}|Synchronize_Trackball.csproj|solutionrelative:mainwindow.xaml||{F11ACC28-31D1-4C80-A34B-F4E09D3D753C}"
8+
},
9+
{
10+
"AbsoluteMoniker": "D:0:0:{2AE8056D-FC80-4398-A603-35E59086C4CA}|Synchronize_Trackball.csproj|d:\\wpf samples\\synchronize_trackball\\mainwindow.xaml.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
11+
"RelativeMoniker": "D:0:0:{2AE8056D-FC80-4398-A603-35E59086C4CA}|Synchronize_Trackball.csproj|solutionrelative:mainwindow.xaml.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
12+
}
13+
],
14+
"DocumentGroupContainers": [
15+
{
16+
"Orientation": 0,
17+
"VerticalTabListWidth": 256,
18+
"DocumentGroups": [
19+
{
20+
"DockedWidth": 200,
21+
"SelectedChildIndex": 4,
22+
"Children": [
23+
{
24+
"$type": "Bookmark",
25+
"Name": "ST:0:0:{b1e99781-ab81-11d0-b683-00aa00a3ee26}"
26+
},
27+
{
28+
"$type": "Bookmark",
29+
"Name": "ST:0:0:{34e76e81-ee4a-11d0-ae2e-00a0c90fffc3}"
30+
},
31+
{
32+
"$type": "Bookmark",
33+
"Name": "ST:0:0:{d78612c7-9962-4b83-95d9-268046dad23a}"
34+
},
35+
{
36+
"$type": "Document",
37+
"DocumentIndex": 1,
38+
"Title": "MainWindow.xaml.cs",
39+
"DocumentMoniker": "D:\\WPF Samples\\Synchronize_Trackball\\MainWindow.xaml.cs",
40+
"RelativeDocumentMoniker": "MainWindow.xaml.cs",
41+
"ToolTip": "D:\\WPF Samples\\Synchronize_Trackball\\MainWindow.xaml.cs",
42+
"RelativeToolTip": "MainWindow.xaml.cs",
43+
"ViewState": "AgIAAEcAAAAAAAAAAAAuwGAAAAAFAAAAAAAAAA==",
44+
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
45+
"WhenOpened": "2024-12-13T10:58:11.468Z",
46+
"EditorCaption": ""
47+
},
48+
{
49+
"$type": "Document",
50+
"DocumentIndex": 0,
51+
"Title": "MainWindow.xaml",
52+
"DocumentMoniker": "D:\\WPF Samples\\Synchronize_Trackball\\MainWindow.xaml",
53+
"RelativeDocumentMoniker": "MainWindow.xaml",
54+
"ToolTip": "D:\\WPF Samples\\Synchronize_Trackball\\MainWindow.xaml",
55+
"RelativeToolTip": "MainWindow.xaml",
56+
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.003549|",
57+
"WhenOpened": "2024-12-13T10:58:05.535Z",
58+
"EditorCaption": ""
59+
}
60+
]
61+
}
62+
]
63+
}
64+
]
65+
}

Synchronize_Trackball/App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="Synchronize_Trackball.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:Synchronize_Trackball"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

Synchronize_Trackball/App.xaml.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Configuration;
2+
using System.Data;
3+
using System.Windows;
4+
5+
namespace Synchronize_Trackball
6+
{
7+
/// <summary>
8+
/// Interaction logic for App.xaml
9+
/// </summary>
10+
public partial class App : Application
11+
{
12+
}
13+
14+
}

Synchronize_Trackball/AssemblyInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]

Synchronize_Trackball/MainWindow.xaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<Window x:Class="Synchronize_Trackball.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:chart="clr-namespace:Syncfusion.UI.Xaml.Charts;assembly=Syncfusion.SfChart.WPF"
7+
xmlns:local="clr-namespace:Synchronize_Trackball"
8+
mc:Ignorable="d"
9+
Title="MainWindow" Height="450" Width="800">
10+
<Grid>
11+
<Grid.DataContext>
12+
<local:DataGenerator/>
13+
</Grid.DataContext>
14+
15+
<Grid.RowDefinitions>
16+
<RowDefinition />
17+
<RowDefinition />
18+
</Grid.RowDefinitions>
19+
20+
<chart:SfChart x:Name="chart1" MouseMove="SfChart_MouseMove1" Header="FirstChart" Margin="5">
21+
22+
<chart:SfChart.Behaviors>
23+
<local:CustomTrackBallBehavior x:Name="behavior1"/>
24+
</chart:SfChart.Behaviors>
25+
26+
<chart:SfChart.PrimaryAxis>
27+
<chart:DateTimeAxis ShowTrackBallInfo="True" x:Name="axis" LabelFormat="MM/dd/yy" />
28+
</chart:SfChart.PrimaryAxis>
29+
30+
<chart:SfChart.SecondaryAxis>
31+
<chart:NumericalAxis ShowTrackBallInfo="True" x:Name="secAxis"/>
32+
</chart:SfChart.SecondaryAxis>
33+
34+
<chart:FastLineSeries x:Name="series1" XBindingPath="Date" YBindingPath="Value"
35+
ItemsSource="{Binding DataCollection1}"/>
36+
37+
</chart:SfChart>
38+
39+
<chart:SfChart x:Name="chart2" MouseMove="SfChart_MouseMove2" Grid.Row="1" Grid.Column="0" Header="Second Chart" Margin="5">
40+
41+
<chart:SfChart.Behaviors>
42+
<local:CustomTrackBallBehavior x:Name="behavior2"/>
43+
</chart:SfChart.Behaviors>
44+
45+
<chart:SfChart.PrimaryAxis>
46+
<chart:DateTimeAxis ShowTrackBallInfo="True" LabelFormat="MM/dd/yy"/>
47+
</chart:SfChart.PrimaryAxis>
48+
49+
<chart:SfChart.SecondaryAxis>
50+
<chart:NumericalAxis ShowTrackBallInfo="True" />
51+
</chart:SfChart.SecondaryAxis>
52+
53+
<chart:FastLineSeries x:Name="series3" XBindingPath="Date" YBindingPath="Value"
54+
ItemsSource="{Binding DataCollection2}"/>
55+
</chart:SfChart>
56+
</Grid>
57+
</Window>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using System.Collections.ObjectModel;
2+
using Syncfusion.UI.Xaml.Charts;
3+
using System.Text;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
using System.Windows.Data;
7+
using System.Windows.Documents;
8+
using System.Windows.Input;
9+
using System.Windows.Media;
10+
using System.Windows.Media.Imaging;
11+
using System.Windows.Navigation;
12+
using System.Windows.Shapes;
13+
14+
namespace Synchronize_Trackball
15+
{
16+
/// <summary>
17+
/// Interaction logic for MainWindow.xaml
18+
/// </summary>
19+
public partial class MainWindow : Window
20+
{
21+
public Point MousePoint
22+
{
23+
get;
24+
set;
25+
}
26+
27+
public MainWindow()
28+
{
29+
InitializeComponent();
30+
}
31+
32+
private void SfChart_MouseMove1(object sender, MouseEventArgs e)
33+
{
34+
// Taking the first chart position as reference to get the mouse point.
35+
MousePoint = Mouse.GetPosition(behavior1.AdorningCanvas);
36+
var chart1 = (sender as SfChart);
37+
38+
if (chart1.SeriesClipRect.Contains(MousePoint))
39+
{
40+
// Generalizing position with respect to axis width.
41+
MousePoint = new Point(
42+
MousePoint.X - chart1.SeriesClipRect.Left,
43+
MousePoint.Y - chart1.SeriesClipRect.Top);
44+
45+
behavior1.ActivateTrackball(MousePoint);
46+
behavior2.ActivateTrackball(MousePoint);
47+
}
48+
else
49+
{
50+
behavior1.DeactivateTrackball();
51+
behavior2.DeactivateTrackball();
52+
}
53+
}
54+
55+
private void SfChart_MouseMove2(object sender, MouseEventArgs e)
56+
{
57+
// Taking the second chart position as reference to get the mouse point.
58+
MousePoint = Mouse.GetPosition(behavior2.AdorningCanvas);
59+
var chart2 = (sender as SfChart);
60+
61+
if (chart2.SeriesClipRect.Contains(MousePoint))
62+
{
63+
// Generalizing position with respect to axis width.
64+
MousePoint = new Point(
65+
MousePoint.X - chart2.SeriesClipRect.Left,
66+
MousePoint.Y - chart2.SeriesClipRect.Top);
67+
68+
behavior1.ActivateTrackball(MousePoint);
69+
behavior2.ActivateTrackball(MousePoint);
70+
}
71+
else
72+
{
73+
behavior1.DeactivateTrackball();
74+
behavior2.DeactivateTrackball();
75+
}
76+
}
77+
}
78+
79+
80+
public class CustomTrackBallBehavior : ChartTrackBallBehavior
81+
{
82+
public void ActivateTrackball(Point mousePoint)
83+
{
84+
IsActivated = true;
85+
OnPointerPositionChanged(mousePoint);
86+
}
87+
88+
public void DeactivateTrackball()
89+
{
90+
IsActivated = false;
91+
}
92+
93+
// This method is overriden to improve perfomance by preventing the mouse position calculation which we have done already.
94+
protected override void OnMouseMove(MouseEventArgs e)
95+
{
96+
}
97+
}
98+
99+
public class Data
100+
{
101+
public Data(DateTime date, double value)
102+
{
103+
Date = date;
104+
Value = value;
105+
}
106+
107+
public DateTime Date
108+
{
109+
get;
110+
set;
111+
}
112+
113+
public double Value
114+
{
115+
get;
116+
set;
117+
}
118+
}
119+
120+
public class DataGenerator
121+
{
122+
public int DataCount = 100;
123+
private Random randomNumber;
124+
public ObservableCollection<Data> DataCollection1 { get; set; }
125+
public ObservableCollection<Data> DataCollection2 { get; set; }
126+
127+
public DataGenerator()
128+
{
129+
randomNumber = new Random();
130+
DataCollection1 = GenerateData();
131+
DataCollection2 = GenerateData();
132+
}
133+
134+
public ObservableCollection<Data> GenerateData()
135+
{
136+
ObservableCollection<Data> datas = new ObservableCollection<Data>();
137+
DateTime date = new DateTime(2020, 1, 1);
138+
double value = 100;
139+
140+
for (int i = 0; i < this.DataCount; i++)
141+
{
142+
datas.Add(new Data(date, Math.Round(value, 2)));
143+
date = date.Add(TimeSpan.FromDays(1));
144+
145+
if (randomNumber.NextDouble() > .5)
146+
{
147+
value += randomNumber.NextDouble();
148+
}
149+
else
150+
{
151+
value -= randomNumber.NextDouble();
152+
}
153+
}
154+
155+
return datas;
156+
}
157+
}
158+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<UseWPF>true</UseWPF>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.SfChart.WPF" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup />
4+
<ItemGroup>
5+
<ApplicationDefinition Update="App.xaml">
6+
<SubType>Designer</SubType>
7+
</ApplicationDefinition>
8+
</ItemGroup>
9+
<ItemGroup>
10+
<Page Update="MainWindow.xaml">
11+
<SubType>Designer</SubType>
12+
</Page>
13+
</ItemGroup>
14+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Synchronize_Trackball", "Synchronize_Trackball.csproj", "{2AE8056D-FC80-4398-A603-35E59086C4CA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2AE8056D-FC80-4398-A603-35E59086C4CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2AE8056D-FC80-4398-A603-35E59086C4CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2AE8056D-FC80-4398-A603-35E59086C4CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2AE8056D-FC80-4398-A603-35E59086C4CA}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

0 commit comments

Comments
 (0)