Skip to content

Commit

Permalink
Added the sample
Browse files Browse the repository at this point in the history
  • Loading branch information
subash-sf committed Oct 29, 2024
1 parent 122ed5b commit 5c912ce
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 0 deletions.
25 changes: 25 additions & 0 deletions SfChartMultipleTrackball/SfChartMultipleTrackball.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfChartMultipleTrackball", "SfChartMultipleTrackball\SfChartMultipleTrackball.csproj", "{E5B30519-D065-42FC-B89A-B435C07011D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E5B30519-D065-42FC-B89A-B435C07011D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5B30519-D065-42FC-B89A-B435C07011D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5B30519-D065-42FC-B89A-B435C07011D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5B30519-D065-42FC-B89A-B435C07011D9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0CA07FB0-0F65-49F3-AFCC-11C28CE1D119}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions SfChartMultipleTrackball/SfChartMultipleTrackball/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="SfChartMultipleTrackball.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SfChartMultipleTrackball"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
14 changes: 14 additions & 0 deletions SfChartMultipleTrackball/SfChartMultipleTrackball/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Configuration;
using System.Data;
using System.Windows;

namespace SfChartMultipleTrackball
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

}
10 changes: 10 additions & 0 deletions SfChartMultipleTrackball/SfChartMultipleTrackball/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
64 changes: 64 additions & 0 deletions SfChartMultipleTrackball/SfChartMultipleTrackball/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<Window x:Class="SfChartMultipleTrackball.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SfChartMultipleTrackball"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
xmlns:chart="clr-namespace:Syncfusion.UI.Xaml.Charts;assembly=Syncfusion.SfChart.WPF">

<Grid>

<chart:SfChart x:Name="chart" Header="Server CPU load by Day">

<chart:SfChart.PrimaryAxis>
<chart:CategoryAxis ShowTrackBallInfo="True"/>
</chart:SfChart.PrimaryAxis>

<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis ShowTrackBallInfo="True" Minimum="0" Maximum="100"/>
</chart:SfChart.SecondaryAxis>

<chart:SfChart.Resources>
<Style TargetType="chart:ChartTrackBallControl" x:Key="trackballStyle1">
<Setter Property="Background" Value="GreenYellow"/>
<Setter Property="Height" Value="10"/>
<Setter Property="Width" Value="10"/>
</Style>
<Style TargetType="Line" x:Key="lineStyle1">
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1.5"/>
</Style>

<Style TargetType="chart:ChartTrackBallControl" x:Key="trackballStyle2">
<Setter Property="Background" Value="Orange"/>
<Setter Property="Height" Value="10"/>
<Setter Property="Width" Value="10"/>
</Style>
<Style TargetType="Line" x:Key="lineStyle2">
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1.5"/>
</Style>
</chart:SfChart.Resources>

<chart:SfChart.Behaviors>
<local:ChartTrackBallBehaviorExt x:Name="trackball1" ChartTrackBallStyle="{StaticResource trackballStyle1}" UseSeriesPalette="True" LineStyle="{StaticResource lineStyle1}"/>
<local:ChartTrackBallBehaviorExt x:Name="trackball2" ChartTrackBallStyle="{StaticResource trackballStyle2}" UseSeriesPalette="True" LineStyle="{StaticResource lineStyle2}"/>
</chart:SfChart.Behaviors>

<chart:LineSeries ItemsSource="{Binding Data}" XBindingPath="Day" YBindingPath="CPULoad" StrokeThickness="3" Interior="OrangeRed">
<chart:LineSeries.AdornmentsInfo>
<chart:ChartAdornmentInfo Symbol="Ellipse" SymbolInterior="White" SymbolStroke="OrangeRed"/>
</chart:LineSeries.AdornmentsInfo>
</chart:LineSeries>

</chart:SfChart>

</Grid>

<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>

</Window>
187 changes: 187 additions & 0 deletions SfChartMultipleTrackball/SfChartMultipleTrackball/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using Syncfusion.UI.Xaml.Charts;
using System.Reflection;
using System.Windows;
using System.Windows.Input;

namespace SfChartMultipleTrackball
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
trackball1.SfChart = this.chart;
trackball2.SfChart = this.chart;
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);

// Run the ShowTrackball method asynchronously
Task.Run(async () =>
{
await ShowTrackball();
});
}

async Task ShowTrackball()
{
// Wait for 1 second before executing the rest of the method
await Task.Delay(1000);
Application.Current.Dispatcher.Invoke(() =>
{
// Calculated positions for the first trackball
float xPosition = (float)chart.ValueToPoint(chart.PrimaryAxis, 1);
float yPosition = (float)chart.ValueToPoint(chart.SecondaryAxis, 169);

// Calculated positions for the second trackball
float xPosition1 = (float)chart.ValueToPoint(chart.PrimaryAxis, 6);
float yPosition1 = (float)chart.ValueToPoint(chart.SecondaryAxis, 170);

// Display the first trackball
trackball1.Display(xPosition, yPosition);

// Display the second trackball
trackball2.Display(xPosition1, yPosition1);
});
}
}

public class Model
{
public string Day { get; set; }
public double CPULoad { get; set; }
}

public class ViewModel
{
public List<Model> Data { get; set; }

public ViewModel()
{
Data = new List<Model>
{
new Model { Day = "Monday", CPULoad = 35 },
new Model { Day = "Tuesday", CPULoad = 42 },
new Model { Day = "Wednesday", CPULoad = 18 },
new Model { Day = "Thursday", CPULoad = 30 },
new Model { Day = "Friday", CPULoad = 64 },
new Model { Day = "Saturday", CPULoad = 22 },
new Model { Day = "Sunday", CPULoad = 10 }
};
}
}

public class ChartTrackBallBehaviorExt : ChartTrackBallBehavior
{
private bool isTrackballActive = false;

public SfChart SfChart { get; set; }

public double X { get; set; }
public double Y { get; set; }

protected override void OnMouseEnter(MouseEventArgs e)
{
// Get the position of the mouse pointer
var touchPoint = e.GetPosition(null);

// Find the nearest trackball to the mouse pointer
var trackball = FindNearestTrackball(touchPoint);

// Activate the trackball if it is the nearest one
if (trackball == this)
{
isTrackballActive = true;
base.OnMouseEnter(e);
}
}

protected override void OnMouseMove(MouseEventArgs e)
{
// Check if the trackball is activated
if (isTrackballActive)
{
// Get the position of the mouse pointer
var touchPoint = e.GetPosition(null);

// Display the trackball at the current mouse position
Display((float)touchPoint.X, (float)touchPoint.Y);
base.OnMouseMove(e);
}
}

protected override void OnMouseLeave(MouseEventArgs e)
{
// Deactivate the trackball
isTrackballActive = false;
}

private ChartTrackBallBehavior FindNearestTrackball(Point touchPoint)
{
ChartTrackBallBehavior nearestTrackball = null;
double minDistance = double.MaxValue;

// Iterate through all trackball behaviors to find the nearest one
foreach (var trackballBehaviour in SfChart.Behaviors)
{
if (trackballBehaviour is ChartTrackBallBehaviorExt trackball)
{
// Calculate the distance between the trackball and the touch point
double distance = Math.Sqrt(Math.Pow(trackball.X - touchPoint.X, 2) + Math.Pow(trackball.Y - touchPoint.Y, 2));

// Update the nearest trackball if the current one is closer
if (distance < minDistance)
{
minDistance = distance;
nearestTrackball = trackball;
}
}
}

return nearestTrackball;
}

public void Display(float x, float y)
{
X = x; Y = y;
IsActivated = true;
var point = new Point(x, y);

// Set the internal property for the current point
SetInternalProperty(typeof(ChartTrackBallBehavior), this, point, "CurrentPoint");

// Trigger the pointer position changed event
base.OnPointerPositionChanged();

// Activate the trackball
InvokeInternalMethod(typeof(ChartTrackBallBehavior), this, "Activate", IsActivated);
}

// Sets an internal property of an object using reflection.
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;
}
}
}

// Invokes an internal method of an object using reflection.
internal static object? InvokeInternalMethod(Type type, object obj, string methodName, params object[] args)
{
var method = type.GetTypeInfo().GetDeclaredMethod(methodName);
return method?.Invoke(obj, args);
}
}
}
14 changes: 14 additions & 0 deletions SfChartMultipleTrackball/SfChartMultipleTrackball/Model/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SfChartMultipleTrackball
{
public class Model
{
public string? Day { get; set; }
public double CPULoad { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.SfChart.WPF" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<ItemGroup>
<ApplicationDefinition Update="App.xaml">
<SubType>Designer</SubType>
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Page Update="MainWindow.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace SfChartMultipleTrackball
{
public class ViewModel
{
public List<Model> Data { get; set; }

public ViewModel()
{
Data = new List<Model>
{
new Model { Day = "Monday", CPULoad = 35 },
new Model { Day = "Tuesday", CPULoad = 42 },
new Model { Day = "Wednesday", CPULoad = 18 },
new Model { Day = "Thursday", CPULoad = 30 },
new Model { Day = "Friday", CPULoad = 64 },
new Model { Day = "Saturday", CPULoad = 22 },
new Model { Day = "Sunday", CPULoad = 10 }
};
}
}
}

0 comments on commit 5c912ce

Please sign in to comment.