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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* 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 zrUtil from 'zrender/src/core/util';
import {parsePercent} from '../util/number';
import {isDimensionStacked} from '../data/helper/dataStackHelper';
import type BarSeriesModel from '../chart/bar/BarSeries';
import type Polar from '../coord/polar/Polar';
import AngleAxis from '../coord/polar/AngleAxis';
import RadiusAxis from '../coord/polar/RadiusAxis';
import GlobalModel from '../model/Global';
import ExtensionAPI from '../core/ExtensionAPI';
import { Dictionary } from '../util/types';
 
type PolarAxis = AngleAxis | RadiusAxis;
 
interface StackInfo {
    width: number
    maxWidth: number
}
interface LayoutColumnInfo {
    autoWidthCount: number
    bandWidth: number
    remainedWidth: number
    categoryGap: string | number
    gap: string | number
    stacks: Dictionary<StackInfo>
}
 
interface BarWidthAndOffset {
    width: number
    offset: number
}
 
function getSeriesStackId(seriesModel: BarSeriesModel) {
    return seriesModel.get('stack')
        || '__ec_stack_' + seriesModel.seriesIndex;
}
 
function getAxisKey(polar: Polar, axis: PolarAxis) {
    return axis.dim + polar.model.componentIndex;
}
 
function barLayoutPolar(seriesType: string, ecModel: GlobalModel, api: ExtensionAPI) {
 
    const lastStackCoords: Dictionary<{p: number, n: number}[]> = {};
 
    const barWidthAndOffset = calRadialBar(
        zrUtil.filter(
            ecModel.getSeriesByType(seriesType) as BarSeriesModel[],
            function (seriesModel) {
                return !ecModel.isSeriesFiltered(seriesModel)
                    && seriesModel.coordinateSystem
                    && seriesModel.coordinateSystem.type === 'polar';
            }
        )
    );
 
    ecModel.eachSeriesByType(seriesType, function (seriesModel: BarSeriesModel) {
 
        // Check series coordinate, do layout for polar only
        if (seriesModel.coordinateSystem.type !== 'polar') {
            return;
        }
 
        const data = seriesModel.getData();
        const polar = seriesModel.coordinateSystem as Polar;
        const baseAxis = polar.getBaseAxis();
        const axisKey = getAxisKey(polar, baseAxis);
 
        const stackId = getSeriesStackId(seriesModel);
        const columnLayoutInfo = barWidthAndOffset[axisKey][stackId];
        const columnOffset = columnLayoutInfo.offset;
        const columnWidth = columnLayoutInfo.width;
        const valueAxis = polar.getOtherAxis(baseAxis);
 
        const cx = seriesModel.coordinateSystem.cx;
        const cy = seriesModel.coordinateSystem.cy;
 
        const barMinHeight = seriesModel.get('barMinHeight') || 0;
        const barMinAngle = seriesModel.get('barMinAngle') || 0;
 
        lastStackCoords[stackId] = lastStackCoords[stackId] || [];
 
        const valueDim = data.mapDimension(valueAxis.dim);
        const baseDim = data.mapDimension(baseAxis.dim);
        const stacked = isDimensionStacked(data, valueDim /*, baseDim*/);
        const clampLayout = baseAxis.dim !== 'radius'
            || !seriesModel.get('roundCap', true);
 
        const valueAxisStart = valueAxis.dataToCoord(0);
        for (let idx = 0, len = data.count(); idx < len; idx++) {
            const value = data.get(valueDim, idx) as number;
            const baseValue = data.get(baseDim, idx) as number;
 
            const sign = value >= 0 ? 'p' : 'n' as 'p' | 'n';
            let baseCoord = valueAxisStart;
 
            // Because of the barMinHeight, we can not use the value in
            // stackResultDimension directly.
            // Only ordinal axis can be stacked.
            if (stacked) {
 
                if (!lastStackCoords[stackId][baseValue]) {
                    lastStackCoords[stackId][baseValue] = {
                        p: valueAxisStart, // Positive stack
                        n: valueAxisStart  // Negative stack
                    };
                }
                // Should also consider #4243
                baseCoord = lastStackCoords[stackId][baseValue][sign];
            }
 
            let r0;
            let r;
            let startAngle;
            let endAngle;
 
            // radial sector
            if (valueAxis.dim === 'radius') {
                let radiusSpan = valueAxis.dataToCoord(value) - valueAxisStart;
                const angle = baseAxis.dataToCoord(baseValue);
 
                if (Math.abs(radiusSpan) < barMinHeight) {
                    radiusSpan = (radiusSpan < 0 ? -1 : 1) * barMinHeight;
                }
 
                r0 = baseCoord;
                r = baseCoord + radiusSpan;
                startAngle = angle - columnOffset;
                endAngle = startAngle - columnWidth;
 
                stacked && (lastStackCoords[stackId][baseValue][sign] = r);
            }
            // tangential sector
            else {
                let angleSpan = valueAxis.dataToCoord(value, clampLayout) - valueAxisStart;
                const radius = baseAxis.dataToCoord(baseValue);
 
                if (Math.abs(angleSpan) < barMinAngle) {
                    angleSpan = (angleSpan < 0 ? -1 : 1) * barMinAngle;
                }
 
                r0 = radius + columnOffset;
                r = r0 + columnWidth;
                startAngle = baseCoord;
                endAngle = baseCoord + angleSpan;
 
                // if the previous stack is at the end of the ring,
                // add a round to differentiate it from origin
                // let extent = angleAxis.getExtent();
                // let stackCoord = angle;
                // if (stackCoord === extent[0] && value > 0) {
                //     stackCoord = extent[1];
                // }
                // else if (stackCoord === extent[1] && value < 0) {
                //     stackCoord = extent[0];
                // }
                stacked && (lastStackCoords[stackId][baseValue][sign] = endAngle);
            }
 
            data.setItemLayout(idx, {
                cx: cx,
                cy: cy,
                r0: r0,
                r: r,
                // Consider that positive angle is anti-clockwise,
                // while positive radian of sector is clockwise
                startAngle: -startAngle * Math.PI / 180,
                endAngle: -endAngle * Math.PI / 180
            });
 
        }
 
    });
 
}
 
/**
 * Calculate bar width and offset for radial bar charts
 */
function calRadialBar(barSeries: BarSeriesModel[]) {
    // Columns info on each category axis. Key is polar name
    const columnsMap: Dictionary<LayoutColumnInfo> = {};
 
    zrUtil.each(barSeries, function (seriesModel, idx) {
        const data = seriesModel.getData();
        const polar = seriesModel.coordinateSystem as Polar;
 
        const baseAxis = polar.getBaseAxis();
        const axisKey = getAxisKey(polar, baseAxis);
 
        const axisExtent = baseAxis.getExtent();
        const bandWidth = baseAxis.type === 'category'
            ? baseAxis.getBandWidth()
            : (Math.abs(axisExtent[1] - axisExtent[0]) / data.count());
 
        const columnsOnAxis = columnsMap[axisKey] || {
            bandWidth: bandWidth,
            remainedWidth: bandWidth,
            autoWidthCount: 0,
            categoryGap: '20%',
            gap: '30%',
            stacks: {}
        };
        const stacks = columnsOnAxis.stacks;
        columnsMap[axisKey] = columnsOnAxis;
 
        const stackId = getSeriesStackId(seriesModel);
 
        if (!stacks[stackId]) {
            columnsOnAxis.autoWidthCount++;
        }
        stacks[stackId] = stacks[stackId] || {
            width: 0,
            maxWidth: 0
        };
 
        let barWidth = parsePercent(
            seriesModel.get('barWidth'),
            bandWidth
        );
        const barMaxWidth = parsePercent(
            seriesModel.get('barMaxWidth'),
            bandWidth
        );
        const barGap = seriesModel.get('barGap');
        const barCategoryGap = seriesModel.get('barCategoryGap');
 
        if (barWidth && !stacks[stackId].width) {
            barWidth = Math.min(columnsOnAxis.remainedWidth, barWidth);
            stacks[stackId].width = barWidth;
            columnsOnAxis.remainedWidth -= barWidth;
        }
 
        barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
        (barGap != null) && (columnsOnAxis.gap = barGap);
        (barCategoryGap != null) && (columnsOnAxis.categoryGap = barCategoryGap);
    });
 
 
    const result: Dictionary<Dictionary<BarWidthAndOffset>> = {};
 
    zrUtil.each(columnsMap, function (columnsOnAxis, coordSysName) {
 
        result[coordSysName] = {};
 
        const stacks = columnsOnAxis.stacks;
        const bandWidth = columnsOnAxis.bandWidth;
        const categoryGap = parsePercent(columnsOnAxis.categoryGap, bandWidth);
        const barGapPercent = parsePercent(columnsOnAxis.gap, 1);
 
        let remainedWidth = columnsOnAxis.remainedWidth;
        let autoWidthCount = columnsOnAxis.autoWidthCount;
        let autoWidth = (remainedWidth - categoryGap)
            / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
        autoWidth = Math.max(autoWidth, 0);
 
        // Find if any auto calculated bar exceeded maxBarWidth
        zrUtil.each(stacks, function (column, stack) {
            let maxWidth = column.maxWidth;
            if (maxWidth && maxWidth < autoWidth) {
                maxWidth = Math.min(maxWidth, remainedWidth);
                if (column.width) {
                    maxWidth = Math.min(maxWidth, column.width);
                }
                remainedWidth -= maxWidth;
                column.width = maxWidth;
                autoWidthCount--;
            }
        });
 
        // Recalculate width again
        autoWidth = (remainedWidth - categoryGap)
            / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
        autoWidth = Math.max(autoWidth, 0);
 
        let widthSum = 0;
        let lastColumn: StackInfo;
        zrUtil.each(stacks, function (column, idx) {
            if (!column.width) {
                column.width = autoWidth;
            }
            lastColumn = column;
            widthSum += column.width * (1 + barGapPercent);
        });
        if (lastColumn) {
            widthSum -= lastColumn.width * barGapPercent;
        }
 
        let offset = -widthSum / 2;
        zrUtil.each(stacks, function (column, stackId) {
            result[coordSysName][stackId] = result[coordSysName][stackId] || {
                offset: offset,
                width: column.width
            } as BarWidthAndOffset;
 
            offset += column.width * (1 + barGapPercent);
        });
    });
 
    return result;
}
 
export default barLayoutPolar;