@agions/taroviz 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +318 -53
- package/dist/index.esm.js +67955 -3318
- package/package.json +102 -20
- package/src/__tests__/integration.test.tsx +168 -0
- package/src/adapters/__tests__/index.test.ts +91 -0
- package/src/adapters/h5/__tests__/index.test.ts +156 -0
- package/src/adapters/h5/index.ts +301 -0
- package/src/adapters/harmony/index.ts +274 -0
- package/src/adapters/index.ts +234 -0
- package/src/adapters/swan/index.ts +274 -0
- package/src/adapters/tt/index.ts +274 -0
- package/src/adapters/types.ts +162 -0
- package/src/adapters/weapp/index.ts +237 -0
- package/src/charts/bar/__tests__/index.test.tsx +113 -0
- package/src/charts/bar/index.tsx +27 -0
- package/src/charts/common/BaseChartWrapper.tsx +136 -0
- package/src/charts/funnel/index.tsx +33 -0
- package/src/charts/gauge/index.tsx +33 -0
- package/src/charts/heatmap/index.tsx +33 -0
- package/src/charts/index.ts +21 -0
- package/src/charts/line/__tests__/index.test.tsx +107 -0
- package/src/charts/line/index.tsx +27 -0
- package/src/charts/pie/__tests__/index.test.tsx +112 -0
- package/src/charts/pie/index.tsx +22 -0
- package/src/charts/radar/index.tsx +33 -0
- package/src/charts/scatter/index.tsx +33 -0
- package/src/charts/types.ts +146 -0
- package/src/charts/utils.ts +56 -0
- package/src/core/__tests__/platform.test.ts +48 -0
- package/src/core/animation/AnimationManager.ts +391 -0
- package/src/core/animation/index.ts +20 -0
- package/src/core/animation/types.ts +248 -0
- package/src/core/components/BaseChart.tsx +1319 -0
- package/src/core/index.ts +19 -0
- package/src/core/types/chart.ts +66 -0
- package/src/core/types/common.ts +224 -0
- package/src/core/types/index.ts +281 -0
- package/src/core/types/platform.ts +325 -0
- package/src/core/utils/__tests__/common.test.ts +52 -0
- package/src/core/utils/__tests__/environment.test.ts +94 -0
- package/src/core/utils/__tests__/i18n.test.ts +247 -0
- package/src/core/utils/__tests__/index.test.ts +219 -0
- package/src/core/utils/__tests__/uuid.test.ts +78 -0
- package/src/core/utils/chartInstances.ts +69 -0
- package/src/core/utils/codeGenerator/CodeGenerator.ts +655 -0
- package/src/core/utils/codeGenerator/index.ts +13 -0
- package/src/core/utils/codeGenerator/types.ts +200 -0
- package/src/core/utils/common.ts +58 -0
- package/src/core/utils/configGenerator/ConfigGenerator.ts +583 -0
- package/src/core/utils/configGenerator/index.ts +13 -0
- package/src/core/utils/configGenerator/types.ts +445 -0
- package/src/core/utils/debug/DebugPanel.tsx +637 -0
- package/src/core/utils/debug/debugger.ts +322 -0
- package/src/core/utils/debug/index.ts +21 -0
- package/src/core/utils/debug/types.ts +142 -0
- package/src/core/utils/i18n.ts +452 -0
- package/src/core/utils/index.ts +162 -0
- package/src/core/utils/performance/PerformanceAnalyzer.ts +586 -0
- package/src/core/utils/performance/index.ts +13 -0
- package/src/core/utils/performance/types.ts +180 -0
- package/src/core/utils/uuid.ts +30 -0
- package/src/editor/ThemeEditor.tsx +449 -0
- package/src/editor/index.ts +10 -0
- package/src/hooks/__tests__/index.test.tsx +333 -0
- package/src/hooks/index.ts +214 -0
- package/src/index.ts +75 -0
- package/src/main.tsx +247 -0
- package/src/react-dom.d.ts +7 -0
- package/src/themes/__tests__/index.test.ts +91 -0
- package/src/themes/index.ts +465 -0
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js +0 -4012
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TaroViz 字节跳动小程序适配器
|
|
3
|
+
* 基于字节跳动小程序canvas组件实现图表渲染
|
|
4
|
+
*/
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
|
|
7
|
+
import { Adapter, AdapterOptions } from '../types';
|
|
8
|
+
|
|
9
|
+
// 扩展 AdapterOptions 类型
|
|
10
|
+
interface ExtendedTTAdapterOptions extends AdapterOptions {
|
|
11
|
+
/**
|
|
12
|
+
* 字节跳动小程序组件实例
|
|
13
|
+
*/
|
|
14
|
+
component?: any;
|
|
15
|
+
/**
|
|
16
|
+
* 画布ID
|
|
17
|
+
*/
|
|
18
|
+
canvasId?: string;
|
|
19
|
+
/**
|
|
20
|
+
* 宽度
|
|
21
|
+
*/
|
|
22
|
+
width?: number | string;
|
|
23
|
+
/**
|
|
24
|
+
* 高度
|
|
25
|
+
*/
|
|
26
|
+
height?: number | string;
|
|
27
|
+
/**
|
|
28
|
+
* 主题
|
|
29
|
+
*/
|
|
30
|
+
theme?: string | object;
|
|
31
|
+
/**
|
|
32
|
+
* 图表选项
|
|
33
|
+
*/
|
|
34
|
+
option?: any;
|
|
35
|
+
/**
|
|
36
|
+
* 初始化回调
|
|
37
|
+
*/
|
|
38
|
+
onInit?: (instance: any) => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 字节跳动小程序环境下的图表适配器
|
|
43
|
+
*/
|
|
44
|
+
class TTAdapter implements Adapter {
|
|
45
|
+
/**
|
|
46
|
+
* 配置项
|
|
47
|
+
*/
|
|
48
|
+
private config: ExtendedTTAdapterOptions;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 图表实例
|
|
52
|
+
*/
|
|
53
|
+
private chartInstance: any | null = null;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 组件实例
|
|
57
|
+
*/
|
|
58
|
+
private component: any | null = null;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 构造函数
|
|
62
|
+
* @param config 适配器配置
|
|
63
|
+
*/
|
|
64
|
+
constructor(config: ExtendedTTAdapterOptions) {
|
|
65
|
+
this.config = config;
|
|
66
|
+
this.component = config.component;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 创建字节跳动小程序适配器实例
|
|
71
|
+
* @param options 适配器选项
|
|
72
|
+
* @returns 适配器实例
|
|
73
|
+
*/
|
|
74
|
+
static create(options: ExtendedTTAdapterOptions): TTAdapter {
|
|
75
|
+
return new TTAdapter(options);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 获取图表实例
|
|
80
|
+
*/
|
|
81
|
+
getInstance(): any {
|
|
82
|
+
return this.chartInstance;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 初始化图表
|
|
87
|
+
*/
|
|
88
|
+
init(): any {
|
|
89
|
+
const { canvasId, width, height, theme, option } = this.config;
|
|
90
|
+
|
|
91
|
+
if (!this.component) {
|
|
92
|
+
console.error('[TaroViz] TTAdapter: component is required');
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!canvasId) {
|
|
97
|
+
console.error('[TaroViz] TTAdapter: canvasId is required');
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 创建图表实例
|
|
102
|
+
const chart = this.component.createChart({
|
|
103
|
+
id: canvasId,
|
|
104
|
+
width: width,
|
|
105
|
+
height: height,
|
|
106
|
+
theme: theme,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// 设置图表选项
|
|
110
|
+
if (option) {
|
|
111
|
+
chart.setOption(option);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 存储图表实例
|
|
115
|
+
this.chartInstance = chart;
|
|
116
|
+
|
|
117
|
+
// 初始化回调
|
|
118
|
+
if (this.config.onInit) {
|
|
119
|
+
this.config.onInit(chart);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return chart;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 设置图表选项
|
|
127
|
+
*/
|
|
128
|
+
setOption(option: any, opts?: any): void {
|
|
129
|
+
if (this.chartInstance) {
|
|
130
|
+
this.chartInstance.setOption(option, opts);
|
|
131
|
+
} else {
|
|
132
|
+
this.config.option = option;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 设置主题
|
|
138
|
+
*/
|
|
139
|
+
setTheme(theme: string | object): void {
|
|
140
|
+
this.config.theme = theme;
|
|
141
|
+
if (this.chartInstance) {
|
|
142
|
+
this.chartInstance.setTheme?.(theme);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 获取图表宽度
|
|
148
|
+
*/
|
|
149
|
+
getWidth(): number {
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 获取图表高度
|
|
155
|
+
*/
|
|
156
|
+
getHeight(): number {
|
|
157
|
+
return 0;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 获取DOM元素
|
|
162
|
+
*/
|
|
163
|
+
getDom(): HTMLElement | null {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 转换为DataURL
|
|
169
|
+
*/
|
|
170
|
+
convertToDataURL(opts?: any): string | undefined {
|
|
171
|
+
return this.chartInstance?.getDataURL(opts);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 清空图表
|
|
176
|
+
*/
|
|
177
|
+
clear(): void {
|
|
178
|
+
if (this.chartInstance) {
|
|
179
|
+
this.chartInstance.clear();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 绑定事件
|
|
185
|
+
*/
|
|
186
|
+
on(event: string, handler: (params: any) => void): void {
|
|
187
|
+
if (this.chartInstance) {
|
|
188
|
+
this.chartInstance.on(event, handler);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 解绑事件
|
|
194
|
+
*/
|
|
195
|
+
off(event: string, handler?: (params: any) => void): void {
|
|
196
|
+
if (this.chartInstance) {
|
|
197
|
+
this.chartInstance.off(event, handler);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 显示加载动画
|
|
203
|
+
*/
|
|
204
|
+
showLoading(opts?: object): void {
|
|
205
|
+
if (this.chartInstance) {
|
|
206
|
+
this.chartInstance.showLoading(opts);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 隐藏加载动画
|
|
212
|
+
*/
|
|
213
|
+
hideLoading(): void {
|
|
214
|
+
if (this.chartInstance) {
|
|
215
|
+
this.chartInstance.hideLoading();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 销毁图表
|
|
221
|
+
*/
|
|
222
|
+
dispose(): void {
|
|
223
|
+
if (this.chartInstance) {
|
|
224
|
+
this.chartInstance.dispose();
|
|
225
|
+
this.chartInstance = null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 处理图表大小变化
|
|
231
|
+
*/
|
|
232
|
+
resize(opts?: any): void {
|
|
233
|
+
if (this.chartInstance) {
|
|
234
|
+
this.chartInstance.resize(opts);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 设置组件实例
|
|
240
|
+
*/
|
|
241
|
+
setComponent(component: any): void {
|
|
242
|
+
this.component = component;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 渲染图表组件
|
|
247
|
+
*/
|
|
248
|
+
render(): JSX.Element {
|
|
249
|
+
const { canvasId = 'ec-canvas', width = '100%', height = '300px', style = {} } = this.config;
|
|
250
|
+
// 注意:这里需要根据实际使用的Taro版本和组件库来调整
|
|
251
|
+
return React.createElement('view', {
|
|
252
|
+
id: canvasId,
|
|
253
|
+
style: { width, height, ...style },
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* 触发图表行为
|
|
259
|
+
*/
|
|
260
|
+
dispatchAction(payload: any): void {
|
|
261
|
+
if (this.chartInstance) {
|
|
262
|
+
this.chartInstance.dispatchAction(payload);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 获取DataURL
|
|
268
|
+
*/
|
|
269
|
+
getDataURL(opts?: any): string | undefined {
|
|
270
|
+
return this.chartInstance?.getDataURL(opts);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export default TTAdapter;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TaroViz 适配器类型定义
|
|
3
|
+
*/
|
|
4
|
+
import { CSSProperties } from 'react';
|
|
5
|
+
|
|
6
|
+
import { PlatformType, Adapter as CoreAdapter } from '../core';
|
|
7
|
+
|
|
8
|
+
export type Adapter = CoreAdapter;
|
|
9
|
+
export { PlatformType };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 基础适配器选项
|
|
13
|
+
*/
|
|
14
|
+
export interface AdapterOptions {
|
|
15
|
+
/**
|
|
16
|
+
* 画布ID
|
|
17
|
+
*/
|
|
18
|
+
canvasId?: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 宽度
|
|
22
|
+
*/
|
|
23
|
+
width?: number | string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 高度
|
|
27
|
+
*/
|
|
28
|
+
height?: number | string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 主题
|
|
32
|
+
*/
|
|
33
|
+
theme?: string | object;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 初始化回调
|
|
37
|
+
*/
|
|
38
|
+
onInit?: (instance: any) => void;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 图表选项
|
|
42
|
+
*/
|
|
43
|
+
option?: any;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 样式
|
|
47
|
+
*/
|
|
48
|
+
style?: CSSProperties;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 是否自动调整大小
|
|
52
|
+
*/
|
|
53
|
+
autoResize?: boolean;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 设备像素比
|
|
57
|
+
*/
|
|
58
|
+
devicePixelRatio?: number;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 渲染器类型
|
|
62
|
+
*/
|
|
63
|
+
renderer?: 'canvas' | 'svg';
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* CSS类名
|
|
67
|
+
*/
|
|
68
|
+
className?: string;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 容器引用
|
|
72
|
+
*/
|
|
73
|
+
containerRef?: any;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 额外的平台特定选项
|
|
77
|
+
*/
|
|
78
|
+
[key: string]: any;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* H5适配器选项
|
|
83
|
+
*/
|
|
84
|
+
export interface H5AdapterOptions extends AdapterOptions {
|
|
85
|
+
/**
|
|
86
|
+
* 容器引用
|
|
87
|
+
*/
|
|
88
|
+
containerRef?: any;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 微信小程序适配器选项
|
|
93
|
+
*/
|
|
94
|
+
export interface WeappAdapterOptions extends AdapterOptions {
|
|
95
|
+
/**
|
|
96
|
+
* 微信小程序组件实例
|
|
97
|
+
*/
|
|
98
|
+
component?: any;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 支付宝小程序适配器选项
|
|
103
|
+
*/
|
|
104
|
+
export interface AlipayAdapterOptions extends AdapterOptions {
|
|
105
|
+
/**
|
|
106
|
+
* 支付宝小程序特有属性
|
|
107
|
+
*/
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 百度小程序适配器选项
|
|
112
|
+
*/
|
|
113
|
+
export interface SwanAdapterOptions extends AdapterOptions {
|
|
114
|
+
/**
|
|
115
|
+
* 百度小程序特有属性
|
|
116
|
+
*/
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 鸿蒙OS适配器选项
|
|
121
|
+
*/
|
|
122
|
+
export interface HarmonyAdapterOptions extends AdapterOptions {
|
|
123
|
+
/**
|
|
124
|
+
* HarmonyOS特有属性
|
|
125
|
+
*/
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 钉钉小程序适配器选项
|
|
130
|
+
*/
|
|
131
|
+
export interface DDAdapterOptions extends AdapterOptions {
|
|
132
|
+
/**
|
|
133
|
+
* 钉钉小程序特有属性
|
|
134
|
+
*/
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 企业微信小程序适配器选项
|
|
139
|
+
*/
|
|
140
|
+
export interface QywxAdapterOptions extends AdapterOptions {
|
|
141
|
+
/**
|
|
142
|
+
* 企业微信小程序特有属性
|
|
143
|
+
*/
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 飞书小程序适配器选项
|
|
148
|
+
*/
|
|
149
|
+
export interface LarkAdapterOptions extends AdapterOptions {
|
|
150
|
+
/**
|
|
151
|
+
* 飞书小程序特有属性
|
|
152
|
+
*/
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 小程序通用适配器选项
|
|
157
|
+
*/
|
|
158
|
+
export interface MiniAppAdapterOptions extends AdapterOptions {
|
|
159
|
+
/**
|
|
160
|
+
* 小程序特有属性
|
|
161
|
+
*/
|
|
162
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TaroViz 微信小程序适配器
|
|
3
|
+
* 基于微信小程序canvas组件实现图表渲染
|
|
4
|
+
*/
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
|
|
7
|
+
import { Adapter, WeappAdapterOptions } from '../types';
|
|
8
|
+
|
|
9
|
+
// 扩展 WeappAdapterOptions 类型
|
|
10
|
+
interface ExtendedWeappAdapterOptions extends WeappAdapterOptions {
|
|
11
|
+
component?: any;
|
|
12
|
+
canvasId?: string;
|
|
13
|
+
width?: number | string;
|
|
14
|
+
height?: number | string;
|
|
15
|
+
theme?: string | object;
|
|
16
|
+
option?: any;
|
|
17
|
+
onInit?: (instance: any) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 微信小程序环境下的图表适配器
|
|
22
|
+
*/
|
|
23
|
+
class WeappAdapter implements Adapter {
|
|
24
|
+
/**
|
|
25
|
+
* 配置项
|
|
26
|
+
*/
|
|
27
|
+
private config: ExtendedWeappAdapterOptions;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 图表实例
|
|
31
|
+
*/
|
|
32
|
+
private chartInstance: any | null = null;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 组件实例
|
|
36
|
+
*/
|
|
37
|
+
private component: any | null = null;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 构造函数
|
|
41
|
+
* @param config 适配器配置
|
|
42
|
+
*/
|
|
43
|
+
constructor(config: ExtendedWeappAdapterOptions) {
|
|
44
|
+
this.config = config;
|
|
45
|
+
this.component = config.component;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 创建微信小程序适配器实例
|
|
50
|
+
* @param options 适配器选项
|
|
51
|
+
* @returns 适配器实例
|
|
52
|
+
*/
|
|
53
|
+
static create(options: ExtendedWeappAdapterOptions): WeappAdapter {
|
|
54
|
+
return new WeappAdapter(options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 获取图表实例
|
|
59
|
+
*/
|
|
60
|
+
getInstance(): any {
|
|
61
|
+
return this.chartInstance;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 初始化图表
|
|
66
|
+
*/
|
|
67
|
+
init(): any {
|
|
68
|
+
const { canvasId, width, height, theme, option } = this.config;
|
|
69
|
+
|
|
70
|
+
if (!this.component) {
|
|
71
|
+
console.error('[TaroViz] WeappAdapter: component is required');
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!canvasId) {
|
|
76
|
+
console.error('[TaroViz] WeappAdapter: canvasId is required');
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 创建图表实例
|
|
81
|
+
const chart = this.component.createChart({
|
|
82
|
+
id: canvasId,
|
|
83
|
+
width: width,
|
|
84
|
+
height: height,
|
|
85
|
+
theme: theme,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// 设置图表选项
|
|
89
|
+
if (option) {
|
|
90
|
+
chart.setOption(option);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 存储图表实例
|
|
94
|
+
this.chartInstance = chart;
|
|
95
|
+
|
|
96
|
+
// 初始化回调
|
|
97
|
+
if (this.config.onInit) {
|
|
98
|
+
this.config.onInit(chart);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return chart;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 设置图表选项
|
|
106
|
+
*/
|
|
107
|
+
setOption(option: any, opts?: any): void {
|
|
108
|
+
if (this.chartInstance) {
|
|
109
|
+
this.chartInstance.setOption(option, opts);
|
|
110
|
+
} else {
|
|
111
|
+
this.config.option = option;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 设置主题
|
|
117
|
+
*/
|
|
118
|
+
setTheme(theme: string | object): void {
|
|
119
|
+
this.config.theme = theme;
|
|
120
|
+
if (this.chartInstance) {
|
|
121
|
+
this.chartInstance.setTheme?.(theme);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 获取图表宽度
|
|
127
|
+
*/
|
|
128
|
+
getWidth(): number {
|
|
129
|
+
return 0;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 获取图表高度
|
|
134
|
+
*/
|
|
135
|
+
getHeight(): number {
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 获取DOM元素
|
|
141
|
+
*/
|
|
142
|
+
getDom(): HTMLElement | null {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 转换为DataURL
|
|
148
|
+
*/
|
|
149
|
+
convertToDataURL(opts?: any): string | undefined {
|
|
150
|
+
return this.chartInstance?.getDataURL(opts);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 清空图表
|
|
155
|
+
*/
|
|
156
|
+
clear(): void {
|
|
157
|
+
if (this.chartInstance) {
|
|
158
|
+
this.chartInstance.clear();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 绑定事件
|
|
164
|
+
*/
|
|
165
|
+
on(event: string, handler: (params: any) => void): void {
|
|
166
|
+
if (this.chartInstance) {
|
|
167
|
+
this.chartInstance.on(event, handler);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 解绑事件
|
|
173
|
+
*/
|
|
174
|
+
off(event: string, handler?: (params: any) => void): void {
|
|
175
|
+
if (this.chartInstance) {
|
|
176
|
+
this.chartInstance.off(event, handler);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 显示加载动画
|
|
182
|
+
*/
|
|
183
|
+
showLoading(opts?: object): void {
|
|
184
|
+
if (this.chartInstance) {
|
|
185
|
+
this.chartInstance.showLoading(opts);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 隐藏加载动画
|
|
191
|
+
*/
|
|
192
|
+
hideLoading(): void {
|
|
193
|
+
if (this.chartInstance) {
|
|
194
|
+
this.chartInstance.hideLoading();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 销毁图表
|
|
200
|
+
*/
|
|
201
|
+
dispose(): void {
|
|
202
|
+
if (this.chartInstance) {
|
|
203
|
+
this.chartInstance.dispose();
|
|
204
|
+
this.chartInstance = null;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 处理图表大小变化
|
|
210
|
+
*/
|
|
211
|
+
resize(opts?: any): void {
|
|
212
|
+
if (this.chartInstance) {
|
|
213
|
+
this.chartInstance.resize(opts);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 设置组件实例
|
|
219
|
+
*/
|
|
220
|
+
setComponent(component: any): void {
|
|
221
|
+
this.component = component;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 渲染图表组件
|
|
226
|
+
*/
|
|
227
|
+
render(): JSX.Element {
|
|
228
|
+
const { canvasId = 'ec-canvas', width = '100%', height = '300px', style = {} } = this.config;
|
|
229
|
+
// 注意:这里需要根据实际使用的Taro版本和组件库来调整
|
|
230
|
+
return React.createElement('view', {
|
|
231
|
+
id: canvasId,
|
|
232
|
+
style: { width, height, ...style },
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export default WeappAdapter;
|