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
/*
* 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 echarts from '../../core/echarts';
import { createHashMap, each, HashMap, hasOwn, keys, map } from 'zrender/src/core/util';
import SeriesModel from '../../model/Series';
import { isCartesian2DSeries, findAxisModels } from './cartesianAxisHelper';
import { getDataDimensionsOnAxis, unionAxisExtentFromData } from '../axisHelper';
import { AxisBaseModel } from '../AxisBaseModel';
import Axis from '../Axis';
import GlobalModel from '../../model/Global';
import { Dictionary } from '../../util/types';
import { ScaleRawExtentInfo, ScaleRawExtentResult, ensureScaleRawExtentInfo } from '../scaleRawExtentInfo';
 
 
type AxisRecord = {
    condExtent: number[];
    rawExtentInfo?: ScaleRawExtentInfo;
    rawExtentResult?: ScaleRawExtentResult
    tarExtent?: number[];
};
 
type SeriesRecord = {
    seriesModel: SeriesModel;
    xAxisModel: AxisBaseModel;
    yAxisModel: AxisBaseModel;
};
 
// A tricky: the priority is just after dataZoom processor.
// If dataZoom has fixed the min/max, this processor do not need to work.
// TODO: SELF REGISTERED.
echarts.registerProcessor(echarts.PRIORITY.PROCESSOR.FILTER + 10, {
 
    getTargetSeries: function (ecModel) {
        const seriesModelMap = createHashMap<SeriesModel>();
        ecModel.eachSeries(function (seriesModel: SeriesModel) {
            isCartesian2DSeries(seriesModel) && seriesModelMap.set(seriesModel.uid, seriesModel);
        });
        return seriesModelMap;
    },
 
    overallReset: function (ecModel, api) {
        const seriesRecords = [] as SeriesRecord[];
        const axisRecordMap = createHashMap<AxisRecord>();
 
        prepareDataExtentOnAxis(ecModel, axisRecordMap, seriesRecords);
        calculateFilteredExtent(axisRecordMap, seriesRecords);
        shrinkAxisExtent(axisRecordMap);
    }
});
 
function prepareDataExtentOnAxis(
    ecModel: GlobalModel,
    axisRecordMap: HashMap<AxisRecord>,
    seriesRecords: SeriesRecord[]
): void {
    ecModel.eachSeries(function (seriesModel: SeriesModel) {
        if (!isCartesian2DSeries(seriesModel)) {
            return;
        }
 
        const axesModelMap = findAxisModels(seriesModel);
        const xAxisModel = axesModelMap.xAxisModel;
        const yAxisModel = axesModelMap.yAxisModel;
        const xAxis = xAxisModel.axis;
        const yAxis = yAxisModel.axis;
        const xRawExtentInfo = xAxis.scale.rawExtentInfo;
        const yRawExtentInfo = yAxis.scale.rawExtentInfo;
        const data = seriesModel.getData();
 
        // If either axis controlled by other filter like "dataZoom",
        // use the rule of dataZoom rather than adopting the rules here.
        if (
            (xRawExtentInfo && xRawExtentInfo.frozen)
            || (yRawExtentInfo && yRawExtentInfo.frozen)
        ) {
            return;
        }
 
        seriesRecords.push({
            seriesModel: seriesModel,
            xAxisModel: xAxisModel,
            yAxisModel: yAxisModel
        });
 
        // FIXME: this logic needs to be consistent with
        // `coord/cartesian/Grid.ts#_updateScale`.
        // It's not good to implement one logic in multiple places.
        unionAxisExtentFromData(prepareAxisRecord(axisRecordMap, xAxisModel).condExtent, data, xAxis.dim);
        unionAxisExtentFromData(prepareAxisRecord(axisRecordMap, yAxisModel).condExtent, data, yAxis.dim);
    });
}
 
function calculateFilteredExtent(
    axisRecordMap: HashMap<AxisRecord>,
    seriesRecords: SeriesRecord[]
) {
    each(seriesRecords, function (seriesRecord) {
        const xAxisModel = seriesRecord.xAxisModel;
        const yAxisModel = seriesRecord.yAxisModel;
        const xAxis = xAxisModel.axis;
        const yAxis = yAxisModel.axis;
        const xAxisRecord = prepareAxisRecord(axisRecordMap, xAxisModel);
        const yAxisRecord = prepareAxisRecord(axisRecordMap, yAxisModel);
        xAxisRecord.rawExtentInfo = ensureScaleRawExtentInfo(
            xAxis.scale, xAxisModel, xAxisRecord.condExtent
        );
        yAxisRecord.rawExtentInfo = ensureScaleRawExtentInfo(
            yAxis.scale, yAxisModel, yAxisRecord.condExtent
        );
        xAxisRecord.rawExtentResult = xAxisRecord.rawExtentInfo.calculate();
        yAxisRecord.rawExtentResult = yAxisRecord.rawExtentInfo.calculate();
 
        // If the "xAxis" is set `min`/`max`, some data items might be out of the cartesian.
        // then the "yAxis" may needs to calculate extent only based on the data items inside
        // the cartesian (similar to what "dataZoom" did).
        // A typical case is bar-racing, where bars ara sort dynamically and may only need to
        // displayed part of the whole bars.
 
        const data = seriesRecord.seriesModel.getData();
        // For duplication removal.
        const condDimMap: Dictionary<boolean> = {};
        const tarDimMap: Dictionary<boolean> = {};
        let condAxis: Axis;
        let tarAxisRecord: AxisRecord;
 
        function addCondition(axis: Axis, axisRecord: AxisRecord) {
            // But for simplicity and safty and performance, we only adopt this
            // feature on category axis at present.
            const condExtent = axisRecord.condExtent;
            const rawExtentResult = axisRecord.rawExtentResult;
            if (axis.type === 'category'
                && (condExtent[0] < rawExtentResult.min || rawExtentResult.max < condExtent[1])
            ) {
                each(getDataDimensionsOnAxis(data, axis.dim), function (dataDim) {
                    if (!hasOwn(condDimMap, dataDim)) {
                        condDimMap[dataDim] = true;
                        condAxis = axis;
                    }
                });
            }
        }
        function addTarget(axis: Axis, axisRecord: AxisRecord) {
            const rawExtentResult = axisRecord.rawExtentResult;
            if (axis.type !== 'category'
                && (!rawExtentResult.minFixed || !rawExtentResult.maxFixed)
            ) {
                each(getDataDimensionsOnAxis(data, axis.dim), function (dataDim) {
                    if (!hasOwn(condDimMap, dataDim) && !hasOwn(tarDimMap, dataDim)) {
                        tarDimMap[dataDim] = true;
                        tarAxisRecord = axisRecord;
                    }
                });
            }
        }
 
        addCondition(xAxis, xAxisRecord);
        addCondition(yAxis, yAxisRecord);
        addTarget(xAxis, xAxisRecord);
        addTarget(yAxis, yAxisRecord);
 
        const condDims = keys(condDimMap);
        const tarDims = keys(tarDimMap);
        const tarDimExtents = map(tarDims, function () {
            return initExtent();
        });
 
        const condDimsLen = condDims.length;
        const tarDimsLen = tarDims.length;
 
        if (!condDimsLen || !tarDimsLen) {
            return;
        }
 
        const singleCondDim = condDimsLen === 1 ? condDims[0] : null;
        const singleTarDim = tarDimsLen === 1 ? tarDims[0] : null;
        const dataLen = data.count();
 
        // Time consuming, because this is a "block task".
        // Simple optimization for the vast majority of cases.
        if (singleCondDim && singleTarDim) {
            for (let dataIdx = 0; dataIdx < dataLen; dataIdx++) {
                const condVal = data.get(singleCondDim, dataIdx) as number;
                if (condAxis.scale.isInExtentRange(condVal)) {
                    unionExtent(tarDimExtents[0], data.get(singleTarDim, dataIdx) as number);
                }
            }
        }
        else {
            for (let dataIdx = 0; dataIdx < dataLen; dataIdx++) {
                for (let j = 0; j < condDimsLen; j++) {
                    const condVal = data.get(condDims[j], dataIdx) as number;
                    if (condAxis.scale.isInExtentRange(condVal)) {
                        for (let k = 0; k < tarDimsLen; k++) {
                            unionExtent(tarDimExtents[k], data.get(tarDims[k], dataIdx) as number);
                        }
                        // Any one dim is in range means satisfied.
                        break;
                    }
                }
            }
        }
 
        each(tarDimExtents, function (tarDimExtent, i) {
            const dim = tarDims[i];
            // FIXME: if there has been approximateExtent set?
            data.setApproximateExtent(tarDimExtent as [number, number], dim);
            const tarAxisExtent = tarAxisRecord.tarExtent = tarAxisRecord.tarExtent || initExtent();
            unionExtent(tarAxisExtent, tarDimExtent[0]);
            unionExtent(tarAxisExtent, tarDimExtent[1]);
        });
    });
}
 
function shrinkAxisExtent(axisRecordMap: HashMap<AxisRecord>) {
    axisRecordMap.each(function (axisRecord) {
        const tarAxisExtent = axisRecord.tarExtent;
        if (tarAxisExtent) {
            const rawExtentResult = axisRecord.rawExtentResult;
            const rawExtentInfo = axisRecord.rawExtentInfo;
            // Shink the original extent.
            if (!rawExtentResult.minFixed && tarAxisExtent[0] > rawExtentResult.min) {
                rawExtentInfo.modifyDataMinMax('min', tarAxisExtent[0]);
            }
            if (!rawExtentResult.maxFixed && tarAxisExtent[1] < rawExtentResult.max) {
                rawExtentInfo.modifyDataMinMax('max', tarAxisExtent[1]);
            }
        }
    });
}
 
function prepareAxisRecord(
    axisRecordMap: HashMap<AxisRecord>,
    axisModel: AxisBaseModel
): AxisRecord {
    return axisRecordMap.get(axisModel.uid)
        || axisRecordMap.set(axisModel.uid, { condExtent: initExtent() });
}
 
function initExtent() {
    return [Infinity, -Infinity];
}
 
function unionExtent(extent: number[], val: number) {
    val < extent[0] && (extent[0] = val);
    val > extent[1] && (extent[1] = val);
}