Skip to content

Commit c8e2429

Browse files
committed
feat(Chart): add AddDataset method
1 parent e6395e7 commit c8e2429

3 files changed

Lines changed: 79 additions & 14 deletions

File tree

src/components/BootstrapBlazor.Chart/Components/Chart/Chart.razor.cs

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
{

src/components/BootstrapBlazor.Chart/Components/Chart/Chart.razor.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,10 @@ const getChartOption = function (option) {
414414

415415
const updateChart = function (config, option) {
416416
if (option.updateMethod === "addDataset") {
417-
config.data.datasets.push(option.data.datasets.pop())
417+
config.data.datasets = option.data.datasets;
418418
}
419419
else if (option.updateMethod === "removeDataset") {
420-
config.data.datasets.pop()
420+
config.data.datasets = option.data.datasets;
421421
}
422422
else if (option.updateMethod === "addData") {
423423
if (config.data.datasets.length > 0) {
@@ -472,7 +472,7 @@ export function init(id, invoke, method, option) {
472472
}
473473
const el = document.getElementById(id);
474474
const chart = new Chart(el.getElementsByTagName('canvas'), op)
475-
Data.set(id, chart)
475+
Data.set(id, { invoke, chart })
476476

477477
if (op.options.height !== null) {
478478
chart.canvas.parentNode.style.height = op.options.height
@@ -490,8 +490,8 @@ export function init(id, invoke, method, option) {
490490
EventHandler.on(window, 'resize', chart.resizeHandler)
491491
}
492492

493-
export function update(id, invoke, option, method, angle) {
494-
const chart = Data.get(id)
493+
export function update(id, option, method, angle) {
494+
const { invoke, chart } = Data.get(id)
495495
let op = getChartOption(option);
496496
handlerClickData(invoke, op, option.options.onClickDataMethod);
497497
op.angle = angle

src/components/BootstrapBlazor.Chart/Components/Chart/ChartDataSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

@@ -26,7 +26,7 @@ public class ChartDataSource
2626
/// 传递给 JS 的配置装箱实例,内部使用,添加数据集请使用 <see cref="Data"/> 属性。
2727
/// </summary>
2828
[JsonPropertyName("data")]
29-
public IEnumerable<object> DataJS { get => Data.Cast<object>().ToList(); }
29+
public IEnumerable<object> DataJS => Data.Cast<object>().ToList();
3030

3131
/// <summary>
3232
/// 获得 组件配置项 设置标题 轴坐标等

0 commit comments

Comments
 (0)