Skip to content

Commit 8daa34d

Browse files
update.
1 parent f06f68d commit 8daa34d

26 files changed

+1150
-858
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ChartGenerator.AIService
2+
{
3+
internal abstract class AICredentials
4+
{
5+
internal const string endpoint = "https://mobilemaui.openai.azure.com/";
6+
internal const string deploymentName = "gpt-4o";
7+
internal const string key = "6673b6975f334c79bd0db8a1cd70aa49";
8+
}
9+
}

ChartGeneratorAISample/ChartGenerator/AIService/ChartsAIService.cs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
1-
using Azure.AI.OpenAI;
2-
using Azure;
1+
using Azure;
2+
using Azure.AI.OpenAI;
3+
using ChartGenerator.AIService;
34
using Microsoft.Extensions.AI;
45

56
namespace ChartGenerator
67
{
7-
internal class ChartAIService
8+
internal class ChartAIService : AICredentials
89
{
910
#region Fields
1011

11-
/// <summary>
12-
/// The EndPoint
13-
/// </summary>
14-
internal const string endpoint = "https://YOUR_ACCOUNT.openai.azure.com/";
15-
16-
/// <summary>
17-
/// The Deployment name
18-
/// </summary>
19-
internal const string deploymentName = "deployment name";
20-
21-
/// <summary>
22-
/// The Image Deployment name
23-
/// </summary>
24-
internal const string imageDeploymentName = "IMAGE_MODEL_NAME";
25-
26-
/// <summary>
27-
/// The API key
28-
/// </summary>
29-
internal const string key = "API key";
30-
3112
/// <summary>
3213
/// The already credential validated field
3314
/// </summary>

ChartGeneratorAISample/ChartGenerator/ChartExtensions/CartesianChartExt.xaml renamed to ChartGeneratorAISample/ChartGenerator/ChartExtensions/CartesianCategory.xaml

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
44
xmlns:chart="clr-namespace:Syncfusion.Maui.Toolkit.Charts;assembly=Syncfusion.Maui.Toolkit"
55
xmlns:local="clr-namespace:ChartGenerator"
6-
x:Class="ChartGenerator.CartesianChartExt"
7-
Title="{Binding Title}"
8-
Source="{Binding Series}">
6+
x:Class="ChartGenerator.CartesianCategory" x:DataType="local:ChartConfig"
7+
Source="{Binding Series}" XAxis="{Binding XAxis}" YAxis="{Binding YAxis}" EnableSideBySideSeriesPlacement="{Binding SideBySidePlacement}">
98

9+
<chart:SfCartesianChart.Title>
10+
<Label Text="{Binding Title}" HorizontalOptions="Center" HorizontalTextAlignment="Center"/>
11+
</chart:SfCartesianChart.Title>
1012
<chart:SfCartesianChart.Resources>
1113
<ResourceDictionary>
1214
<ResourceDictionary.MergedDictionaries>
13-
<local:SeriesTemplates />
15+
<local:ChartResources />
1416
</ResourceDictionary.MergedDictionaries>
1517
</ResourceDictionary>
1618
</chart:SfCartesianChart.Resources>
@@ -19,20 +21,5 @@
1921
<chart:ChartLegend IsVisible="{Binding ShowLegend}" Placement="Bottom" ToggleSeriesVisibility="True"/>
2022
</chart:SfCartesianChart.Legend>
2123

22-
<chart:SfCartesianChart.XAxes>
23-
<chart:CategoryAxis>
24-
<chart:CategoryAxis.Title>
25-
<chart:ChartAxisTitle Text="{Binding XAxis.Title}"/>
26-
</chart:CategoryAxis.Title>
27-
</chart:CategoryAxis>
28-
</chart:SfCartesianChart.XAxes>
29-
30-
<chart:SfCartesianChart.YAxes>
31-
<chart:NumericalAxis Minimum="{Binding YAxis.Min}" Maximum="{Binding YAxis.Max}">
32-
<chart:NumericalAxis.Title>
33-
<chart:ChartAxisTitle Text="{Binding YAxis.Title}" />
34-
</chart:NumericalAxis.Title>
35-
</chart:NumericalAxis>
36-
</chart:SfCartesianChart.YAxes>
3724

3825
</chart:SfCartesianChart>
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
using Syncfusion.Maui.Toolkit.Charts;
2+
using System.Collections.ObjectModel;
3+
4+
namespace ChartGenerator;
5+
6+
public partial class CartesianCategory : SfCartesianChart
7+
{
8+
public CartesianCategory()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
// BindableProperty for the series source
14+
public static readonly BindableProperty SourceProperty = BindableProperty.Create(
15+
nameof(Source),
16+
typeof(ObservableCollection<SeriesConfig>),
17+
typeof(CartesianCategory),
18+
null,
19+
BindingMode.Default,
20+
propertyChanged: OnPropertyChanged);
21+
22+
public ObservableCollection<SeriesConfig> Source
23+
{
24+
get => (ObservableCollection<SeriesConfig>)GetValue(SourceProperty);
25+
set => SetValue(SourceProperty, value);
26+
}
27+
28+
public static readonly BindableProperty XAxisProperty = BindableProperty.Create(
29+
nameof(XAxis),
30+
typeof(ObservableCollection<AxisConfig>),
31+
typeof(CartesianCategory),
32+
null,
33+
BindingMode.Default,
34+
propertyChanged: XAxisChanged);
35+
36+
public ObservableCollection<SeriesConfig> XAxis
37+
{
38+
get => (ObservableCollection<SeriesConfig>)GetValue(XAxisProperty);
39+
set => SetValue(XAxisProperty, value);
40+
}
41+
42+
public static readonly BindableProperty YAxisProperty = BindableProperty.Create(
43+
nameof(YAxis),
44+
typeof(ObservableCollection<AxisConfig>),
45+
typeof(CartesianCategory),
46+
null,
47+
BindingMode.Default,
48+
propertyChanged: YAxisChanged);
49+
50+
public ObservableCollection<SeriesConfig> YAxis
51+
{
52+
get => (ObservableCollection<SeriesConfig>)GetValue(YAxisProperty);
53+
set => SetValue(YAxisProperty, value);
54+
}
55+
56+
private static void XAxisChanged(BindableObject bindable, object oldValue, object newValue)
57+
{
58+
if (bindable is SfCartesianChart cartesianChart && newValue is ObservableCollection<AxisConfig> xAxisConfigs)
59+
{
60+
// Clear current axes
61+
cartesianChart.XAxes.Clear();
62+
63+
// Add new axes based on the new configuration
64+
foreach (var axisConfig in xAxisConfigs)
65+
{
66+
ChartAxis axis = axisConfig.GetXAxis();
67+
68+
if (axis is CategoryAxis category)
69+
{
70+
category.LabelRotation = 45;
71+
}
72+
73+
axis.AxisLineStyle = SetLineStyle();
74+
axis.MajorGridLineStyle = SetLineStyle();
75+
axis.MajorTickStyle = SetTickStyle();
76+
axis.ShowMajorGridLines = false;
77+
cartesianChart.XAxes.Add(axis);
78+
}
79+
}
80+
}
81+
82+
private static void YAxisChanged(BindableObject bindable, object oldValue, object newValue)
83+
{
84+
if (bindable is SfCartesianChart cartesianChart && newValue is ObservableCollection<AxisConfig> yAxisConfigs)
85+
{
86+
cartesianChart.YAxes.Clear();
87+
88+
foreach (var axisConfig in yAxisConfigs)
89+
{
90+
var axis = axisConfig.GetYAxis();
91+
if (axis != null)
92+
{
93+
axis.AxisLineStyle = SetLineStyle();
94+
axis.MajorGridLineStyle = SetLineStyle();
95+
axis.MajorTickStyle = SetTickStyle();
96+
axis.ShowMajorGridLines = false;
97+
cartesianChart.YAxes.Add(axis);
98+
}
99+
}
100+
}
101+
}
102+
103+
private static ChartLineStyle SetLineStyle()
104+
{
105+
ChartLineStyle axisLineStyle = new ChartLineStyle()
106+
{
107+
Stroke = Colors.Transparent,
108+
StrokeWidth = 0,
109+
};
110+
111+
return axisLineStyle;
112+
}
113+
114+
private static ChartAxisTickStyle SetTickStyle()
115+
{
116+
ChartAxisTickStyle tickStyle = new ChartAxisTickStyle()
117+
{
118+
TickSize = 0
119+
};
120+
121+
return tickStyle;
122+
}
123+
124+
private static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
125+
{
126+
if (bindable is CartesianCategory chart)
127+
{
128+
chart.GenerateSeries(newValue as ObservableCollection<SeriesConfig>);
129+
}
130+
}
131+
132+
private void GenerateSeries(ObservableCollection<SeriesConfig> configs)
133+
{
134+
Series.Clear();
135+
if (configs != null)
136+
{
137+
foreach (var config in configs)
138+
{
139+
CreateSeriesFromTemplate(config);
140+
var paletteBrush = GetPaletteBrushes();
141+
142+
if (Series.Count == 1 && Series[0] is ColumnSeries series)
143+
{
144+
series.PaletteBrushes = paletteBrush;
145+
}
146+
else
147+
{
148+
this.PaletteBrushes = paletteBrush;
149+
}
150+
}
151+
}
152+
}
153+
154+
private Brush[] GetPaletteBrushes()
155+
{
156+
var random = new Random();
157+
switch (random.Next(1, 6))
158+
{
159+
case 1:
160+
return Resources["Pallet1"] as Brush[];
161+
case 2:
162+
return Resources["Pallet2"] as Brush[];
163+
case 3:
164+
return Resources["Pallet3"] as Brush[];
165+
case 5:
166+
return Resources["Pallet5"] as Brush[];
167+
default:
168+
return Resources["Pallet6"] as Brush[];
169+
}
170+
}
171+
172+
private void CreateSeriesFromTemplate(SeriesConfig seriesConfig)
173+
{
174+
var templateSelector = (SeriesTemplateSelector)Resources["seriesTemplateSelector"];
175+
var template = templateSelector.SelectTemplate(seriesConfig, null);
176+
177+
if (template != null)
178+
{
179+
ChartSeries series = (ChartSeries)template.CreateContent();
180+
181+
if (series != null)
182+
{
183+
series.BindingContext = seriesConfig;
184+
this.Series.Add(series);
185+
}
186+
}
187+
}
188+
}

ChartGeneratorAISample/ChartGenerator/ChartExtensions/CartesianChartExt.xaml.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)