1- // Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+ // Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33// Website: https://www.blazor.zone or https://argozhang.github.io/
44
@@ -131,6 +131,8 @@ public partial class Chart
131131
132132 private bool UpdateDataSource { get ; set ; }
133133
134+ private ChartDataSource ? _ds = null ;
135+
134136 /// <summary>
135137 /// OnInitialized 方法
136138 /// </summary>
@@ -156,20 +158,19 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
156158 throw new InvalidOperationException ( $ "{ nameof ( OnInitAsync ) } parameter must be set. { nameof ( OnInitAsync ) } 回调方法必须设置") ;
157159 }
158160
159- ChartDataSource ? ds = null ;
160161 if ( OnInitAsync != null )
161162 {
162- ds = await OnInitAsync ( ) ;
163- UpdateOptions ( ds ) ;
163+ _ds = await OnInitAsync ( ) ;
164+ UpdateOptions ( _ds ) ;
164165
165166 if ( Height != null && Width != null )
166167 {
167168 //设置了高度和宽度,会自动禁用约束图表比例,图表充满容器
168- ds . Options . MaintainAspectRatio = false ;
169+ _ds . Options . MaintainAspectRatio = false ;
169170 }
170171 }
171172
172- await InvokeVoidAsync ( "init" , Id , Interop , nameof ( Completed ) , ds ) ;
173+ await InvokeVoidAsync ( "init" , Id , Interop , nameof ( Completed ) , _ds ) ;
173174 }
174175 }
175176
@@ -200,6 +201,70 @@ public async Task OnClickCallback(int datasetIndex, int index)
200201 }
201202 }
202203
204+ /// <summary>
205+ /// 增加数据集
206+ /// </summary>
207+ /// <param name="dataset"></param>
208+ /// <param name="index"></param>
209+ /// <returns></returns>
210+ public async Task AddDataset ( ChartDataset dataset , int ? index = null )
211+ {
212+ if ( _ds == null )
213+ {
214+ if ( OnInitAsync != null )
215+ {
216+ _ds = await OnInitAsync ( ) ;
217+ UpdateOptions ( _ds ) ;
218+ }
219+ }
220+
221+ if ( _ds != null )
222+ {
223+ if ( index . HasValue )
224+ {
225+ _ds . Data . Insert ( index . Value , dataset ) ;
226+ }
227+ else
228+ {
229+ _ds . Data . Add ( dataset ) ;
230+ }
231+ await InvokeVoidAsync ( "update" , Id , _ds , ChartAction . AddDataset . ToDescriptionString ( ) ) ;
232+
233+ if ( OnAfterUpdateAsync != null )
234+ {
235+ await OnAfterUpdateAsync ( ChartAction . AddDataset ) ;
236+ }
237+ }
238+ }
239+
240+ /// <summary>
241+ /// 删除数据集
242+ /// </summary>
243+ /// <param name="index"></param>
244+ /// <returns></returns>
245+ public async Task RemoveDatasetAt ( int index )
246+ {
247+ if ( _ds == null )
248+ {
249+ if ( OnInitAsync != null )
250+ {
251+ _ds = await OnInitAsync ( ) ;
252+ UpdateOptions ( _ds ) ;
253+ }
254+ }
255+
256+ if ( _ds != null )
257+ {
258+ _ds . Data . RemoveAt ( index ) ;
259+ }
260+ await InvokeVoidAsync ( "update" , Id , _ds , ChartAction . RemoveDataset . ToDescriptionString ( ) ) ;
261+
262+ if ( OnAfterUpdateAsync != null )
263+ {
264+ await OnAfterUpdateAsync ( ChartAction . RemoveDataset ) ;
265+ }
266+ }
267+
203268 /// <summary>
204269 /// 更新图表方法
205270 /// </summary>
@@ -210,7 +275,7 @@ public async Task Update(ChartAction action)
210275 var ds = await OnInitAsync ( ) ;
211276 UpdateOptions ( ds ) ;
212277
213- await InvokeVoidAsync ( "update" , Id , Interop , ds , action . ToDescriptionString ( ) , Angle ) ;
278+ await InvokeVoidAsync ( "update" , Id , ds , action . ToDescriptionString ( ) , Angle ) ;
214279
215280 if ( OnAfterUpdateAsync != null )
216281 {
0 commit comments