~irvinamaya/sged/sged

« back to all changes in this revision

Viewing changes to SGED_Project/templates/echarts-5.4.2/src/chart/sunburst/SunburstSeries.ts

  • Committer: Irvin
  • Date: 2023-05-31 21:31:01 UTC
  • Revision ID: fa20014@ues.edu.sv-20230531213101-lg41sz0rqtvbejm2
Proyecto Finalizado

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* Licensed to the Apache Software Foundation (ASF) under one
3
 
* or more contributor license agreements.  See the NOTICE file
4
 
* distributed with this work for additional information
5
 
* regarding copyright ownership.  The ASF licenses this file
6
 
* to you under the Apache License, Version 2.0 (the
7
 
* "License"); you may not use this file except in compliance
8
 
* with the License.  You may obtain a copy of the License at
9
 
*
10
 
*   http://www.apache.org/licenses/LICENSE-2.0
11
 
*
12
 
* Unless required by applicable law or agreed to in writing,
13
 
* software distributed under the License is distributed on an
14
 
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 
* KIND, either express or implied.  See the License for the
16
 
* specific language governing permissions and limitations
17
 
* under the License.
18
 
*/
19
 
 
20
 
import * as zrUtil from 'zrender/src/core/util';
21
 
import SeriesModel from '../../model/Series';
22
 
import Tree, { TreeNode } from '../../data/Tree';
23
 
import {wrapTreePathInfo} from '../helper/treeHelper';
24
 
import {
25
 
    SeriesOption,
26
 
    CircleLayoutOptionMixin,
27
 
    SeriesLabelOption,
28
 
    ItemStyleOption,
29
 
    OptionDataValue,
30
 
    CallbackDataParams,
31
 
    StatesOptionMixin,
32
 
    OptionDataItemObject,
33
 
    DefaultEmphasisFocus,
34
 
    SunburstColorByMixin
35
 
} from '../../util/types';
36
 
import GlobalModel from '../../model/Global';
37
 
import SeriesData from '../../data/SeriesData';
38
 
import Model from '../../model/Model';
39
 
import enableAriaDecalForTree from '../helper/enableAriaDecalForTree';
40
 
 
41
 
interface SunburstItemStyleOption<TCbParams = never> extends ItemStyleOption<TCbParams> {
42
 
    // can be 10
43
 
    // which means that both innerCornerRadius and outerCornerRadius are 10
44
 
    // can also be an array [20, 10]
45
 
    // which means that innerCornerRadius is 20
46
 
    // and outerCornerRadius is 10
47
 
    // can also be a string or string array, such as ['20%', '50%']
48
 
    // which means that innerCornerRadius is 20% of the innerRadius
49
 
    // and outerCornerRadius is half of outerRadius.
50
 
    borderRadius?: (number | string)[] | number | string
51
 
}
52
 
 
53
 
interface SunburstLabelOption extends Omit<SeriesLabelOption<SunburstDataParams>, 'rotate' | 'position'> {
54
 
    rotate?: 'radial' | 'tangential' | number
55
 
    minAngle?: number
56
 
    silent?: boolean
57
 
    position?: SeriesLabelOption['position'] | 'outside'
58
 
}
59
 
 
60
 
interface SunburstDataParams extends CallbackDataParams {
61
 
    treePathInfo: {
62
 
        name: string,
63
 
        dataIndex: number
64
 
        value: SunburstSeriesNodeItemOption['value']
65
 
    }[]
66
 
}
67
 
 
68
 
interface SunburstStatesMixin {
69
 
    emphasis?: {
70
 
        focus?: DefaultEmphasisFocus | 'descendant' | 'ancestor'
71
 
    }
72
 
}
73
 
 
74
 
export interface SunburstStateOption<TCbParams = never> {
75
 
    itemStyle?: SunburstItemStyleOption<TCbParams>
76
 
    label?: SunburstLabelOption
77
 
}
78
 
 
79
 
export interface SunburstSeriesNodeItemOption extends
80
 
    SunburstStateOption<SunburstDataParams>,
81
 
    StatesOptionMixin<SunburstStateOption<SunburstDataParams>, SunburstStatesMixin>,
82
 
    OptionDataItemObject<OptionDataValue>
83
 
{
84
 
    nodeClick?: 'rootToNode' | 'link' | false
85
 
    // Available when nodeClick is link
86
 
    link?: string
87
 
    target?: string
88
 
 
89
 
    children?: SunburstSeriesNodeItemOption[]
90
 
 
91
 
    collapsed?: boolean
92
 
 
93
 
    cursor?: string
94
 
}
95
 
export interface SunburstSeriesLevelOption extends
96
 
    SunburstStateOption<SunburstDataParams>,
97
 
    StatesOptionMixin<SunburstStateOption<SunburstDataParams>, SunburstStatesMixin> {
98
 
 
99
 
    radius?: (number | string)[]
100
 
    /**
101
 
     * @deprecated use radius instead
102
 
     */
103
 
    r?: number | string
104
 
    /**
105
 
     * @deprecated use radius instead
106
 
     */
107
 
    r0?: number | string
108
 
 
109
 
    highlight?: {
110
 
        itemStyle?: SunburstItemStyleOption
111
 
        label?: SunburstLabelOption
112
 
    }
113
 
}
114
 
 
115
 
interface SortParam {
116
 
    dataIndex: number
117
 
    depth: number
118
 
    height: number
119
 
    getValue(): number
120
 
}
121
 
export interface SunburstSeriesOption extends
122
 
    SeriesOption<SunburstStateOption<SunburstDataParams>, SunburstStatesMixin>,
123
 
    SunburstStateOption<SunburstDataParams>,
124
 
    SunburstColorByMixin,
125
 
    CircleLayoutOptionMixin {
126
 
 
127
 
    type?: 'sunburst'
128
 
 
129
 
    clockwise?: boolean
130
 
    startAngle?: number
131
 
    minAngle?: number
132
 
    /**
133
 
     * If still show when all data zero.
134
 
     */
135
 
    stillShowZeroSum?: boolean
136
 
    /**
137
 
     * Policy of highlighting pieces when hover on one
138
 
     * Valid values: 'none' (for not downplay others), 'descendant',
139
 
     * 'ancestor', 'self'
140
 
     */
141
 
    // highlightPolicy?: 'descendant' | 'ancestor' | 'self'
142
 
 
143
 
    nodeClick?: 'rootToNode' | 'link' | false
144
 
 
145
 
    renderLabelForZeroData?: boolean
146
 
 
147
 
    data?: SunburstSeriesNodeItemOption[]
148
 
 
149
 
    levels?: SunburstSeriesLevelOption[]
150
 
 
151
 
    animationType?: 'expansion' | 'scale'
152
 
 
153
 
    sort?: 'desc' | 'asc' | ((a: SortParam, b: SortParam) => number)
154
 
}
155
 
 
156
 
interface SunburstSeriesModel {
157
 
    getFormattedLabel(
158
 
        dataIndex: number,
159
 
        state?: 'emphasis' | 'normal' | 'highlight' | 'blur' | 'select'
160
 
    ): string
161
 
}
162
 
class SunburstSeriesModel extends SeriesModel<SunburstSeriesOption> {
163
 
 
164
 
    static readonly type = 'series.sunburst';
165
 
    readonly type = SunburstSeriesModel.type;
166
 
 
167
 
    ignoreStyleOnData = true;
168
 
 
169
 
    private _viewRoot: TreeNode;
170
 
    private _levelModels: Model<SunburstSeriesLevelOption>[];
171
 
 
172
 
    getInitialData(option: SunburstSeriesOption, ecModel: GlobalModel) {
173
 
        // Create a virtual root.
174
 
        const root = { name: option.name, children: option.data } as SunburstSeriesNodeItemOption;
175
 
 
176
 
        completeTreeValue(root);
177
 
 
178
 
        const levelModels = this._levelModels =
179
 
            zrUtil.map(option.levels || [], function (levelDefine) {
180
 
                return new Model(levelDefine, this, ecModel);
181
 
            }, this);
182
 
 
183
 
        // Make sure always a new tree is created when setOption,
184
 
        // in TreemapView, we check whether oldTree === newTree
185
 
        // to choose mappings approach among old shapes and new shapes.
186
 
        const tree = Tree.createTree(root, this, beforeLink);
187
 
 
188
 
        function beforeLink(nodeData: SeriesData) {
189
 
            nodeData.wrapMethod('getItemModel', function (model, idx) {
190
 
                const node = tree.getNodeByDataIndex(idx);
191
 
                const levelModel = levelModels[node.depth];
192
 
                levelModel && (model.parentModel = levelModel);
193
 
                return model;
194
 
            });
195
 
        }
196
 
        return tree.data;
197
 
    }
198
 
 
199
 
    optionUpdated() {
200
 
        this.resetViewRoot();
201
 
    }
202
 
 
203
 
    /*
204
 
     * @override
205
 
     */
206
 
    getDataParams(dataIndex: number) {
207
 
        const params = super.getDataParams.apply(this, arguments as any) as SunburstDataParams;
208
 
 
209
 
        const node = this.getData().tree.getNodeByDataIndex(dataIndex);
210
 
        params.treePathInfo = wrapTreePathInfo<SunburstSeriesNodeItemOption['value']>(node, this);
211
 
 
212
 
        return params;
213
 
    }
214
 
 
215
 
    getLevelModel(node: TreeNode) {
216
 
        return this._levelModels && this._levelModels[node.depth];
217
 
    }
218
 
 
219
 
    static defaultOption: SunburstSeriesOption = {
220
 
        // zlevel: 0,
221
 
        z: 2,
222
 
 
223
 
        // 默认全局居中
224
 
        center: ['50%', '50%'],
225
 
        radius: [0, '75%'],
226
 
        // 默认顺时针
227
 
        clockwise: true,
228
 
        startAngle: 90,
229
 
        // 最小角度改为0
230
 
        minAngle: 0,
231
 
 
232
 
        // If still show when all data zero.
233
 
        stillShowZeroSum: true,
234
 
 
235
 
        // 'rootToNode', 'link', or false
236
 
        nodeClick: 'rootToNode',
237
 
 
238
 
        renderLabelForZeroData: false,
239
 
 
240
 
        label: {
241
 
            // could be: 'radial', 'tangential', or 'none'
242
 
            rotate: 'radial',
243
 
            show: true,
244
 
            opacity: 1,
245
 
            // 'left' is for inner side of inside, and 'right' is for outer
246
 
            // side for inside
247
 
            align: 'center',
248
 
            position: 'inside',
249
 
            distance: 5,
250
 
            silent: true
251
 
        },
252
 
        itemStyle: {
253
 
            borderWidth: 1,
254
 
            borderColor: 'white',
255
 
            borderType: 'solid',
256
 
            shadowBlur: 0,
257
 
            shadowColor: 'rgba(0, 0, 0, 0.2)',
258
 
            shadowOffsetX: 0,
259
 
            shadowOffsetY: 0,
260
 
            opacity: 1
261
 
        },
262
 
 
263
 
        emphasis: {
264
 
            focus: 'descendant'
265
 
        },
266
 
 
267
 
        blur: {
268
 
            itemStyle: {
269
 
                opacity: 0.2
270
 
            },
271
 
            label: {
272
 
                opacity: 0.1
273
 
            }
274
 
        },
275
 
 
276
 
        // Animation type can be expansion, scale.
277
 
        animationType: 'expansion',
278
 
        animationDuration: 1000,
279
 
        animationDurationUpdate: 500,
280
 
 
281
 
        data: [],
282
 
 
283
 
        /**
284
 
         * Sort order.
285
 
         *
286
 
         * Valid values: 'desc', 'asc', null, or callback function.
287
 
         * 'desc' and 'asc' for descend and ascendant order;
288
 
         * null for not sorting;
289
 
         * example of callback function:
290
 
         * function(nodeA, nodeB) {
291
 
         *     return nodeA.getValue() - nodeB.getValue();
292
 
         * }
293
 
         */
294
 
        sort: 'desc'
295
 
    };
296
 
 
297
 
    getViewRoot() {
298
 
        return this._viewRoot;
299
 
    }
300
 
 
301
 
    resetViewRoot(viewRoot?: TreeNode) {
302
 
        viewRoot
303
 
            ? (this._viewRoot = viewRoot)
304
 
            : (viewRoot = this._viewRoot);
305
 
 
306
 
        const root = this.getRawData().tree.root;
307
 
 
308
 
        if (!viewRoot
309
 
            || (viewRoot !== root && !root.contains(viewRoot))
310
 
        ) {
311
 
            this._viewRoot = root;
312
 
        }
313
 
    }
314
 
 
315
 
    enableAriaDecal() {
316
 
        enableAriaDecalForTree(this);
317
 
    }
318
 
}
319
 
 
320
 
 
321
 
 
322
 
function completeTreeValue(dataNode: SunburstSeriesNodeItemOption) {
323
 
    // Postorder travel tree.
324
 
    // If value of none-leaf node is not set,
325
 
    // calculate it by suming up the value of all children.
326
 
    let sum = 0;
327
 
 
328
 
    zrUtil.each(dataNode.children, function (child) {
329
 
 
330
 
        completeTreeValue(child);
331
 
 
332
 
        let childValue = child.value;
333
 
        // TODO First value of array must be a number
334
 
        zrUtil.isArray(childValue) && (childValue = childValue[0]);
335
 
        sum += childValue as number;
336
 
    });
337
 
 
338
 
    let thisValue = dataNode.value as number;
339
 
    if (zrUtil.isArray(thisValue)) {
340
 
        thisValue = thisValue[0];
341
 
    }
342
 
 
343
 
    if (thisValue == null || isNaN(thisValue)) {
344
 
        thisValue = sum;
345
 
    }
346
 
    // Value should not less than 0.
347
 
    if (thisValue < 0) {
348
 
        thisValue = 0;
349
 
    }
350
 
 
351
 
    zrUtil.isArray(dataNode.value)
352
 
        ? (dataNode.value[0] = thisValue)
353
 
        : (dataNode.value = thisValue);
354
 
}
355
 
 
356
 
 
357
 
export default SunburstSeriesModel;