Skip to content

Commit 417d366

Browse files
Merge pull request #1 from SyncfusionExamples/Add-sample
Added the sample for demonstrating how to add crosshair lines in .NET MAUI SfCartesianChart.
2 parents c42cb12 + 2d16993 commit 417d366

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+9365
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Syncfusion.Maui.Toolkit.Charts;
2+
3+
namespace ChartCrossHair.UsingAnnotations
4+
{
5+
public class CrossHairInteractiveBehavior : ChartInteractiveBehavior
6+
{
7+
public delegate void TouchMoveEventHandler(object sender, TouchMoveEventArgs e);
8+
9+
// Define an event based on the delegate
10+
public event TouchMoveEventHandler TouchMoveEvent;
11+
12+
public CrossHairInteractiveBehavior()
13+
{
14+
TouchMoveEvent += CrossHairInteractiveBehavior_TouchMoveEvent;
15+
}
16+
17+
private void CrossHairInteractiveBehavior_TouchMoveEvent(object sender, TouchMoveEventArgs e)
18+
{
19+
}
20+
21+
protected override void OnTouchMove(ChartBase chart, float pointX, float pointY)
22+
{
23+
base.OnTouchMove(chart, pointX, pointY);
24+
TouchMoveEvent?.Invoke(this, new TouchMoveEventArgs(pointX, pointY));
25+
}
26+
}
27+
28+
public class TouchMoveEventArgs : EventArgs
29+
{
30+
public float PointX { get; }
31+
public float PointY { get; }
32+
33+
public TouchMoveEventArgs(float pointX, float pointY)
34+
{
35+
PointX = pointX;
36+
PointY = pointY;
37+
}
38+
}
39+
}

ChartCrossHair/App.xaml

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+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:ChartCrossHair"
5+
x:Class="ChartCrossHair.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>

ChartCrossHair/App.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace ChartCrossHair
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
}
9+
10+
protected override Window CreateWindow(IActivationState? activationState)
11+
{
12+
return new Window(new AppShell());
13+
}
14+
}
15+
}

ChartCrossHair/AppShell.xaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="ChartCrossHair.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:ChartCrossHair"
7+
Shell.FlyoutBehavior="Flyout"
8+
Title="ChartCrossHair">
9+
10+
<ShellContent Title="CrossHair with GraphicsView"
11+
ContentTemplate="{DataTemplate local:WithGraphicsView}"
12+
Route="WithGraphicsView" />
13+
14+
<ShellContent Title="CrossHair with Annotation"
15+
ContentTemplate="{DataTemplate local:WithAnnotation}"
16+
Route="WithAnnotation" />
17+
18+
</Shell>

ChartCrossHair/AppShell.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace ChartCrossHair
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks>
6+
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
7+
<!-- <TargetFrameworks>$(TargetFrameworks);net9.0-tizen</TargetFrameworks> -->
8+
9+
<!-- Note for MacCatalyst:
10+
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
11+
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
12+
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
13+
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
14+
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
15+
16+
<OutputType>Exe</OutputType>
17+
<RootNamespace>ChartCrossHair</RootNamespace>
18+
<UseMaui>true</UseMaui>
19+
<SingleProject>true</SingleProject>
20+
<ImplicitUsings>enable</ImplicitUsings>
21+
<Nullable>enable</Nullable>
22+
23+
<!-- Display name -->
24+
<ApplicationTitle>ChartCrossHair</ApplicationTitle>
25+
26+
<!-- App Identifier -->
27+
<ApplicationId>com.companyname.addcrosshairtomauichart</ApplicationId>
28+
29+
<!-- Versions -->
30+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
31+
<ApplicationVersion>1</ApplicationVersion>
32+
33+
<!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged -->
34+
<WindowsPackageType>None</WindowsPackageType>
35+
36+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
37+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
38+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
39+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
40+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
41+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
42+
</PropertyGroup>
43+
44+
<ItemGroup>
45+
<!-- App Icon -->
46+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
47+
48+
<!-- Splash Screen -->
49+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
50+
51+
<!-- Images -->
52+
<MauiImage Include="Resources\Images\*" />
53+
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
54+
55+
<!-- Custom Fonts -->
56+
<MauiFont Include="Resources\Fonts\*" />
57+
58+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
59+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
60+
</ItemGroup>
61+
62+
<ItemGroup>
63+
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
64+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
65+
<PackageReference Include="Syncfusion.Maui.Toolkit" Version="*" />
66+
</ItemGroup>
67+
68+
<ItemGroup>
69+
<MauiXaml Update="Views\WithAnnotation.xaml">
70+
<Generator>MSBuild:Compile</Generator>
71+
</MauiXaml>
72+
<MauiXaml Update="Views\WithGraphicsView.xaml">
73+
<Generator>MSBuild:Compile</Generator>
74+
</MauiXaml>
75+
</ItemGroup>
76+
77+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<IsFirstTimeProjectOpen>False</IsFirstTimeProjectOpen>
5+
<ActiveDebugFramework>net9.0-windows10.0.19041.0</ActiveDebugFramework>
6+
<ActiveDebugProfile>Windows Machine</ActiveDebugProfile>
7+
</PropertyGroup>
8+
</Project>

ChartCrossHair/ChartCrossHair.sln

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35521.163
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChartCrossHair", "ChartCrossHair.csproj", "{26AF134E-FF0E-49C1-A320-9ED16639D6E9}"
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+
{26AF134E-FF0E-49C1-A320-9ED16639D6E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{26AF134E-FF0E-49C1-A320-9ED16639D6E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{26AF134E-FF0E-49C1-A320-9ED16639D6E9}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{26AF134E-FF0E-49C1-A320-9ED16639D6E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{26AF134E-FF0E-49C1-A320-9ED16639D6E9}.Release|Any CPU.Build.0 = Release|Any CPU
19+
EndGlobalSection
20+
GlobalSection(SolutionProperties) = preSolution
21+
HideSolutionNode = FALSE
22+
EndGlobalSection
23+
EndGlobal
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using Syncfusion.Maui.Toolkit.Charts;
2+
using Font = Microsoft.Maui.Graphics.Font;
3+
4+
namespace ChartCrossHair.UsingGraphicsView
5+
{
6+
public class CrossHairBehavior : GraphicsView, IDrawable
7+
{
8+
private bool isTouchActive = false;
9+
private string? CrossHairXValue;
10+
private string? CrossHairYValue;
11+
private string? CrossHairPointValue;
12+
private PointF CrossHairX1;
13+
private PointF CrossHairX2;
14+
private PointF CrossHairY1;
15+
private PointF CrossHairY2;
16+
17+
public static readonly BindableProperty ShowCrossHairProperty = BindableProperty.Create(nameof(ShowCrossHair), typeof(bool), typeof(CrossHairBehavior), false, BindingMode.Default, null);
18+
19+
public bool ShowCrossHair
20+
{
21+
get { return (bool)GetValue(ShowCrossHairProperty); }
22+
set { SetValue(ShowCrossHairProperty, value); }
23+
}
24+
25+
public CrossHairBehavior()
26+
{
27+
Drawable = this;
28+
}
29+
30+
public void Draw(ICanvas canvas, RectF dirtyRect)
31+
{
32+
if (ShowCrossHair && isTouchActive)
33+
{
34+
canvas.StrokeColor = Colors.Red;
35+
canvas.DrawLine(CrossHairX1, CrossHairX2);
36+
canvas.DrawLine(CrossHairY1, CrossHairY2);
37+
38+
canvas.FontColor = Colors.Black;
39+
canvas.Font = Font.DefaultBold;
40+
canvas.DrawString(CrossHairPointValue, CrossHairX1.X + 2, CrossHairY1.Y - 5, HorizontalAlignment.Left);
41+
canvas.DrawString(CrossHairXValue, CrossHairX1.X + 2, CrossHairX2.Y - 5, HorizontalAlignment.Left);
42+
canvas.DrawString(CrossHairYValue, CrossHairY1.X + 2, CrossHairY1.Y - 5, HorizontalAlignment.Left);
43+
}
44+
}
45+
46+
public void OnTouchMove(ChartBase chart, float pointX, float pointY)
47+
{
48+
isTouchActive = true;
49+
CalculateCrossHairPoints(chart, pointX, pointY);
50+
Invalidate();
51+
}
52+
53+
public void OnTouchUp(ChartBase chart, float pointX, float pointY)
54+
{
55+
isTouchActive = false;
56+
Invalidate();
57+
}
58+
59+
private void CalculateCrossHairPoints(ChartBase chart, float pointX, float pointY)
60+
{
61+
if (chart is SfCartesianChart cartesianChart)
62+
{
63+
bool isCrossHairArea = cartesianChart.SeriesBounds.Contains(pointX, pointY);
64+
65+
if (isCrossHairArea)
66+
{
67+
double x = cartesianChart.PointToValue(cartesianChart.XAxes[0], pointX, pointY);
68+
double y = cartesianChart.PointToValue(cartesianChart.YAxes[0], pointX, pointY);
69+
70+
float crossHairX = cartesianChart.XAxes[0].ValueToPoint(x);
71+
float crossHairY = cartesianChart.YAxes[0].ValueToPoint(y);
72+
73+
CrossHairXValue = cartesianChart.XAxes[0].PointToValue(crossHairX, crossHairY).ToString("0.00");
74+
CrossHairYValue = cartesianChart.YAxes[0].PointToValue(crossHairX, crossHairY).ToString("0.00");
75+
76+
CrossHairPointValue = CrossHairXValue + ", " + CrossHairYValue;
77+
78+
float crossHairLeft = cartesianChart.XAxes[0].ValueToPoint(cartesianChart.XAxes[0].VisibleMinimum);
79+
float crossHairRight = cartesianChart.XAxes[0].ValueToPoint(cartesianChart.XAxes[0].VisibleMaximum);
80+
float crossHairTop = cartesianChart.YAxes[0].ValueToPoint(cartesianChart.YAxes[0].VisibleMaximum);
81+
float crossHairBottom = cartesianChart.YAxes[0].ValueToPoint(cartesianChart.YAxes[0].VisibleMinimum);
82+
83+
CrossHairX1 = new PointF(crossHairX, crossHairTop);
84+
CrossHairX2 = new PointF(crossHairX, crossHairBottom);
85+
CrossHairY1 = new PointF(crossHairLeft, crossHairY);
86+
CrossHairY2 = new PointF(crossHairRight, crossHairY);
87+
}
88+
else
89+
{
90+
Reset();
91+
}
92+
}
93+
}
94+
95+
private void Reset()
96+
{
97+
CrossHairX1 = CrossHairX2 = CrossHairY1 = CrossHairY2 = new PointF(0, 0);
98+
}
99+
}
100+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Syncfusion.Maui.Toolkit.Charts;
2+
3+
namespace ChartCrossHair.UsingGraphicsView
4+
{
5+
public class CrossHairInteractiveBehavior : ChartInteractiveBehavior
6+
{
7+
protected override void OnTouchMove(ChartBase chart, float pointX, float pointY)
8+
{
9+
if (chart is SfCartesianChart cartesianChart && cartesianChart.PlotAreaBackgroundView is CrossHairBehavior crossHairBehavior)
10+
{
11+
crossHairBehavior.OnTouchMove(cartesianChart, pointX, pointY);
12+
}
13+
}
14+
15+
protected override void OnTouchUp(ChartBase chart, float pointX, float pointY)
16+
{
17+
if (chart is SfCartesianChart cartesianChart && cartesianChart.PlotAreaBackgroundView is CrossHairBehavior crossHairBehavior)
18+
{
19+
crossHairBehavior.OnTouchUp(cartesianChart, pointX, pointY);
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)