-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathChart.razor.cs
More file actions
309 lines (265 loc) · 8.65 KB
/
Chart.razor.cs
File metadata and controls
309 lines (265 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
namespace BootstrapBlazor.Components;
/// <summary>
/// Chart 组件基类
/// </summary>
public partial class Chart
{
/// <summary>
/// 获得 样式集合
/// </summary>
private string? ClassName => CssBuilder.Default("chart d-flex justify-content-center align-items-center position-relative is-loading")
.AddClassFromAttributes(AdditionalAttributes)
.Build();
/// <summary>
/// 获得/设置 组件 Style 字符串
/// </summary>
private string? StyleString => CssBuilder.Default()
.AddClass($"height: {Height};", !string.IsNullOrEmpty(Height))
.AddClass($"width: {Width};", !string.IsNullOrEmpty(Width))
.Build();
/// <summary>
/// 获得/设置 图表标题
/// </summary>
[Parameter]
public string? Title { get; set; }
/// <summary>
/// 获得/设置 组件高度支持单位<para>如: 30% , 30px , 30em , calc(30%)</para>
/// </summary>
[Parameter]
public string? Height { get; set; }
/// <summary>
/// 获得/设置 组件宽度支持单位<para>如: 30% , 30px , 30em , calc(30%)</para>
/// </summary>
[Parameter]
public string? Width { get; set; }
/// <summary>
/// 获得/设置 图表所在 canvas 是否随其容器大小变化而变化 默认为 true
/// </summary>
[Parameter]
public bool Responsive { get; set; } = true;
/// <summary>
/// 获取/设置 是否 约束图表比例 默认为 true
/// </summary>
[Parameter]
public bool MaintainAspectRatio { get; set; } = true;
/// <summary>
/// 获得/设置 设置 canvas 的宽高比(值为1表示 canvas 是正方形),如果显示定义了 canvas 的高度,则此属性无效 默认为 2
/// </summary>
[Parameter]
public int AspectRatio { get; set; } = 2;
/// <summary>
/// 获得/设置 图表尺寸延迟变化时间 默认为 0
/// </summary>
[Parameter]
public int ResizeDelay { get; set; } = 0;
/// <summary>
/// 获得/设置 Bubble 模式下显示角度 180 为 半圆 360 为正圆
/// </summary>
[Parameter]
public int Angle { get; set; }
/// <summary>
/// 获得/设置 正在加载文本
/// </summary>
[Parameter]
[NotNull]
public string? LoadingText { get; set; }
/// <summary>
/// 获得/设置 图表组件渲染类型 默认为 line 图
/// </summary>
[Parameter]
public ChartType ChartType { get; set; }
/// <summary>
/// 获得/设置 图表组件组件方法 默认为 Update
/// </summary>
[Parameter]
public ChartAction ChartAction { get; set; }
/// <summary>
/// 获得/设置 是否开启动画 默认为 true
/// </summary>
[Parameter]
public bool IsAnimation { get; set; } = true;
/// <summary>
/// 获得/设置 组件数据初始化委托方法
/// </summary>
[Parameter]
public Func<Task<ChartDataSource>>? OnInitAsync { get; set; }
/// <summary>
/// 获得/设置 点击图标数据回调方法
/// </summary>
[Parameter]
public Func<(int DatasetIndex, int Index), Task>? OnClickDataAsync { get; set; }
/// <summary>
/// 获得/设置 客户端绘制图表完毕后回调此委托方法
/// </summary>
[Parameter]
public Func<Task>? OnAfterInitAsync { get; set; }
/// <summary>
/// 获得/设置 客户端更新图表完毕后回调此委托方法
/// </summary>
[Parameter]
public Func<ChartAction, Task>? OnAfterUpdateAsync { get; set; }
[Inject]
[NotNull]
private IStringLocalizer<Chart>? Localizer { get; set; }
private bool UpdateDataSource { get; set; }
private ChartDataSource? _ds = null;
/// <summary>
/// OnInitialized 方法
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
LoadingText ??= Localizer[nameof(LoadingText)];
}
/// <summary>
/// OnAfterRenderAsync 方法
/// </summary>
/// <param name="firstRender"></param>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
if (OnInitAsync == null)
{
throw new InvalidOperationException($"{nameof(OnInitAsync)} parameter must be set. {nameof(OnInitAsync)} 回调方法必须设置");
}
if (OnInitAsync != null)
{
_ds = await OnInitAsync();
UpdateOptions(_ds);
if (Height != null && Width != null)
{
//设置了高度和宽度,会自动禁用约束图表比例,图表充满容器
_ds.Options.MaintainAspectRatio = false;
}
}
await InvokeVoidAsync("init", Id, Interop, nameof(Completed), _ds);
}
}
/// <summary>
/// 图表绘制完成后回调此方法
/// </summary>
[JSInvokable]
public async Task Completed()
{
if (OnAfterInitAsync != null)
{
await OnAfterInitAsync();
}
}
/// <summary>
/// 点击数据回调方法
/// </summary>
/// <param name="datasetIndex"></param>
/// <param name="index"></param>
/// <returns></returns>
[JSInvokable]
public async Task OnClickCallback(int datasetIndex, int index)
{
if (OnClickDataAsync != null)
{
await OnClickDataAsync((datasetIndex, index));
}
}
/// <summary>
/// 增加数据集
/// </summary>
/// <param name="dataset"></param>
/// <param name="index"></param>
/// <returns></returns>
public async Task AddDataset(ChartDataset dataset, int? index = null)
{
if (_ds == null)
{
if (OnInitAsync != null)
{
_ds = await OnInitAsync();
UpdateOptions(_ds);
}
}
if (_ds != null)
{
if (index.HasValue)
{
_ds.Data.Insert(index.Value, dataset);
}
else
{
_ds.Data.Add(dataset);
}
await InvokeVoidAsync("update", Id, _ds, ChartAction.AddDataset.ToDescriptionString());
if (OnAfterUpdateAsync != null)
{
await OnAfterUpdateAsync(ChartAction.AddDataset);
}
}
}
/// <summary>
/// 删除数据集
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public async Task RemoveDatasetAt(int index)
{
if (_ds == null)
{
if (OnInitAsync != null)
{
_ds = await OnInitAsync();
UpdateOptions(_ds);
}
}
if (_ds != null)
{
_ds.Data.RemoveAt(index);
}
await InvokeVoidAsync("update", Id, _ds, ChartAction.RemoveDataset.ToDescriptionString());
if (OnAfterUpdateAsync != null)
{
await OnAfterUpdateAsync(ChartAction.RemoveDataset);
}
}
/// <summary>
/// 更新图表方法
/// </summary>
public async Task Update(ChartAction action)
{
if (OnInitAsync != null)
{
var ds = await OnInitAsync();
UpdateOptions(ds);
await InvokeVoidAsync("update", Id, ds, action.ToDescriptionString(), Angle);
if (OnAfterUpdateAsync != null)
{
await OnAfterUpdateAsync(action);
}
}
}
/// <summary>
/// 重新加载方法, 强制重新渲染图表
/// </summary>
public Task Reload() => Update(ChartAction.Reload);
private void UpdateOptions(ChartDataSource ds)
{
ds.Options.OnClickDataMethod = OnClickDataAsync == null ? null : nameof(OnClickCallback);
ds.Type = ChartType.ToDescriptionString();
ds.Options.Title ??= Title;
ds.Options.Responsive = Responsive;
ds.Options.MaintainAspectRatio = MaintainAspectRatio;
ds.Options.AspectRatio = AspectRatio;
ds.Options.ResizeDelay = ResizeDelay;
ds.Options.Animation = IsAnimation;
}
/// <summary>
/// 生成图片方法
/// </summary>
/// <returns></returns>
public Task<byte[]?> ToImageAsync(string mimeType = "image/png") => InvokeAsync<byte[]?>("toImage", Id, mimeType);
}