Skip to content

Commit 06b7c72

Browse files
Merge pull request #2 from SyncfusionExamples/882409-How-to-add-a-custom-view
How to add a custom view to the data label of a .NET MAUI chart
2 parents b69e7d5 + 3d22db7 commit 06b7c72

Some content is hidden

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

41 files changed

+1231
-2
lines changed

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

DataLabelTemplate/App.xaml.cs

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

DataLabelTemplate/AppShell.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+
<Shell
3+
x:Class="DataLabelTemplate.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:DataLabelTemplate"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="DataLabelTemplate">
9+
10+
<ShellContent
11+
ContentTemplate="{DataTemplate local:MainPage}"
12+
Route="MainPage" />
13+
14+
</Shell>

DataLabelTemplate/AppShell.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace DataLabelTemplate
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}

DataLabelTemplate/Convertor.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace DataLabelTemplate
9+
{
10+
public class ValueToSummaryValueConverter : IValueConverter
11+
{
12+
private double sumOfValues = 0;
13+
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
14+
{
15+
if (value == null || !(value is double))
16+
return null;
17+
18+
double numericValue = (double)value;
19+
sumOfValues += numericValue;
20+
if (numericValue == 0)
21+
return sumOfValues;
22+
else
23+
return numericValue;
24+
}
25+
26+
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
27+
{
28+
throw new NotImplementedException();
29+
}
30+
}
31+
32+
public class ValueToColorConverter : IValueConverter
33+
{
34+
35+
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
36+
{
37+
if (value == null || !(value is double))
38+
return Colors.Transparent;
39+
40+
double numericValue = (double)value;
41+
if (numericValue == 0)
42+
return Color.FromHex("#3333cc");
43+
else
44+
return numericValue > 0 ? Color.FromHex("#3bab46") : Color.FromHex("#df3320");
45+
}
46+
47+
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
48+
{
49+
throw new NotImplementedException();
50+
}
51+
}
52+
53+
public class ValueToImageConvertor : IValueConverter
54+
{
55+
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
56+
{
57+
if (value == null || !(value is double))
58+
return null;
59+
60+
double numericValue = (double)value;
61+
if (numericValue == 0)
62+
return null;
63+
else
64+
return numericValue > 0 ? "up.png" : "down.png";
65+
}
66+
67+
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
68+
{
69+
throw new NotImplementedException();
70+
}
71+
}
72+
}
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>DataLabelTemplate</RootNamespace>
18+
<UseMaui>true</UseMaui>
19+
<SingleProject>true</SingleProject>
20+
<ImplicitUsings>enable</ImplicitUsings>
21+
<Nullable>enable</Nullable>
22+
23+
<!-- Display name -->
24+
<ApplicationTitle>DataLabelTemplate</ApplicationTitle>
25+
26+
<!-- App Identifier -->
27+
<ApplicationId>com.companyname.datalabeltemplate</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.Toolkit" 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+
<?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>net8.0-windows10.0.19041.0</ActiveDebugFramework>
6+
<ActiveDebugProfile>Windows Machine</ActiveDebugProfile>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<None Update="App.xaml">
10+
<SubType>Designer</SubType>
11+
</None>
12+
<None Update="AppShell.xaml">
13+
<SubType>Designer</SubType>
14+
</None>
15+
<None Update="MainPage.xaml">
16+
<SubType>Designer</SubType>
17+
</None>
18+
<None Update="Platforms\Windows\App.xaml">
19+
<SubType>Designer</SubType>
20+
</None>
21+
<None Update="Platforms\Windows\Package.appxmanifest">
22+
<SubType>Designer</SubType>
23+
</None>
24+
<None Update="Resources\Styles\Colors.xaml">
25+
<SubType>Designer</SubType>
26+
</None>
27+
<None Update="Resources\Styles\Styles.xaml">
28+
<SubType>Designer</SubType>
29+
</None>
30+
</ItemGroup>
31+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataLabelTemplate", "DataLabelTemplate.csproj", "{13A2C44B-3AC6-45CE-9F0B-1D17070557D0}"
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+
{13A2C44B-3AC6-45CE-9F0B-1D17070557D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{13A2C44B-3AC6-45CE-9F0B-1D17070557D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{13A2C44B-3AC6-45CE-9F0B-1D17070557D0}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{13A2C44B-3AC6-45CE-9F0B-1D17070557D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{13A2C44B-3AC6-45CE-9F0B-1D17070557D0}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{13A2C44B-3AC6-45CE-9F0B-1D17070557D0}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {9D8766D1-6381-4336-BAA0-023A3227E2B8}
26+
EndGlobalSection
27+
EndGlobal

DataLabelTemplate/MainPage.xaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
x:Class="DataLabelTemplate.MainPage"
5+
xmlns:chart="clr-namespace:Syncfusion.Maui.Toolkit.Charts;assembly=Syncfusion.Maui.Toolkit"
6+
xmlns:local="clr-namespace:DataLabelTemplate">
7+
8+
<ContentPage.BindingContext>
9+
<local:ChartViewModel x:Name="ViewModel"></local:ChartViewModel>
10+
</ContentPage.BindingContext>
11+
12+
<ContentPage.Resources>
13+
<ResourceDictionary>
14+
<local:ValueToSummaryValueConverter x:Key="ValueToValueConverter"/>
15+
<local:ValueToColorConverter x:Key="ValueToColorConverter"/>
16+
<local:ValueToImageConvertor x:Key="ValueToImageConverter"></local:ValueToImageConvertor>
17+
<DataTemplate x:Key="dataLabel">
18+
<StackLayout Orientation="Horizontal" Spacing="3">
19+
<Label FontSize="14" FontAttributes="Bold">
20+
<Label.Text>
21+
<Binding Path="Item.Value" Converter="{StaticResource ValueToValueConverter}" StringFormat="{}$.{0}"/>
22+
</Label.Text>
23+
<Label.TextColor>
24+
<Binding Path="Item.Value" Converter="{StaticResource ValueToColorConverter}"/>
25+
</Label.TextColor>
26+
</Label>
27+
<Image Source="{Binding Item.Value,Converter={StaticResource ValueToImageConverter}}" HeightRequest="15" WidthRequest="15"></Image>
28+
</StackLayout>
29+
</DataTemplate>
30+
</ResourceDictionary>
31+
</ContentPage.Resources>
32+
33+
34+
<chart:SfCartesianChart >
35+
<chart:SfCartesianChart.XAxes>
36+
<chart:CategoryAxis></chart:CategoryAxis>
37+
</chart:SfCartesianChart.XAxes>
38+
39+
<chart:SfCartesianChart.YAxes>
40+
<chart:NumericalAxis Minimum="0" Maximum="20000" Interval="4000"></chart:NumericalAxis>
41+
</chart:SfCartesianChart.YAxes>
42+
43+
44+
<chart:WaterfallSeries ItemsSource="{Binding Data}"
45+
XBindingPath="Department"
46+
YBindingPath="Value"
47+
Fill="#3bab46"
48+
NegativePointsBrush="#df3320"
49+
ShowDataLabels="True"
50+
LabelTemplate="{StaticResource dataLabel}"
51+
SummaryBindingPath="IsSummary"
52+
SummaryPointsBrush="#3333cc">
53+
54+
55+
<chart:WaterfallSeries.DataLabelSettings>
56+
<chart:CartesianDataLabelSettings LabelPlacement="Outer" UseSeriesPalette="False"></chart:CartesianDataLabelSettings>
57+
</chart:WaterfallSeries.DataLabelSettings>
58+
</chart:WaterfallSeries>
59+
</chart:SfCartesianChart>
60+
61+
</ContentPage>

DataLabelTemplate/MainPage.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace DataLabelTemplate
2+
{
3+
public partial class MainPage : ContentPage
4+
{
5+
6+
7+
public MainPage()
8+
{
9+
InitializeComponent();
10+
}
11+
12+
13+
}
14+
15+
}

0 commit comments

Comments
 (0)