|
2 | 2 |
|
3 | 3 | public class BlazorBootstrapChart : BlazorBootstrapComponentBase, IDisposable, IAsyncDisposable
|
4 | 4 | {
|
5 |
| - #region Fields and Constants |
| 5 | + #region Fields and Constants |
6 | 6 |
|
7 |
| - internal ChartType chartType; |
| 7 | + internal ChartType chartType; |
8 | 8 |
|
9 |
| - #endregion |
| 9 | + #endregion |
10 | 10 |
|
11 |
| - #region Methods |
| 11 | + #region Methods |
12 | 12 |
|
13 |
| - //public async Task Stop() { } |
| 13 | + //public async Task Stop() { } |
14 | 14 |
|
15 |
| - //public async Task ToBase64Image() { } |
| 15 | + //public async Task ToBase64Image() { } |
16 | 16 |
|
17 |
| - //public async Task ToBase64Image(string type, double quality) { } |
| 17 | + //public async Task ToBase64Image(string type, double quality) { } |
18 | 18 |
|
19 |
| - public virtual async Task<ChartData> AddDataAsync(ChartData chartData, string dataLabel, IChartDatasetData data) => await Task.FromResult(chartData); |
| 19 | + public virtual async Task<ChartData> AddDataAsync( ChartData chartData, string dataLabel, IChartDatasetData data ) => await Task.FromResult( chartData ); |
20 | 20 |
|
21 |
| - public virtual async Task<ChartData> AddDataAsync(ChartData chartData, string dataLabel, IReadOnlyCollection<IChartDatasetData> data) => await Task.FromResult(chartData); |
| 21 | + public virtual async Task<ChartData> AddDataAsync( ChartData chartData, string dataLabel, IReadOnlyCollection<IChartDatasetData> data ) => await Task.FromResult( chartData ); |
22 | 22 |
|
23 |
| - public virtual async Task<ChartData> AddDatasetAsync(ChartData chartData, IChartDataset chartDataset, IChartOptions chartOptions) => await Task.FromResult(chartData); |
| 23 | + public virtual async Task<ChartData> AddDatasetAsync( ChartData chartData, IChartDataset chartDataset, IChartOptions chartOptions ) => await Task.FromResult( chartData ); |
24 | 24 |
|
25 |
| - /// <inheritdoc /> |
26 |
| - public new virtual void Dispose() => Dispose(true); |
| 25 | + /// <inheritdoc /> |
| 26 | + public new virtual void Dispose() => Dispose( true ); |
27 | 27 |
|
28 |
| - /// <inheritdoc /> |
29 |
| - public new virtual async ValueTask DisposeAsync() |
30 |
| - { |
31 |
| - await DisposeAsyncCore(true); |
32 |
| - Dispose(false); |
33 |
| - } |
| 28 | + /// <inheritdoc /> |
| 29 | + public new virtual async ValueTask DisposeAsync() |
| 30 | + { |
| 31 | + await DisposeAsyncCore( true ); |
| 32 | + Dispose( false ); |
| 33 | + } |
34 | 34 |
|
35 |
| - //public async Task Clear() { } |
| 35 | + //public async Task Clear() { } |
36 | 36 |
|
37 |
| - /// <summary> |
38 |
| - /// Initialize Bar Chart. |
39 |
| - /// </summary> |
40 |
| - /// <param name="chartData"></param> |
41 |
| - /// <param name="chartOptions"></param> |
42 |
| - /// <param name="plugins"></param> |
43 |
| - public virtual async Task InitializeAsync(ChartData chartData, IChartOptions chartOptions, string[]? plugins = null) |
| 37 | + /// <summary> |
| 38 | + /// Initialize Bar Chart. |
| 39 | + /// </summary> |
| 40 | + /// <param name="chartData"></param> |
| 41 | + /// <param name="chartOptions"></param> |
| 42 | + /// <param name="plugins"></param> |
| 43 | + public virtual async Task InitializeAsync( ChartData chartData, IChartOptions chartOptions, string[]? plugins = null ) |
| 44 | + { |
| 45 | + if( chartData is not null && chartData.Datasets is not null && chartData.Datasets.Any() ) |
44 | 46 | {
|
45 |
| - if (chartData is not null && chartData.Datasets is not null && chartData.Datasets.Any()) |
46 |
| - { |
47 |
| - var _data = GetChartDataObject(chartData); |
48 |
| - |
49 |
| - if (chartType == ChartType.Bar) |
50 |
| - await JSRuntime.InvokeVoidAsync("window.blazorChart.bar.initialize", Id, GetChartType(), _data, (BarChartOptions)chartOptions, plugins); |
51 |
| - else if (chartType == ChartType.Line) |
52 |
| - await JSRuntime.InvokeVoidAsync("window.blazorChart.line.initialize", Id, GetChartType(), _data, (LineChartOptions)chartOptions, plugins); |
53 |
| - else |
54 |
| - await JSRuntime.InvokeVoidAsync("window.blazorChart.initialize", Id, GetChartType(), _data, chartOptions, plugins); |
55 |
| - } |
| 47 | + var _data = GetChartDataObject( chartData ); |
| 48 | + |
| 49 | + if( chartType == ChartType.Bar ) |
| 50 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.bar.initialize", Id, GetChartType(), _data, (BarChartOptions)chartOptions, plugins ); |
| 51 | + else if( chartType == ChartType.Line ) |
| 52 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.line.initialize", Id, GetChartType(), _data, (LineChartOptions)chartOptions, plugins ); |
| 53 | + else |
| 54 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.initialize", Id, GetChartType(), _data, chartOptions, plugins ); |
56 | 55 | }
|
57 |
| - |
58 |
| - //public async Task Render() { } |
59 |
| - |
60 |
| - //public async Task Reset() { } |
61 |
| - |
62 |
| - /// <summary> |
63 |
| - /// Resize the chart. |
64 |
| - /// </summary> |
65 |
| - /// <param name="width"></param> |
66 |
| - /// <param name="height"></param> |
67 |
| - /// <param name="widthUnit"></param> |
68 |
| - /// <param name="heightUnit"></param> |
69 |
| - public async Task ResizeAsync(int width, int height, Unit widthUnit = Unit.Px, Unit heightUnit = Unit.Px) |
| 56 | + } |
| 57 | + |
| 58 | + //public async Task Render() { } |
| 59 | + |
| 60 | + //public async Task Reset() { } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Resize the chart. |
| 64 | + /// </summary> |
| 65 | + /// <param name="width"></param> |
| 66 | + /// <param name="height"></param> |
| 67 | + /// <param name="widthUnit"></param> |
| 68 | + /// <param name="heightUnit"></param> |
| 69 | + public async Task ResizeAsync( int width, int height, Unit widthUnit = Unit.Px, Unit heightUnit = Unit.Px ) |
| 70 | + { |
| 71 | + var widthWithUnit = $"width:{width.ToString( CultureInfo.InvariantCulture )}{widthUnit.ToCssString()}"; |
| 72 | + var heightWithUnit = $"height:{height.ToString( CultureInfo.InvariantCulture )}{heightUnit.ToCssString()}"; |
| 73 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.resize", Id, widthWithUnit, heightWithUnit ); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Update chart. |
| 78 | + /// </summary> |
| 79 | + /// <param name="chartData"></param> |
| 80 | + /// <param name="chartOptions"></param> |
| 81 | + public virtual async Task UpdateAsync( ChartData chartData, IChartOptions chartOptions ) |
| 82 | + { |
| 83 | + if( chartData is not null && chartData.Datasets is not null && chartData.Datasets.Any() ) |
70 | 84 | {
|
71 |
| - var widthWithUnit = $"width:{width.ToString(CultureInfo.InvariantCulture)}{widthUnit.ToCssString()}"; |
72 |
| - var heightWithUnit = $"height:{height.ToString(CultureInfo.InvariantCulture)}{heightUnit.ToCssString()}"; |
73 |
| - await JSRuntime.InvokeVoidAsync("window.blazorChart.resize", Id, widthWithUnit, heightWithUnit); |
| 85 | + var _data = GetChartDataObject( chartData ); |
| 86 | + |
| 87 | + if( chartType == ChartType.Bar ) |
| 88 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.bar.update", Id, GetChartType(), _data, (BarChartOptions)chartOptions ); |
| 89 | + else if( chartType == ChartType.Line ) |
| 90 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.line.update", Id, GetChartType(), _data, (LineChartOptions)chartOptions ); |
| 91 | + else |
| 92 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.update", Id, GetChartType(), _data, chartOptions ); |
74 | 93 | }
|
75 |
| - |
76 |
| - /// <summary> |
77 |
| - /// Update chart. |
78 |
| - /// </summary> |
79 |
| - /// <param name="chartData"></param> |
80 |
| - /// <param name="chartOptions"></param> |
81 |
| - public virtual async Task UpdateAsync(ChartData chartData, IChartOptions chartOptions) |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Update chart. |
| 98 | + /// </summary> |
| 99 | + /// <param name="chartData"></param> |
| 100 | + /// <param name="chartOptions"></param> |
| 101 | + public virtual async Task UpdateValuesAsync( ChartData chartData ) |
| 102 | + { |
| 103 | + if( chartData is not null && chartData.Datasets is not null && chartData.Datasets.Any() ) |
82 | 104 | {
|
83 |
| - if (chartData is not null && chartData.Datasets is not null && chartData.Datasets.Any()) |
84 |
| - { |
85 |
| - var _data = GetChartDataObject(chartData); |
86 |
| - |
87 |
| - if (chartType == ChartType.Bar) |
88 |
| - await JSRuntime.InvokeVoidAsync("window.blazorChart.bar.update", Id, GetChartType(), _data, (BarChartOptions)chartOptions); |
89 |
| - else if (chartType == ChartType.Line) |
90 |
| - await JSRuntime.InvokeVoidAsync("window.blazorChart.line.update", Id, GetChartType(), _data, (LineChartOptions)chartOptions); |
91 |
| - else |
92 |
| - await JSRuntime.InvokeVoidAsync("window.blazorChart.update", Id, GetChartType(), _data, chartOptions); |
93 |
| - } |
| 105 | + var _data = GetChartDataObject( chartData ); |
| 106 | + |
| 107 | + if( chartType == ChartType.Bar ) |
| 108 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.bar.updateDataValues", Id, _data ); |
| 109 | + else if( chartType == ChartType.Line ) |
| 110 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.line.updateDataValues", Id, _data ); |
| 111 | + else |
| 112 | + await JSRuntime.InvokeVoidAsync( "window.blazorChart.updateDataValues", Id, _data ); |
94 | 113 | }
|
| 114 | + } |
95 | 115 |
|
96 |
| - protected string GetChartType() => |
| 116 | + protected string GetChartType() => |
97 | 117 | chartType switch
|
98 | 118 | {
|
99 |
| - ChartType.Bar => "bar", |
100 |
| - ChartType.Bubble => "bubble", |
101 |
| - ChartType.Doughnut => "doughnut", |
102 |
| - ChartType.Line => "line", |
103 |
| - ChartType.Pie => "pie", |
104 |
| - ChartType.PolarArea => "polarArea", |
105 |
| - ChartType.Radar => "radar", |
106 |
| - ChartType.Scatter => "scatter", |
107 |
| - _ => "line" // default |
| 119 | + ChartType.Bar => "bar", |
| 120 | + ChartType.Bubble => "bubble", |
| 121 | + ChartType.Doughnut => "doughnut", |
| 122 | + ChartType.Line => "line", |
| 123 | + ChartType.Pie => "pie", |
| 124 | + ChartType.PolarArea => "polarArea", |
| 125 | + ChartType.Radar => "radar", |
| 126 | + ChartType.Scatter => "scatter", |
| 127 | + _ => "line" // default |
108 | 128 | };
|
109 | 129 |
|
110 |
| - private string GetChartContainerSizeAsStyle() |
111 |
| - { |
112 |
| - var style = ""; |
113 |
| - |
114 |
| - if (Width > 0) |
115 |
| - style += $"width:{Width.Value.ToString(CultureInfo.InvariantCulture)}{WidthUnit.ToCssString()};"; |
116 |
| - |
117 |
| - if (Height > 0) |
118 |
| - style += $"height:{Height.Value.ToString(CultureInfo.InvariantCulture)}{HeightUnit.ToCssString()};"; |
119 |
| - |
120 |
| - return style; |
121 |
| - } |
122 |
| - |
123 |
| - private object GetChartDataObject(ChartData chartData) |
124 |
| - { |
125 |
| - var datasets = new List<object>(); |
126 |
| - |
127 |
| - if (chartData?.Datasets?.Any() ?? false) |
128 |
| - foreach (var dataset in chartData.Datasets) |
129 |
| - if (dataset is BarChartDataset) |
130 |
| - datasets.Add((BarChartDataset)dataset); |
131 |
| - else if (dataset is DoughnutChartDataset) |
132 |
| - datasets.Add((DoughnutChartDataset)dataset); |
133 |
| - else if (dataset is LineChartDataset) |
134 |
| - datasets.Add((LineChartDataset)dataset); |
135 |
| - else if (dataset is PieChartDataset) |
136 |
| - datasets.Add((PieChartDataset)dataset); |
137 |
| - else if (dataset is PolarAreaChartDataset) |
138 |
| - datasets.Add((PolarAreaChartDataset)dataset); |
139 |
| - |
140 |
| - var data = new { chartData?.Labels, Datasets = datasets }; |
141 |
| - |
142 |
| - return data; |
143 |
| - } |
144 |
| - |
145 |
| - #endregion |
146 |
| - |
147 |
| - #region Properties, Indexers |
148 |
| - |
149 |
| - internal string ContainerStyle => GetChartContainerSizeAsStyle(); |
150 |
| - |
151 |
| - /// <summary> |
152 |
| - /// Gets or sets chart container height. |
153 |
| - /// The default unit of measure is <see cref="Unit.Px" />. |
154 |
| - /// To change the unit of measure see <see cref="HeightUnit" />. |
155 |
| - /// </summary> |
156 |
| - /// <remarks> |
157 |
| - /// Default value is null. |
158 |
| - /// </remarks> |
159 |
| - [Parameter] |
160 |
| - public int? Height { get; set; } |
161 |
| - |
162 |
| - /// <summary> |
163 |
| - /// Gets or sets chart container height unit of measure. |
164 |
| - /// </summary> |
165 |
| - /// <remarks> |
166 |
| - /// Default value is <see cref="Unit.Px" />. |
167 |
| - /// </remarks> |
168 |
| - [Parameter] |
169 |
| - public Unit HeightUnit { get; set; } = Unit.Px; |
170 |
| - |
171 |
| - /// <summary> |
172 |
| - /// Get or sets chart container width. |
173 |
| - /// The default unit of measure is <see cref="Unit.Px" />. |
174 |
| - /// To change the unit of measure see <see cref="WidthUnit" />. |
175 |
| - /// </summary> |
176 |
| - /// <remarks> |
177 |
| - /// Default value is null. |
178 |
| - /// </remarks> |
179 |
| - [Parameter] |
180 |
| - public int? Width { get; set; } |
181 |
| - |
182 |
| - /// <summary> |
183 |
| - /// Gets or sets chart container width unit of measure. |
184 |
| - /// </summary> |
185 |
| - /// <remarks> |
186 |
| - /// Default value is <see cref="Unit.Px" />. |
187 |
| - /// </remarks> |
188 |
| - [Parameter] |
189 |
| - public Unit WidthUnit { get; set; } = Unit.Px; |
190 |
| - |
191 |
| - #endregion |
| 130 | + private string GetChartContainerSizeAsStyle() |
| 131 | + { |
| 132 | + var style = ""; |
| 133 | + |
| 134 | + if( Width > 0 ) |
| 135 | + style += $"width:{Width.Value.ToString( CultureInfo.InvariantCulture )}{WidthUnit.ToCssString()};"; |
| 136 | + |
| 137 | + if( Height > 0 ) |
| 138 | + style += $"height:{Height.Value.ToString( CultureInfo.InvariantCulture )}{HeightUnit.ToCssString()};"; |
| 139 | + |
| 140 | + return style; |
| 141 | + } |
| 142 | + |
| 143 | + private object GetChartDataObject( ChartData chartData ) |
| 144 | + { |
| 145 | + var datasets = new List<object>(); |
| 146 | + |
| 147 | + if( chartData?.Datasets?.Any() ?? false ) |
| 148 | + foreach( var dataset in chartData.Datasets ) |
| 149 | + if( dataset is BarChartDataset ) |
| 150 | + datasets.Add( (BarChartDataset)dataset ); |
| 151 | + else if( dataset is DoughnutChartDataset ) |
| 152 | + datasets.Add( (DoughnutChartDataset)dataset ); |
| 153 | + else if( dataset is LineChartDataset ) |
| 154 | + datasets.Add( (LineChartDataset)dataset ); |
| 155 | + else if( dataset is PieChartDataset ) |
| 156 | + datasets.Add( (PieChartDataset)dataset ); |
| 157 | + else if( dataset is PolarAreaChartDataset ) |
| 158 | + datasets.Add( (PolarAreaChartDataset)dataset ); |
| 159 | + |
| 160 | + var data = new { chartData?.Labels, Datasets = datasets }; |
| 161 | + |
| 162 | + return data; |
| 163 | + } |
| 164 | + |
| 165 | + #endregion |
| 166 | + |
| 167 | + #region Properties, Indexers |
| 168 | + |
| 169 | + internal string ContainerStyle => GetChartContainerSizeAsStyle(); |
| 170 | + |
| 171 | + /// <summary> |
| 172 | + /// Gets or sets chart container height. |
| 173 | + /// The default unit of measure is <see cref="Unit.Px" />. |
| 174 | + /// To change the unit of measure see <see cref="HeightUnit" />. |
| 175 | + /// </summary> |
| 176 | + /// <remarks> |
| 177 | + /// Default value is null. |
| 178 | + /// </remarks> |
| 179 | + [Parameter] |
| 180 | + public int? Height { get; set; } |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Gets or sets chart container height unit of measure. |
| 184 | + /// </summary> |
| 185 | + /// <remarks> |
| 186 | + /// Default value is <see cref="Unit.Px" />. |
| 187 | + /// </remarks> |
| 188 | + [Parameter] |
| 189 | + public Unit HeightUnit { get; set; } = Unit.Px; |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Get or sets chart container width. |
| 193 | + /// The default unit of measure is <see cref="Unit.Px" />. |
| 194 | + /// To change the unit of measure see <see cref="WidthUnit" />. |
| 195 | + /// </summary> |
| 196 | + /// <remarks> |
| 197 | + /// Default value is null. |
| 198 | + /// </remarks> |
| 199 | + [Parameter] |
| 200 | + public int? Width { get; set; } |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Gets or sets chart container width unit of measure. |
| 204 | + /// </summary> |
| 205 | + /// <remarks> |
| 206 | + /// Default value is <see cref="Unit.Px" />. |
| 207 | + /// </remarks> |
| 208 | + [Parameter] |
| 209 | + public Unit WidthUnit { get; set; } = Unit.Px; |
| 210 | + |
| 211 | + #endregion |
192 | 212 | }
|
0 commit comments