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+ }
0 commit comments