|
1 | 1 | import { mark } from '../../adaptor'; |
2 | 2 | import type { Adaptor } from '../../types'; |
3 | | -import { flow, get, isArray, set, transformOptions } from '../../utils'; |
| 3 | +import { flow, get, isArray, set, dataTransform, transformOptions } from '../../utils'; |
4 | 4 | import type { SankeyOptions } from './type'; |
5 | 5 |
|
6 | 6 | type Params = Adaptor<SankeyOptions>; |
7 | 7 |
|
| 8 | +const defaultTransform = (params: Params) => { |
| 9 | + const { options } = params; |
| 10 | + const { data } = options; |
| 11 | + const transformLinks = [ |
| 12 | + { |
| 13 | + type: 'custom', |
| 14 | + callback: (datum) => ({ links: datum }), |
| 15 | + }, |
| 16 | + ]; |
| 17 | + if (isArray(data)) { |
| 18 | + if (data.length > 0) { |
| 19 | + set(options, 'data', { |
| 20 | + value: data, |
| 21 | + transform: transformLinks, |
| 22 | + }); |
| 23 | + } else { |
| 24 | + delete options.children; |
| 25 | + } |
| 26 | + } else if (get(data, 'type') === 'fetch' && get(data, 'value')) { |
| 27 | + const transform = get(data, 'transform'); |
| 28 | + if (!isArray(transform)) { |
| 29 | + set(data, 'transform', transformLinks); |
| 30 | + } |
| 31 | + } |
| 32 | + return params; |
| 33 | +}; |
| 34 | + |
8 | 35 | /** |
9 | 36 | * @param chart |
10 | 37 | * @param options |
11 | 38 | */ |
12 | 39 | export function adaptor(params: Params) { |
13 | | - const dataTransform = (params: Params) => { |
14 | | - const { options } = params; |
15 | | - const { data } = options; |
16 | | - const defaultTransform = [ |
17 | | - { |
18 | | - type: 'custom', |
19 | | - callback: (datum) => ({ links: datum }), |
20 | | - }, |
21 | | - ]; |
22 | | - if (isArray(data)) { |
23 | | - if (data.length > 0) { |
24 | | - set(options, 'data', { |
25 | | - value: data, |
26 | | - transform: defaultTransform, |
27 | | - }); |
28 | | - } else { |
29 | | - delete options.children; |
30 | | - } |
31 | | - } else if (get(data, 'type') === 'fetch' && get(data, 'value')) { |
32 | | - const transform = get(data, 'transform'); |
33 | | - if (isArray(transform)) { |
34 | | - set(data, 'transform', transform.concat(defaultTransform)); |
35 | | - } else { |
36 | | - set(data, 'transform', defaultTransform); |
37 | | - } |
38 | | - } |
39 | | - return params; |
40 | | - }; |
41 | | - return flow(dataTransform, mark, transformOptions)(params); |
| 40 | + |
| 41 | + return flow(dataTransform, defaultTransform, mark, transformOptions)(params); |
42 | 42 | } |
0 commit comments