Skip to content

Commit

Permalink
Merge pull request #1 from SyncfusionExamples/Auto_Generate_Charts
Browse files Browse the repository at this point in the history
Added  the sample for  Auto generate charts
  • Loading branch information
Saravanan-Madhesh authored Dec 30, 2024
2 parents aa93141 + 53f3d41 commit b5291e3
Show file tree
Hide file tree
Showing 59 changed files with 10,379 additions and 2 deletions.
22 changes: 22 additions & 0 deletions ChartGenerator/ChartGenerator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35506.116 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChartGenerator", "ChartGenerator\ChartGenerator.csproj", "{EC5A9A69-964B-4546-B344-41C7A240CD6D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EC5A9A69-964B-4546-B344-41C7A240CD6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC5A9A69-964B-4546-B344-41C7A240CD6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC5A9A69-964B-4546-B344-41C7A240CD6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC5A9A69-964B-4546-B344-41C7A240CD6D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions ChartGenerator/ChartGenerator/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ChartGenerator"
x:Class="ChartGenerator.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
15 changes: 15 additions & 0 deletions ChartGenerator/ChartGenerator/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace ChartGenerator
{
public partial class App : Application
{
public App()
{
InitializeComponent();
}

protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(new NavigationPage(new MainPage()));
}
}
}
14 changes: 14 additions & 0 deletions ChartGenerator/ChartGenerator/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="ChartGenerator.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ChartGenerator"
Shell.FlyoutBehavior="Flyout"
Title="ChartGenerator">

<ShellContent
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions ChartGenerator/ChartGenerator/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ChartGenerator
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<chart:SfCartesianChart xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:chart="clr-namespace:Syncfusion.Maui.Toolkit.Charts;assembly=Syncfusion.Maui.Toolkit"
xmlns:local="clr-namespace:ChartGenerator"
x:Class="ChartGenerator.CartesianChartExt"
Title="{Binding Title}"
Source="{Binding Series}">

<chart:SfCartesianChart.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:SeriesTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</chart:SfCartesianChart.Resources>

<chart:SfCartesianChart.Legend>
<chart:ChartLegend Placement="Bottom" ToggleSeriesVisibility="True"/>
</chart:SfCartesianChart.Legend>

<chart:SfCartesianChart.XAxes>
<chart:CategoryAxis>
<chart:CategoryAxis.Title>
<chart:ChartAxisTitle Text="{Binding XAxis.Title}"/>
</chart:CategoryAxis.Title>
</chart:CategoryAxis>
</chart:SfCartesianChart.XAxes>

<chart:SfCartesianChart.YAxes>
<chart:NumericalAxis Minimum="{Binding YAxis.Min}" Maximum="{Binding YAxis.Max}">
<chart:NumericalAxis.Title>
<chart:ChartAxisTitle Text="{Binding YAxis.Title}" />
</chart:NumericalAxis.Title>
</chart:NumericalAxis>
</chart:SfCartesianChart.YAxes>

</chart:SfCartesianChart>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using Syncfusion.Maui.Toolkit.Charts;
using System.Collections.ObjectModel;

namespace ChartGenerator;

public partial class CartesianChartExt : SfCartesianChart
{
public CartesianChartExt()
{
InitializeComponent();
}

public static readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(Source), typeof(ObservableCollection<SeriesConfig>), typeof(CartesianChartExt), null, BindingMode.Default, null, OnPropertyChanged);
public ObservableCollection<SeriesConfig> Source
{
get => (ObservableCollection<SeriesConfig>)GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}

private static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (Equals(oldValue, newValue))
{
return;
}

if (bindable is CartesianChartExt chart)
{
chart.GenerateSeries(newValue as ObservableCollection<SeriesConfig>);
}
}

private void GenerateSeries(ObservableCollection<SeriesConfig> configs)
{
if (configs != null)
{
this.Series.Clear();
foreach (var config in configs)
{
//CreateSeries(config);
CreateSeriesFromTemplate(config);
}
}
}

private void CreateSeriesFromTemplate(SeriesConfig seriesConfig)
{
var templateSelector = (SeriesTemplateSelector)Resources["seriesTemplateSelector"];
var template = templateSelector.SelectTemplate(seriesConfig, null);

if (template != null)
{
ChartSeries series = (ChartSeries)template.CreateContent();

if (series != null)
{
series.BindingContext = seriesConfig;
this.Series.Add(series);
}
}
}

private void CreateSeries(SeriesConfig config)
{
ChartSeries series = null;

switch (config.Type)
{
case ChartEnums.SeriesType.Line:
series = new LineSeries
{
ItemsSource = config.DataSource,
XBindingPath = config.XPath,
YBindingPath = "yvalue",
EnableTooltip = config.Tooltip
};
break;

case ChartEnums.SeriesType.Area:
series = new AreaSeries
{
ItemsSource = config.DataSource,
XBindingPath = config.XPath,
YBindingPath = "yvalue",
EnableTooltip = config.Tooltip
};
break;

case ChartEnums.SeriesType.Spline:
series = new SplineSeries
{
ItemsSource = config.DataSource,
XBindingPath = config.XPath,
YBindingPath = "yvalue",
EnableTooltip = config.Tooltip
};
break;

case ChartEnums.SeriesType.Column:
series = new ColumnSeries
{
ItemsSource = config.DataSource,
XBindingPath = config.XPath,
YBindingPath = "yvalue",
EnableTooltip = config.Tooltip
};
break;
}

if (series != null)
{
this.Series.Add(series);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<chart:SfCircularChart xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:chart="clr-namespace:Syncfusion.Maui.Toolkit.Charts;assembly=Syncfusion.Maui.Toolkit"
xmlns:local="clr-namespace:ChartGenerator"
x:Class="ChartGenerator.CircularChartExt"
Title="{Binding Title}"
Source="{Binding Series}" >

<chart:SfCircularChart.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:SeriesTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</chart:SfCircularChart.Resources>

<chart:SfCircularChart.Legend>
<chart:ChartLegend Placement="Right" ToggleSeriesVisibility="True"/>
</chart:SfCircularChart.Legend>

</chart:SfCircularChart>
100 changes: 100 additions & 0 deletions ChartGenerator/ChartGenerator/ChartExtensions/CircularChartExt.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Syncfusion.Maui.Toolkit.Charts;
using System.Collections.ObjectModel;

namespace ChartGenerator;

public partial class CircularChartExt : SfCircularChart
{
public CircularChartExt()
{
InitializeComponent();
}

public static readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(Source), typeof(ObservableCollection<SeriesConfig>), typeof(CircularChartExt), null, BindingMode.Default, null, OnPropertyChanged);
public ObservableCollection<SeriesConfig> Source
{
get => (ObservableCollection<SeriesConfig>)GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}

private static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is CircularChartExt chart)
{
chart.GenerateSeries(newValue as ObservableCollection<SeriesConfig>);
}
}

private void GenerateSeries(ObservableCollection<SeriesConfig> configs)
{
if (configs != null)
{
this.Series.Clear();
foreach (var config in configs)
{
//CreateSeries(config);
CreateSeriesFromTemplate(config);
}
}
}

private void CreateSeriesFromTemplate(SeriesConfig config)
{
var templateSelector = (SeriesTemplateSelector)Resources["seriesTemplateSelector"];
var template = templateSelector.SelectTemplate(config, null);

if (template != null)
{
ChartSeries series = (ChartSeries)template.CreateContent();

if (series != null)
{
series.BindingContext = config;
this.Series.Add(series);
}
}
}

private void CreateSeries(SeriesConfig config)
{
ChartSeries series = null;

switch (config.Type)
{
case ChartEnums.SeriesType.Pie:
series = new PieSeries
{
ItemsSource = config.DataSource,
XBindingPath = config.XPath,
YBindingPath = "yvalue",
EnableTooltip = config.Tooltip
};
break;

case ChartEnums.SeriesType.Doughnut:
series = new DoughnutSeries
{
ItemsSource = config.DataSource,
XBindingPath = config.XPath,
YBindingPath = "yvalue",
EnableTooltip = config.Tooltip
};
break;

case ChartEnums.SeriesType.RadialBar:
series = new RadialBarSeries
{
ItemsSource = config.DataSource,
XBindingPath = config.XPath,
YBindingPath = "yvalue",
EnableTooltip = config.Tooltip
};
break;
}

if (series != null)
{
this.Series.Add(series);
}
}
}
Loading

0 comments on commit b5291e3

Please sign in to comment.