|
| 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 | +} |
0 commit comments