Skip to content

Commit d351cef

Browse files
Merge pull request #1 from SyncfusionExamples/MAUI-3405-How-to-pass-a-JSON-array-to-MAU-Chart
MAUI-3405 How to pass a JSON array to .NET MAUI Chart
2 parents 859763b + e8e71a2 commit d351cef

38 files changed

+1128
-1
lines changed

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

MAUI_Chart_JSON_Data/App.xaml.cs

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

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

MAUI_Chart_JSON_Data/AppShell.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MAUI_Chart_JSON_Data;
2+
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.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);net6.0-tizen</TargetFrameworks> -->
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>MAUI_Chart_JSON_Data</RootNamespace>
10+
<UseMaui>true</UseMaui>
11+
<SingleProject>true</SingleProject>
12+
<ImplicitUsings>enable</ImplicitUsings>
13+
14+
<!-- Display name -->
15+
<ApplicationTitle>MAUI_Chart_JSON_Data</ApplicationTitle>
16+
17+
<!-- App Identifier -->
18+
<ApplicationId>com.companyname.maui_chart_json_data</ApplicationId>
19+
<ApplicationIdGuid>737cff82-cea7-4532-9183-172a96d3cbaa</ApplicationIdGuid>
20+
21+
<!-- Versions -->
22+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
23+
<ApplicationVersion>1</ApplicationVersion>
24+
25+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
26+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
27+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
28+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
29+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
30+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
31+
</PropertyGroup>
32+
33+
<ItemGroup>
34+
<!-- App Icon -->
35+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
36+
37+
<!-- Splash Screen -->
38+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
39+
40+
<!-- Images -->
41+
<MauiImage Include="Resources\Images\*" />
42+
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
43+
44+
<!-- Custom Fonts -->
45+
<MauiFont Include="Resources\Fonts\*" />
46+
47+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
48+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
49+
</ItemGroup>
50+
51+
<ItemGroup>
52+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
53+
<PackageReference Include="Syncfusion.Maui.Charts" Version="*" />
54+
</ItemGroup>
55+
56+
</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.0.31611.283
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MAUI_Chart_JSON_Data", "MAUI_Chart_JSON_Data.csproj", "{880BB72B-24BE-403B-B01D-36E2A7F9A088}"
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+
{880BB72B-24BE-403B-B01D-36E2A7F9A088}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{880BB72B-24BE-403B-B01D-36E2A7F9A088}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{880BB72B-24BE-403B-B01D-36E2A7F9A088}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{880BB72B-24BE-403B-B01D-36E2A7F9A088}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{880BB72B-24BE-403B-B01D-36E2A7F9A088}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{880BB72B-24BE-403B-B01D-36E2A7F9A088}.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 = {61F7FB11-1E47-470C-91E2-47F8143E1572}
26+
EndGlobalSection
27+
EndGlobal

MAUI_Chart_JSON_Data/MainPage.xaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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="MAUI_Chart_JSON_Data.MainPage">
6+
<Grid>
7+
<chart:SfCartesianChart x:Name="chart">
8+
<chart:SfCartesianChart.XAxes>
9+
<chart:CategoryAxis>
10+
<chart:CategoryAxis.Title>
11+
<chart:ChartAxisTitle Text="Country"/>
12+
</chart:CategoryAxis.Title>
13+
</chart:CategoryAxis>
14+
</chart:SfCartesianChart.XAxes>
15+
16+
<chart:SfCartesianChart.YAxes>
17+
<chart:NumericalAxis>
18+
<chart:NumericalAxis.Title>
19+
<chart:ChartAxisTitle Text="Medals"/>
20+
</chart:NumericalAxis.Title>
21+
</chart:NumericalAxis>
22+
</chart:SfCartesianChart.YAxes>
23+
24+
<chart:ColumnSeries ItemsSource="{Binding Medal}"
25+
XBindingPath="Country"
26+
YBindingPath="Count">
27+
</chart:ColumnSeries>
28+
</chart:SfCartesianChart>
29+
</Grid>
30+
</ContentPage>

MAUI_Chart_JSON_Data/MainPage.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Newtonsoft.Json;
2+
3+
namespace MAUI_Chart_JSON_Data;
4+
5+
public partial class MainPage : ContentPage
6+
{
7+
8+
public MainPage()
9+
{
10+
InitializeComponent();
11+
string jsonData = @"{'Medal':[{'Country':'USA','Count':46},{'Country':'China','Count':36},{'Country':'Japan','Count':63},{'Country':'Australia','Count':54},{'Country':'France','Count':50}]}";
12+
13+
var jsonDataCollection = JsonConvert.DeserializeObject<ViewModel>(jsonData);
14+
chart.BindingContext = jsonDataCollection;
15+
}
16+
}
17+

MAUI_Chart_JSON_Data/MauiProgram.cs

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

MAUI_Chart_JSON_Data/Model/Medal.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+

2+
namespace MAUI_Chart_JSON_Data
3+
{
4+
public class Medal
5+
{
6+
public string Country { get; set; }
7+
public int Count { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)