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
/*
* 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 * as graphic from '../../util/graphic';
import { setStatesStylesFromModel, enableHoverEmphasis } from '../../util/states';
import ChartView from '../../view/Chart';
import List from '../../data/List';
import ParallelSeriesModel, { ParallelSeriesDataItemOption } from './ParallelSeries';
import GlobalModel from '../../model/Global';
import ExtensionAPI from '../../core/ExtensionAPI';
import { StageHandlerProgressParams, ParsedValue, Payload } from '../../util/types';
import Parallel from '../../coord/parallel/Parallel';
import { OptionAxisType } from '../../coord/axisCommonTypes';
import { numericToNumber } from '../../util/number';
import { eqNaN } from 'zrender/src/core/util';
 
const DEFAULT_SMOOTH = 0.3;
 
interface ParallelDrawSeriesScope {
    smooth: number
}
class ParallelView extends ChartView {
    static type = 'parallel';
    type = ParallelView.type;
 
    private _dataGroup = new graphic.Group();
 
    private _data: List;
 
    private _initialized = false;
 
    init() {
        this.group.add(this._dataGroup);
    }
 
    /**
     * @override
     */
    render(
        seriesModel: ParallelSeriesModel,
        ecModel: GlobalModel,
        api: ExtensionAPI,
        payload: Payload
    ) {
        const dataGroup = this._dataGroup;
        const data = seriesModel.getData();
        const oldData = this._data;
        const coordSys = seriesModel.coordinateSystem;
        const dimensions = coordSys.dimensions;
        const seriesScope = makeSeriesScope(seriesModel);
 
        data.diff(oldData)
            .add(add)
            .update(update)
            .remove(remove)
            .execute();
 
        function add(newDataIndex: number) {
            const line = addEl(data, dataGroup, newDataIndex, dimensions, coordSys);
            updateElCommon(line, data, newDataIndex, seriesScope);
        }
 
        function update(newDataIndex: number, oldDataIndex: number) {
            const line = oldData.getItemGraphicEl(oldDataIndex) as graphic.Polyline;
 
            const points = createLinePoints(data, newDataIndex, dimensions, coordSys);
            data.setItemGraphicEl(newDataIndex, line);
 
            graphic.updateProps(line, {shape: {points: points}}, seriesModel, newDataIndex);
 
            updateElCommon(line, data, newDataIndex, seriesScope);
        }
 
        function remove(oldDataIndex: number) {
            const line = oldData.getItemGraphicEl(oldDataIndex);
            dataGroup.remove(line);
        }
 
        // First create
        if (!this._initialized) {
            this._initialized = true;
            const clipPath = createGridClipShape(
                coordSys, seriesModel, function () {
                    // Callback will be invoked immediately if there is no animation
                    setTimeout(function () {
                        dataGroup.removeClipPath();
                    });
                }
            );
            dataGroup.setClipPath(clipPath);
        }
 
        this._data = data;
    }
 
    incrementalPrepareRender(seriesModel: ParallelSeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
        this._initialized = true;
        this._data = null;
        this._dataGroup.removeAll();
    }
 
    incrementalRender(taskParams: StageHandlerProgressParams, seriesModel: ParallelSeriesModel, ecModel: GlobalModel) {
        const data = seriesModel.getData();
        const coordSys = seriesModel.coordinateSystem;
        const dimensions = coordSys.dimensions;
        const seriesScope = makeSeriesScope(seriesModel);
 
        for (let dataIndex = taskParams.start; dataIndex < taskParams.end; dataIndex++) {
            const line = addEl(data, this._dataGroup, dataIndex, dimensions, coordSys);
            line.incremental = true;
            updateElCommon(line, data, dataIndex, seriesScope);
        }
    }
 
    remove() {
        this._dataGroup && this._dataGroup.removeAll();
        this._data = null;
    }
}
 
function createGridClipShape(coordSys: Parallel, seriesModel: ParallelSeriesModel, cb: () => void) {
    const parallelModel = coordSys.model;
    const rect = coordSys.getRect();
    const rectEl = new graphic.Rect({
        shape: {
            x: rect.x,
            y: rect.y,
            width: rect.width,
            height: rect.height
        }
    });
 
    const dim = parallelModel.get('layout') === 'horizontal' ? 'width' as const : 'height' as const;
    rectEl.setShape(dim, 0);
    graphic.initProps(rectEl, {
        shape: {
            width: rect.width,
            height: rect.height
        }
    }, seriesModel, cb);
    return rectEl;
}
 
function createLinePoints(data: List, dataIndex: number, dimensions: string[], coordSys: Parallel) {
    const points = [];
    for (let i = 0; i < dimensions.length; i++) {
        const dimName = dimensions[i];
        const value = data.get(data.mapDimension(dimName), dataIndex);
        if (!isEmptyValue(value, coordSys.getAxis(dimName).type)) {
            points.push(coordSys.dataToPoint(value, dimName));
        }
    }
    return points;
}
 
function addEl(data: List, dataGroup: graphic.Group, dataIndex: number, dimensions: string[], coordSys: Parallel) {
    const points = createLinePoints(data, dataIndex, dimensions, coordSys);
    const line = new graphic.Polyline({
        shape: {points: points},
        // silent: true,
        z2: 10
    });
    dataGroup.add(line);
    data.setItemGraphicEl(dataIndex, line);
    return line;
}
 
function makeSeriesScope(seriesModel: ParallelSeriesModel): ParallelDrawSeriesScope {
    let smooth = seriesModel.get('smooth', true);
    smooth === true && (smooth = DEFAULT_SMOOTH);
    smooth = numericToNumber(smooth);
    eqNaN(smooth) && (smooth = 0);
 
    return { smooth };
}
 
function updateElCommon(
    el: graphic.Polyline,
    data: List,
    dataIndex: number,
    seriesScope: ParallelDrawSeriesScope
) {
    el.useStyle(data.getItemVisual(dataIndex, 'style'));
    el.style.fill = null;
    el.setShape('smooth', seriesScope.smooth);
 
    const itemModel = data.getItemModel<ParallelSeriesDataItemOption>(dataIndex);
    const emphasisModel = itemModel.getModel('emphasis');
    setStatesStylesFromModel(el, itemModel, 'lineStyle');
 
    enableHoverEmphasis(el, emphasisModel.get('focus'), emphasisModel.get('blurScope'));
}
 
// function simpleDiff(oldData, newData, dimensions) {
//     let oldLen;
//     if (!oldData
//         || !oldData.__plProgressive
//         || (oldLen = oldData.count()) !== newData.count()
//     ) {
//         return true;
//     }
 
//     let dimLen = dimensions.length;
//     for (let i = 0; i < oldLen; i++) {
//         for (let j = 0; j < dimLen; j++) {
//             if (oldData.get(dimensions[j], i) !== newData.get(dimensions[j], i)) {
//                 return true;
//             }
//         }
//     }
 
//     return false;
// }
 
// FIXME put in common util?
function isEmptyValue(val: ParsedValue, axisType: OptionAxisType) {
    return axisType === 'category'
        ? val == null
        : (val == null || isNaN(val as number)); // axisType === 'value'
}
 
export default ParallelView;