3
13693261870
2022-09-16 63ba114e70e380442fcbed4a5157ee52c9491216
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
 
import SeriesModel from '../../model/Series';
import Tree from '../../data/Tree';
import {
    SeriesOption,
    SymbolOptionMixin,
    BoxLayoutOptionMixin,
    RoamOptionMixin,
    LineStyleOption,
    ItemStyleOption,
    SeriesLabelOption,
    OptionDataValue,
    StatesOptionMixin,
    OptionDataItemObject,
    CallbackDataParams,
    DefaultEmphasisFocus
} from '../../util/types';
import List from '../../data/List';
import View from '../../coord/View';
import { LayoutRect } from '../../util/layout';
import Model from '../../model/Model';
import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup';
 
interface CurveLineStyleOption extends LineStyleOption{
    curveness?: number
}
 
export interface TreeSeriesStateOption {
    itemStyle?: ItemStyleOption
    /**
     * Line style of the edge between node and it's parent.
     */
    lineStyle?: CurveLineStyleOption
    label?: SeriesLabelOption
}
 
interface ExtraStateOption {
    emphasis?: {
        focus?: DefaultEmphasisFocus | 'ancestor' | 'descendant'
        scale?: boolean
    }
}
 
export interface TreeSeriesNodeItemOption extends SymbolOptionMixin<CallbackDataParams>,
    TreeSeriesStateOption, StatesOptionMixin<TreeSeriesStateOption, ExtraStateOption>,
    OptionDataItemObject<OptionDataValue> {
 
    children?: TreeSeriesNodeItemOption[]
 
    collapsed?: boolean
 
    link?: string
    target?: string
}
 
/**
 * Configuration of leaves nodes.
 */
export interface TreeSeriesLeavesOption extends TreeSeriesStateOption, StatesOptionMixin<TreeSeriesStateOption> {
 
}
 
export interface TreeSeriesOption extends
    SeriesOption<TreeSeriesStateOption, ExtraStateOption>, TreeSeriesStateOption,
    SymbolOptionMixin, BoxLayoutOptionMixin, RoamOptionMixin {
    type?: 'tree'
 
    layout?: 'orthogonal' | 'radial'
 
    edgeShape?: 'polyline' | 'curve'
 
    /**
     * Available when edgeShape is polyline
     */
    edgeForkPosition?: string | number
 
    nodeScaleRatio?: number
 
    /**
     * The orient of orthoginal layout, can be setted to 'LR', 'TB', 'RL', 'BT'.
     * and the backward compatibility configuration 'horizontal = LR', 'vertical = TB'.
     */
    orient?: 'LR' | 'TB' | 'RL' | 'BT' | 'horizontal' | 'vertical'
 
    expandAndCollapse?: boolean
 
    /**
     * The initial expanded depth of tree
     */
    initialTreeDepth?: number
 
    leaves?: TreeSeriesLeavesOption
 
    data?: TreeSeriesNodeItemOption[]
}
 
class TreeSeriesModel extends SeriesModel<TreeSeriesOption> {
    static readonly type = 'series.tree';
 
    // can support the position parameters 'left', 'top','right','bottom', 'width',
    // 'height' in the setOption() with 'merge' mode normal.
    static readonly layoutMode = 'box';
 
    coordinateSystem: View;
 
    layoutInfo: LayoutRect;
 
    hasSymbolVisual = true;
 
    // Do it self.
    ignoreStyleOnData = true;
 
    /**
     * Init a tree data structure from data in option series
     * @param  option  the object used to config echarts view
     * @return storage initial data
     */
    getInitialData(option: TreeSeriesOption): List {
 
        //create an virtual root
        const root: TreeSeriesNodeItemOption = {
            name: option.name,
            children: option.data
        };
 
        const leaves = option.leaves || {};
        const leavesModel = new Model(leaves, this, this.ecModel);
 
        const tree = Tree.createTree(root, this, beforeLink);
 
        function beforeLink(nodeData: List) {
            nodeData.wrapMethod('getItemModel', function (model, idx) {
                const node = tree.getNodeByDataIndex(idx);
                if (!node.children.length || !node.isExpand) {
                    model.parentModel = leavesModel;
                }
                return model;
            });
        }
 
        let treeDepth = 0;
 
        tree.eachNode('preorder', function (node) {
            if (node.depth > treeDepth) {
                treeDepth = node.depth;
            }
        });
 
        const expandAndCollapse = option.expandAndCollapse;
        const expandTreeDepth = (expandAndCollapse && option.initialTreeDepth >= 0)
            ? option.initialTreeDepth : treeDepth;
 
        tree.root.eachNode('preorder', function (node) {
            const item = node.hostTree.data.getRawDataItem(node.dataIndex) as TreeSeriesNodeItemOption;
            // Add item.collapsed != null, because users can collapse node original in the series.data.
            node.isExpand = (item && item.collapsed != null)
                ? !item.collapsed
                : node.depth <= expandTreeDepth;
        });
 
        return tree.data;
    }
 
    /**
     * Make the configuration 'orient' backward compatibly, with 'horizontal = LR', 'vertical = TB'.
     * @returns {string} orient
     */
    getOrient() {
        let orient = this.get('orient');
        if (orient === 'horizontal') {
            orient = 'LR';
        }
        else if (orient === 'vertical') {
            orient = 'TB';
        }
        return orient;
    }
 
    setZoom(zoom: number) {
        this.option.zoom = zoom;
    }
 
    setCenter(center: number[]) {
        this.option.center = center;
    }
 
    formatTooltip(
        dataIndex: number,
        multipleSeries: boolean,
        dataType: string
    ) {
        const tree = this.getData().tree;
        const realRoot = tree.root.children[0];
        let node = tree.getNodeByDataIndex(dataIndex);
        const value = node.getValue();
        let name = node.name;
        while (node && (node !== realRoot)) {
            name = node.parentNode.name + '.' + name;
            node = node.parentNode;
        }
 
        return createTooltipMarkup('nameValue', {
            name: name,
            value: value,
            noValue: isNaN(value as number) || value == null
        });
    }
 
    static defaultOption: TreeSeriesOption = {
        zlevel: 0,
        z: 2,
        coordinateSystem: 'view',
 
        // the position of the whole view
        left: '12%',
        top: '12%',
        right: '12%',
        bottom: '12%',
 
        // the layout of the tree, two value can be selected, 'orthogonal' or 'radial'
        layout: 'orthogonal',
 
        // value can be 'polyline'
        edgeShape: 'curve',
 
        edgeForkPosition: '50%',
 
        // true | false | 'move' | 'scale', see module:component/helper/RoamController.
        roam: false,
 
        // Symbol size scale ratio in roam
        nodeScaleRatio: 0.4,
 
        // Default on center of graph
        center: null,
 
        zoom: 1,
 
        orient: 'LR',
 
        symbol: 'emptyCircle',
 
        symbolSize: 7,
 
        expandAndCollapse: true,
 
        initialTreeDepth: 2,
 
        lineStyle: {
            color: '#ccc',
            width: 1.5,
            curveness: 0.5
        },
 
        itemStyle: {
            color: 'lightsteelblue',
            // borderColor: '#c23531',
            borderWidth: 1.5
        },
 
        label: {
            show: true
        },
 
        animationEasing: 'linear',
 
        animationDuration: 700,
 
        animationDurationUpdate: 500
    };
}
 
export default TreeSeriesModel;