Skip to content

Commit a81c297

Browse files
Merge pull request #1 from SyncfusionExamples/876723-How-to-highlight-selected-data-points-by-using-GetDataPoints-method-in-.NET-MAUI-Cartesian-Charts
876723-How to highlight selected data points by using GetDataPoints method in .NET MAUI Cartesian Charts sample.
2 parents 337703a + 4591fb8 commit a81c297

39 files changed

+1310
-0
lines changed

GetDataPointsSample/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:GetDataPointsSample"
5+
x:Class="GetDataPointsSample.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>

GetDataPointsSample/App.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace GetDataPointsSample
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}
12+
}

GetDataPointsSample/AppShell.xaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="GetDataPointsSample.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:GetDataPointsSample"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="GetDataPointsSample">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace GetDataPointsSample
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.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);net8.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>GetDataPointsSample</RootNamespace>
18+
<UseMaui>true</UseMaui>
19+
<SingleProject>true</SingleProject>
20+
<ImplicitUsings>enable</ImplicitUsings>
21+
<Nullable>enable</Nullable>
22+
23+
<!-- Display name -->
24+
<ApplicationTitle>GetDataPointsSample</ApplicationTitle>
25+
26+
<!-- App Identifier -->
27+
<ApplicationId>com.companyname.getdatapointssample</ApplicationId>
28+
29+
<!-- Versions -->
30+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
31+
<ApplicationVersion>1</ApplicationVersion>
32+
33+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
34+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
35+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
36+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
37+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
38+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
39+
</PropertyGroup>
40+
41+
<ItemGroup>
42+
<!-- App Icon -->
43+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
44+
45+
<!-- Splash Screen -->
46+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
47+
48+
<!-- Images -->
49+
<MauiImage Include="Resources\Images\*" />
50+
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
51+
52+
<!-- Custom Fonts -->
53+
<MauiFont Include="Resources\Fonts\*" />
54+
55+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
56+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
57+
</ItemGroup>
58+
59+
<ItemGroup>
60+
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
61+
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
62+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
63+
<PackageReference Include="Syncfusion.Maui.Charts" Version="*" />
64+
</ItemGroup>
65+
66+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.34916.146
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GetDataPointsSample", "GetDataPointsSample.csproj", "{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
Release-Xml|Any CPU = Release-Xml|Any CPU
13+
EndGlobalSection
14+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
15+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
16+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
17+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
18+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Release|Any CPU.Deploy.0 = Release|Any CPU
21+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Release-Xml|Any CPU.ActiveCfg = Release|Any CPU
22+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Release-Xml|Any CPU.Build.0 = Release|Any CPU
23+
{E8A64D9C-3538-4373-8A5A-6774CCE99BBF}.Release-Xml|Any CPU.Deploy.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {3A2345C4-16E1-43EE-9FD0-EADBABFF4736}
30+
EndGlobalSection
31+
EndGlobal

GetDataPointsSample/MainPage.xaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
5+
x:Class="GetDataPointsSample.MainPage"
6+
xmlns:model="clr-namespace:GetDataPointsSample">
7+
8+
<ContentPage.BindingContext>
9+
<model:ScatterSeriesViewModel x:Name="viewModel"/>
10+
</ContentPage.BindingContext>
11+
12+
<chart:SfCartesianChart HorizontalOptions="Fill" VerticalOptions="Fill" x:Name="Chart">
13+
14+
<chart:SfCartesianChart.PlotAreaBackgroundView>
15+
<GraphicsView Drawable="{x:Reference InteractionExt}" x:Name="graphicsView" InputTransparent="True" ZIndex="1"/>
16+
</chart:SfCartesianChart.PlotAreaBackgroundView>
17+
18+
<chart:SfCartesianChart.InteractiveBehavior>
19+
<model:ChartInteractionExt x:Name="InteractionExt" Graphics="{x:Reference graphicsView}"/>
20+
</chart:SfCartesianChart.InteractiveBehavior>
21+
22+
<chart:SfCartesianChart.XAxes>
23+
<chart:NumericalAxis Minimum="100" Maximum="220" Interval="20"/>
24+
</chart:SfCartesianChart.XAxes>
25+
<chart:SfCartesianChart.YAxes>
26+
<chart:NumericalAxis Minimum="50" Maximum="80" Interval="5"/>
27+
</chart:SfCartesianChart.YAxes>
28+
<chart:SfCartesianChart.Series>
29+
<chart:ScatterSeries ItemsSource="{Binding Data}" XBindingPath="XValue" YBindingPath="YValue" PointWidth="8" Opacity="0.8" Fill="#FE7A36" PointHeight="8" >
30+
<chart:ScatterSeries.SelectionBehavior>
31+
<chart:DataPointSelectionBehavior Type="Multiple" SelectionBrush="#3652AD"/>
32+
</chart:ScatterSeries.SelectionBehavior>
33+
</chart:ScatterSeries>
34+
35+
</chart:SfCartesianChart.Series>
36+
</chart:SfCartesianChart>
37+
38+
</ContentPage>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Syncfusion.Maui.Charts;
2+
using System.ComponentModel;
3+
4+
namespace GetDataPointsSample
5+
{
6+
public partial class MainPage : ContentPage
7+
{
8+
public MainPage()
9+
{
10+
InitializeComponent();
11+
}
12+
}
13+
14+
public class ChartInteractionExt : ChartInteractiveBehavior, IDrawable, INotifyPropertyChanged
15+
{
16+
float startX;
17+
float startY;
18+
float endX;
19+
float endY;
20+
21+
bool showRect = false;
22+
public GraphicsView Graphics { get; set; }
23+
public void Draw(ICanvas canvas, RectF dirtyRect)
24+
{
25+
canvas.StrokeColor = Colors.Black;
26+
canvas.StrokeSize = 2;
27+
canvas.DrawRectangle(startX, startY, endX, endY);
28+
}
29+
30+
protected override void OnTouchDown(ChartBase chart, float pointX, float pointY)
31+
{
32+
endY = 0; endX = 0;
33+
showRect = true;
34+
var seriesBounds = chart.SeriesBounds;
35+
startX = (float)(pointX - seriesBounds.Left);
36+
startY = (float)(pointY - seriesBounds.Top);
37+
}
38+
39+
40+
protected override void OnTouchUp(ChartBase chart, float pointX, float pointY)
41+
{
42+
if (chart is SfCartesianChart cartesianChart)
43+
{
44+
var viewModel = chart.BindingContext as ScatterSeriesViewModel;
45+
46+
var rect = new Rect(startX, startY, endX, endY);
47+
48+
var selectedIndexes = new List<int>();
49+
50+
foreach (var series in cartesianChart.Series)
51+
{
52+
if (series is ScatterSeries scatterSeries)
53+
{
54+
var dataPoints = scatterSeries.GetDataPoints(rect);
55+
56+
if (dataPoints != null && viewModel != null)
57+
{
58+
for (int i = 0; i < viewModel.Data.Count; i++)
59+
{
60+
if (dataPoints.Contains(viewModel.Data[i]))
61+
selectedIndexes.Add(i);
62+
}
63+
64+
scatterSeries.SelectionBehavior.SelectedIndexes = selectedIndexes;
65+
}
66+
}
67+
}
68+
}
69+
70+
showRect = false;
71+
Graphics.Invalidate();
72+
}
73+
74+
protected override void OnTouchMove(ChartBase chart, float pointX, float pointY)
75+
{
76+
var seriesBounds = chart.SeriesBounds;
77+
78+
if (showRect)
79+
{
80+
float width = (float)(pointX - startX - seriesBounds.Left);
81+
float height = (float)(pointY - startY - seriesBounds.Top);
82+
endX = width;
83+
endY = height;
84+
}
85+
Graphics.Invalidate();
86+
}
87+
}
88+
89+
}

GetDataPointsSample/MauiProgram.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
using Syncfusion.Maui.Core.Hosting;
3+
4+
namespace GetDataPointsSample
5+
{
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}
27+
}

GetDataPointsSample/Model/Model.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace GetDataPointsSample
8+
{
9+
public class ChartDataModel
10+
{
11+
public double XValue { get; set; }
12+
13+
public double YValue { get; set; }
14+
15+
public ChartDataModel() { }
16+
17+
public ChartDataModel(double xValue, double yValue)
18+
{
19+
XValue = xValue;
20+
YValue = yValue;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)