@visactor/taro-vchart 0.2.0-alpha.1 → 0.10.0-alpha.2

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 CHANGED
@@ -10,17 +10,15 @@ VChart 基于 [Taro](https://docs.taro.zone/docs/) 封装的图表组件。
10
10
 
11
11
  ## 支持环境
12
12
 
13
- 目前组件支持的环境有:**字节小程序**('tt'),**飞书小程序**('lark'),**浏览器**('h5', 'web')。
13
+ 目前组件支持的环境有:**微信小程序**('weapp'),**字节小程序**('tt'),**飞书小程序**('lark'),**浏览器**('web'),**h5 移动端**('h5')。
14
14
 
15
15
  以上环境通过 `type` 属性进行声明,`type` 属性值及对应环境如下:
16
16
 
17
+ - `weapp` 微信小程序。
17
18
  - `tt` 字节小程序。
18
-
19
19
  - `lark` 飞书小程序。
20
-
21
- - `h5` 浏览器环境, 与`web`等价。
22
-
23
- - `web` 浏览器环境, 与`h5`等价。
20
+ - `h5` h5 移动端环境。
21
+ - `web` 浏览器环境。
24
22
 
25
23
  ### 跨端支持
26
24
 
@@ -40,22 +38,22 @@ VChart 基于 [Taro](https://docs.taro.zone/docs/) 封装的图表组件。
40
38
 
41
39
  ### 版本要求
42
40
 
43
- **字节小程序端**
41
+ 1. **字节小程序端**
44
42
 
45
- 需要确保 **Taro 版本 >= 3.3.17**
43
+ 需要确保 **Taro 版本 >= 3.3.17** (taro 因为小版本有一些不兼容的 break change,所以尽量使用 3.3 版本)
46
44
 
47
- > taro 因为小版本有一些不兼容的 break change,所以尽量使用 3.3 版本
45
+ 2. **飞书小程序端**
48
46
 
49
- **飞书小程序端**
50
-
51
- 需要确保 **Taro 版本 >= 3.2.0**, **飞书版本 >= 3.45.0**
52
-
53
- > taro 因为小版本有一些不兼容的 break change,所以尽量使用 3.3 版本
47
+ 需要确保 **Taro 版本 >= 3.2.0**, **飞书版本 >= 3.45.0** (taro 因为小版本有一些不兼容的 break change,所以尽量使用 3.3 版本)
54
48
 
55
49
  ## 安装
56
50
 
57
- ```
58
- npm install @visactor/taro-vchart
51
+ ```bash
52
+ # npm
53
+ $ npm install @visactor/taro-vchart
54
+
55
+ # yarn
56
+ $ yarn add @visactor/taro-vchart
59
57
  ```
60
58
 
61
59
  ## API
@@ -63,34 +61,59 @@ npm install @visactor/taro-vchart
63
61
  图表组件使用示例如下:
64
62
 
65
63
  ```tsx
66
- <VCHart
64
+ import VChart from '@visactor/taro-vchart';
65
+
66
+ <VChart
67
67
  type="tt"
68
68
  spec={spec}
69
69
  canvasId="pie"
70
70
  style={{ height: '35vh', width: '100%' }}
71
71
  onChartInit={chart => {
72
- console.log('init pie');
72
+ console.log('onChartInit');
73
73
  }}
74
74
  onChartReady={chart => {
75
- console.log('ready pie');
75
+ console.log('onChartReady');
76
76
  }}
77
77
  onChartUpdate={chart => {
78
- console.log('update pie');
78
+ console.log('onChartUpdate');
79
79
  }}
80
- />
80
+ />;
81
81
  ```
82
82
 
83
- | API | 类型 | 说明 |
84
- | ------------- | -------- | ------------------------------------------------------------------------------------------------------- |
85
- | type | string | 配置的环境,目前组件支持的环境有:**字节小程序**('tt'),**飞书小程序**('lark'),**浏览器**('h5', 'web') |
86
- | canvasId | String | 图表 id, 必确唯一 |
87
- | spec | Object | 图表配置项, 请参考[VChart 配置项](todo) |
88
- | style | Object | 图表容器样式 |
89
- | events | Object[] | 事件绑定配置 |
90
- | options | Object | 初始化 VChart 实例传入的额外配置项,同 [VChart 实例化配置项](todo) |
91
- | onChartInit | Function | 图表初始化完后触发的回调 |
92
- | onChartReady | Function | 图表渲染完毕后触发的回调 |
93
- | onChartUpdate | Function | 图表更新完毕后触发的回调 |
83
+ | API | 类型 | 说明 |
84
+ | ------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
85
+ | type | String | 配置的环境,目前组件支持的环境有:**字节小程序**('tt'),**飞书小程序**('lark'),**浏览器**('web'),**h5 移动端**('h5'),如果没有声明,则会通过 `Taro.getEnv()` 自动获取 |
86
+ | canvasId | String | 图表 id, 必确唯一 |
87
+ | spec | Object | 图表配置项, 请参考[VChart 配置项](https://www.visactor.io/vchart/option/) |
88
+ | style | Object | 图表容器样式 |
89
+ | events | Object[] | 事件绑定配置,具体配置为定义[如下](#事件配置) |
90
+ | options | Object | 初始化 VChart 实例传入的额外配置项,同 [VChart 实例化配置项](https://www.visactor.io/vchart/api/API/vchart#options) |
91
+ | onChartInit | Function | 图表初始化完后触发的回调 |
92
+ | onChartReady | Function | 图表渲染完毕后触发的回调 |
93
+ | onChartUpdate | Function | 图表更新完毕后触发的回调 |
94
+
95
+ ### 事件配置
96
+
97
+ ```ts
98
+ interface IEvent {
99
+ /**
100
+ * 事件的名称
101
+ */
102
+ type: EventType;
103
+ /**
104
+ * 事件 API 中的事件筛选配置
105
+ */
106
+ query?: EventQuery;
107
+ /**
108
+ * 事件监听函数
109
+ */
110
+ handler: EventCallback<EventParams>;
111
+ }
112
+ ```
113
+
114
+ - `type` 代表事件名称,支持的值详见:[事件分类](https://www.visactor.io/vchart/api/API/event#%E4%BA%8B%E4%BB%B6%E5%88%86%E7%B1%BB)
115
+ - `query` 事件 API 中的事件筛选配置,使用详见:[事件过滤](https://www.visactor.io/vchart/api/API/event#%E4%BA%8B%E4%BB%B6%E8%BF%87%E6%BB%A4)
116
+ - `handler` 即事件监听函数,函数的参数类型详见:[事件参数](https://www.visactor.io/vchart/api/API/event#%E4%BA%8B%E4%BB%B6%E5%8F%82%E6%95%B0)
94
117
 
95
118
  ## 快速上手
96
119
 
@@ -173,21 +196,16 @@ export function Pie() {
173
196
 
174
197
  图表内部, 监听了 `spec` 的变化. 当 spec 变化时, 则会基于新的 `spec` 更新图表。
175
198
 
176
- 此外用户也可以使用 VChart 实例提供的渲染接口,进行图表的渲染、更新、销毁操作。
177
-
178
- > 可以通过 `onChartInit` 接口, 获取到 VChart 实例.
179
-
180
- #### API
199
+ 此外用户也可以使用 VChart 实例提供的渲染接口,进行图表的渲染、更新、销毁操作(可以通过 `onChartInit` 接口, 获取到 VChart 实例)。
181
200
 
182
- - `chartInstance.renderAsync()` 更新图表
201
+ 下面是 VChart 实例上提供的较常用的 API:
183
202
 
203
+ - `chartInstance.renderAsync()` 渲染图表
184
204
  - `chartInstance.release()` 销毁图表
185
-
186
205
  - `chartInstance.updateSpec()` 基于 Spec 更新图表
187
-
188
206
  - `chartInstance.updateData()` 基于数据更新图表
189
207
 
190
- 详细使用方法请参考:[VChart API](todo)
208
+ 详细使用方法请参考:[VChart API](https://www.visactor.io/vchart/api/API/vchart)
191
209
 
192
210
  #### 示例
193
211
 
@@ -293,3 +311,19 @@ export function Pie() {
293
311
  );
294
312
  }
295
313
  ```
314
+
315
+ ## 如何开发
316
+
317
+ ```bash
318
+ # 因为 taro-vchart 依赖 vchart 的编译结果,所以先要运行如下命令
319
+ $ rush run -p @visactor/vchart -s build:es5
320
+
321
+ # 运行 lark 小程序
322
+ $ rush run -p @visactor/taro-vchart -s dev:lark
323
+
324
+
325
+ # 运行微信小程序
326
+ $ rush run -p @visactor/taro-vchart -s dev:wx
327
+ ```
328
+
329
+ 然后使用飞书开发者工具导入 `packages/taro-vchart/dist/lark` 或者 `packages/taro-vchart/dist/weapp` 目录即可。
@@ -1,15 +1,20 @@
1
1
  import React from 'react';
2
2
  import { TTCanvas } from '../../utils';
3
- import { IChartProps, IDomRef } from '../../typings';
4
- export declare class GeneralChart extends React.Component<IChartProps> {
3
+ import { IChartProps, IDomRef, RenderMode } from '../../typings';
4
+ export interface GeneralChartProps extends IChartProps {
5
+ /**
6
+ * vchart 的环境参数
7
+ */
8
+ mode?: RenderMode;
9
+ }
10
+ export declare class GeneralChart extends React.Component<GeneralChartProps> {
5
11
  ttCanvas: TTCanvas;
6
- constructor(props: IChartProps);
7
12
  componentDidMount(): Promise<void>;
8
- componentDidUpdate(prevProps: IChartProps): void;
13
+ componentDidUpdate(prevProps: GeneralChartProps): void;
9
14
  componentWillUnmount(): void;
10
15
  init({ domref, dpr }: {
11
16
  domref: IDomRef;
12
17
  dpr: number;
13
- }): void;
18
+ }): Promise<void>;
14
19
  render(): React.JSX.Element;
15
20
  }
@@ -1,6 +1,12 @@
1
1
  import React from 'react';
2
- import { IChartProps } from '../../typings';
3
- export declare class WebChart extends React.Component<IChartProps> {
2
+ import { IChartProps, RenderMode } from '../../typings';
3
+ export interface WebChartProps extends IChartProps {
4
+ /**
5
+ * vchart 的环境参数
6
+ */
7
+ mode?: RenderMode;
8
+ }
9
+ export declare class WebChart extends React.Component<WebChartProps> {
4
10
  private vchart;
5
11
  constructor(props: IChartProps);
6
12
  componentDidMount(): void;
@@ -4,6 +4,7 @@ interface IVChartProps extends IChartProps {
4
4
  * 配置环境。如果没有声明,则会通过 `Taro.getEnv()` 自动获取。
5
5
  * - `tt` 字节小程序。
6
6
  * - `lark` 飞书小程序。
7
+ * - `weapp` 微信小程序
7
8
  * - `h5` 浏览器环境, 与`web`等价。
8
9
  * - `web` 浏览器环境, 与`h5`等价。
9
10
  */
package/lib/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import VChart$1 from '@visactor/vchart/build';
2
+ import VChart$1, { vglobal, registerLarkEnv, registerWXEnv } from '@visactor/vchart/build/es5';
3
3
  import { View, Canvas } from '@tarojs/components';
4
4
  import Taro from '@tarojs/taro';
5
5
 
@@ -45,8 +45,8 @@ class WebChart extends React.Component {
45
45
  super(props);
46
46
  }
47
47
  componentDidMount() {
48
- const { onChartInit, onChartReady } = this.props;
49
- this.vchart = new VChart$1(this.props.spec, Object.assign({ dom: `${this.props.canvasId}` }, this.props.options));
48
+ const { onChartInit, onChartReady, mode } = this.props;
49
+ this.vchart = new VChart$1(this.props.spec, Object.assign({ dom: `${this.props.canvasId}`, mode }, this.props.options));
50
50
  onChartInit && onChartInit(this.vchart);
51
51
  this.vchart
52
52
  .renderAsync()
@@ -77,6 +77,7 @@ class WebChart extends React.Component {
77
77
  }
78
78
  }
79
79
 
80
+ // @ts-ignore
80
81
  class TTCanvas {
81
82
  constructor(props) {
82
83
  const { dpr, events, spec, options, domref, mode, onChartInit, onChartReady, onChartUpdate } = props;
@@ -107,7 +108,7 @@ class TTCanvas {
107
108
  force: true,
108
109
  canvasIdLists: [`${domref.id}_draw_canvas`, `${domref.id}_tooltip_canvas`, `${domref.id}_hidden_canvas`],
109
110
  tooltipCanvasId: `${domref.id}_tooltip_canvas`,
110
- freeCanvasIdx: 1 // 自由 canvas 索引
111
+ freeCanvasIdx: 2 // 自由 canvas 索引
111
112
  }, dpr: this.dpr, renderCanvas: `${domref.id}_draw_canvas` }, this.options));
112
113
  this.onChartInit && this.onChartInit(this.chartInstance);
113
114
  // events
@@ -173,9 +174,6 @@ const style_container = {
173
174
  };
174
175
 
175
176
  class GeneralChart extends React.Component {
176
- constructor(props) {
177
- super(props);
178
- }
179
177
  componentDidMount() {
180
178
  return __awaiter(this, void 0, void 0, function* () {
181
179
  if (!this.props.spec || !this.props.canvasId) {
@@ -241,35 +239,46 @@ class GeneralChart extends React.Component {
241
239
  this.ttCanvas && this.ttCanvas.release();
242
240
  }
243
241
  init({ domref, dpr = 2 }) {
244
- if (!domref) {
245
- console.error(`未找到 #${this.props.canvasId} 组件`);
246
- return;
247
- }
248
- domref.id = this.props.canvasId;
249
- this.ttCanvas = new TTCanvas({
250
- dpr: dpr,
251
- domref,
252
- spec: this.props.spec,
253
- events: this.props.events,
254
- options: this.props.options,
255
- onChartInit: (chart) => {
256
- var _a;
257
- ((_a = this.props) === null || _a === void 0 ? void 0 : _a.onChartInit) && this.props.onChartInit(chart);
258
- },
259
- onChartReady: (chart) => {
260
- var _a;
261
- ((_a = this.props) === null || _a === void 0 ? void 0 : _a.onChartReady) && this.props.onChartReady(chart);
262
- },
263
- onChartUpdate: (chart) => {
264
- var _a;
265
- ((_a = this.props) === null || _a === void 0 ? void 0 : _a.onChartUpdate) && this.props.onChartUpdate(chart);
242
+ return __awaiter(this, void 0, void 0, function* () {
243
+ if (!domref) {
244
+ console.error(`未找到 #${this.props.canvasId} 组件`);
245
+ return;
266
246
  }
247
+ if (this.props.mode === 'wx') {
248
+ // 微信小程序环境特殊处理
249
+ const canvasIdLists = [
250
+ `${this.props.canvasId}_draw_canvas`,
251
+ `${this.props.canvasId}_tooltip_canvas`,
252
+ `${this.props.canvasId}_hidden_canvas`
253
+ ];
254
+ yield vglobal.setEnv('wx', { domref, force: true, canvasIdLists, freeCanvasIdx: 2, component: undefined });
255
+ }
256
+ domref.id = this.props.canvasId;
257
+ this.ttCanvas = new TTCanvas({
258
+ dpr: dpr,
259
+ domref,
260
+ spec: this.props.spec,
261
+ events: this.props.events,
262
+ options: this.props.options,
263
+ mode: this.props.mode,
264
+ onChartInit: (chart) => {
265
+ var _a;
266
+ ((_a = this.props) === null || _a === void 0 ? void 0 : _a.onChartInit) && this.props.onChartInit(chart);
267
+ },
268
+ onChartReady: (chart) => {
269
+ var _a;
270
+ ((_a = this.props) === null || _a === void 0 ? void 0 : _a.onChartReady) && this.props.onChartReady(chart);
271
+ },
272
+ onChartUpdate: (chart) => {
273
+ var _a;
274
+ ((_a = this.props) === null || _a === void 0 ? void 0 : _a.onChartUpdate) && this.props.onChartUpdate(chart);
275
+ }
276
+ });
267
277
  });
268
278
  }
269
279
  render() {
270
280
  const handleEvent = (event) => {
271
- const id = event.target.id.split('_')[0];
272
- if (id === this.props.canvasId && this.ttCanvas.chartInstance) {
281
+ if (this.ttCanvas && this.ttCanvas.chartInstance) {
273
282
  const chartInstance = this.ttCanvas.chartInstance;
274
283
  Object.defineProperty(event, 'target', {
275
284
  writable: false,
@@ -280,27 +289,33 @@ class GeneralChart extends React.Component {
280
289
  };
281
290
  const { canvasId, style = {} } = this.props;
282
291
  return (React.createElement(View, { key: canvasId, style: Object.assign(Object.assign(Object.assign({}, style_container), style), { padding: 0 }) },
283
- React.createElement(Canvas, { style: Object.assign({}, style_cs_tooltip_canvas), id: `${canvasId}_tooltip_canvas`, canvasId: `${canvasId}_tooltip_canvas` }),
284
- React.createElement(Canvas, { onTouchStart: handleEvent, onTouchMove: handleEvent, onTouchEnd: handleEvent, style: style_cs_canvas, id: `${canvasId}_draw_canvas`, canvasId: `${canvasId}_draw_canvas` }),
285
- React.createElement(Canvas, { style: Object.assign(Object.assign({}, style_cs_canvas), style_cs_canvas_hidden), id: `${canvasId}_hidden_canvas`, canvasId: `${canvasId}_hidden_canvas` })));
292
+ React.createElement(Canvas, { type: this.props.mode === 'wx' ? '2d' : undefined, style: Object.assign({}, style_cs_tooltip_canvas), id: `${canvasId}_tooltip_canvas`, canvasId: `${canvasId}_tooltip_canvas` }),
293
+ React.createElement(Canvas, { type: this.props.mode === 'wx' ? '2d' : undefined, onTouchStart: handleEvent, onTouchMove: handleEvent, onTouchEnd: handleEvent, style: style_cs_canvas, id: `${canvasId}_draw_canvas`, canvasId: `${canvasId}_draw_canvas` }),
294
+ React.createElement(Canvas, { type: this.props.mode === 'wx' ? '2d' : undefined, style: Object.assign(Object.assign({}, style_cs_canvas), style_cs_canvas_hidden), id: `${canvasId}_hidden_canvas`, canvasId: `${canvasId}_hidden_canvas` })));
286
295
  }
287
296
  }
288
297
 
289
298
  function VChart(_a) {
290
299
  var { type } = _a, args = __rest(_a, ["type"]);
291
- const env = type ? type.toLocaleLowerCase() : Taro.getEnv();
300
+ const env = (type !== null && type !== void 0 ? type : Taro.getEnv()).toLocaleLowerCase();
292
301
  const strategies = {
293
302
  lark: () => {
294
- return React.createElement(GeneralChart, Object.assign({}, args));
303
+ registerLarkEnv();
304
+ return React.createElement(GeneralChart, Object.assign({}, args, { mode: "miniApp" }));
295
305
  },
296
306
  tt: () => {
297
- return React.createElement(GeneralChart, Object.assign({}, args));
307
+ registerLarkEnv();
308
+ return React.createElement(GeneralChart, Object.assign({}, args, { mode: "miniApp" }));
309
+ },
310
+ weapp: () => {
311
+ registerWXEnv();
312
+ return React.createElement(GeneralChart, Object.assign({}, args, { mode: "wx" }));
298
313
  },
299
314
  web: () => {
300
315
  return React.createElement(WebChart, Object.assign({}, args));
301
316
  },
302
317
  h5: () => {
303
- return React.createElement(WebChart, Object.assign({}, args));
318
+ return React.createElement(WebChart, Object.assign({}, args, { mode: "mobile-browser" }));
304
319
  }
305
320
  };
306
321
  if (env && strategies[env] !== undefined) {
@@ -1,2 +1,2 @@
1
- import { IVChart, ISpec } from '@visactor/vchart';
2
- export { IVChart, ISpec };
1
+ import { IVChart, ISpec, RenderMode } from '@visactor/vchart';
2
+ export { IVChart, ISpec, RenderMode };
@@ -1,8 +1,10 @@
1
1
  type tt = 'tt';
2
2
  type lark = 'lark';
3
3
  type web = 'web' | 'h5';
4
+ type weapp = 'weapp';
4
5
  type TT = 'TT';
5
6
  type LARK = 'LARK';
6
7
  type WEB = 'WEB' | 'H5';
7
- export type VChartEnvType = tt | lark | web | TT | LARK | WEB;
8
+ type WEAPP = 'WEAPP';
9
+ export type VChartEnvType = tt | lark | weapp | web | TT | LARK | WEB | WEAPP;
8
10
  export {};
@@ -1,5 +1,5 @@
1
1
  export { IChartProps } from './IChartProps';
2
- export { IVChart } from './IVChart';
2
+ export { IVChart, RenderMode } from './IVChart';
3
3
  export { IEvent } from './IEvent';
4
4
  export { IOptions } from './IOptions';
5
5
  export { IDomRef } from './IDomRef';
package/package.json CHANGED
@@ -1,22 +1,12 @@
1
1
  {
2
2
  "name": "@visactor/taro-vchart",
3
- "version": "0.2.0-alpha.1",
3
+ "version": "0.10.0-alpha.2",
4
4
  "description": "Taro VChart 图表组件",
5
5
  "sideEffects": false,
6
- "type": "module",
7
6
  "main": "lib/src/index.js",
8
7
  "files": [
9
8
  "lib"
10
9
  ],
11
- "scripts": {
12
- "build": "rm -rf lib && rollup -c",
13
- "build:tt": "taro build --type tt",
14
- "build:lark": "taro build --type lark",
15
- "build:h5": "taro build --type h5",
16
- "dev:tt": "npm run build:tt -- --watch",
17
- "dev:lark": "npm run build:lark -- --watch",
18
- "dev:h5": "npm run build:h5 -- --watch"
19
- },
20
10
  "keywords": [
21
11
  "taro",
22
12
  "charts",
@@ -28,7 +18,7 @@
28
18
  "graphics",
29
19
  "interaction"
30
20
  ],
31
- "homepage": "",
21
+ "homepage": "https://www.visactor.io/vchart",
32
22
  "bugs": "https://github.com/VisActor/VChart/issues",
33
23
  "repository": {
34
24
  "type": "git",
@@ -37,16 +27,13 @@
37
27
  },
38
28
  "author": {
39
29
  "name": "VisActor",
40
- "url": "https://VisActor.io/"
30
+ "url": "https://www.visactor.io/"
41
31
  },
42
32
  "license": "MIT",
43
33
  "dependencies": {
44
- "@visactor/vchart": "workspace:1.1.0-beta.1"
34
+ "@visactor/vchart": "0.10.0-alpha.2"
45
35
  },
46
36
  "devDependencies": {
47
- "@internal/bundler": "workspace:*",
48
- "@internal/eslint-config": "workspace:*",
49
- "@internal/ts-config": "workspace:*",
50
37
  "@rushstack/eslint-patch": "~1.1.4",
51
38
  "@vitejs/plugin-react": "3.1.0",
52
39
  "eslint": "~8.18.0",
@@ -83,10 +70,23 @@
83
70
  "rollup": "3.20.5",
84
71
  "rollup-plugin-import-css": "^3.0.2",
85
72
  "stylelint": "9.3.0",
86
- "tslib": "2.3.1"
73
+ "tslib": "2.3.1",
74
+ "@internal/eslint-config": "0.0.1",
75
+ "@internal/ts-config": "0.0.1"
87
76
  },
88
77
  "publishConfig": {
89
78
  "access": "public",
90
79
  "registry": "https://registry.npmjs.org/"
80
+ },
81
+ "scripts": {
82
+ "build": "rm -rf lib && rollup -c --bundleConfigAsCjs",
83
+ "build:tt": "taro build --type tt",
84
+ "build:lark": "taro build --type lark",
85
+ "build:h5": "taro build --type h5",
86
+ "build:wx": "taro build --type weapp",
87
+ "dev:tt": "npm run build:tt -- --watch",
88
+ "dev:lark": "npm run build:lark -- --watch",
89
+ "dev:h5": "npm run build:h5 -- --watch",
90
+ "dev:wx": "npm run build:wx -- --watch"
91
91
  }
92
- }
92
+ }