RadarView.ts 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* 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.
*/

S
sushuang 已提交
20
import * as graphic from '../../util/graphic';
S
sushuang 已提交
21
import * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
22
import * as symbolUtil from '../../util/symbol';
P
pissang 已提交
23 24 25 26 27 28
import ChartView from '../../view/Chart';
import RadarSeriesModel, { RadarSeriesDataItemOption } from './RadarSeries';
import ExtensionAPI from '../../ExtensionAPI';
import List from '../../data/List';
import { ZRColor, DisplayState, ECElement } from '../../util/types';
import GlobalModel from '../../model/Global';
1
100pah 已提交
29
import { VectorArray } from 'zrender/src/core/vector';
P
pissang 已提交
30 31

function normalizeSymbolSize(symbolSize: number | number[]) {
S
sushuang 已提交
32 33 34 35 36
    if (!zrUtil.isArray(symbolSize)) {
        symbolSize = [+symbolSize, +symbolSize];
    }
    return symbolSize;
}
L
lang 已提交
37

P
pissang 已提交
38 39 40 41 42 43 44
type RadarSymbol = ReturnType<typeof symbolUtil.createSymbol> & {
    __dimIdx: number
}

class RadarView extends ChartView {
    static type = 'radar'
    type = RadarView.type
L
lang 已提交
45

P
pissang 已提交
46
    private _data: List<RadarSeriesModel>
L
lang 已提交
47

P
pissang 已提交
48
    render(seriesModel: RadarSeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
S
sushuang 已提交
49 50
        var polar = seriesModel.coordinateSystem;
        var group = this.group;
L
lang 已提交
51

S
sushuang 已提交
52 53
        var data = seriesModel.getData();
        var oldData = this._data;
L
lang 已提交
54

P
pissang 已提交
55 56 57
        function createSymbol(data: List<RadarSeriesModel>, idx: number) {
            var symbolType = data.getItemVisual(idx, 'symbol') as string || 'circle';
            var color = data.getItemVisual(idx, 'color') as ZRColor;
S
sushuang 已提交
58 59
            if (symbolType === 'none') {
                return;
L
lang 已提交
60
            }
S
sushuang 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
            var symbolSize = normalizeSymbolSize(
                data.getItemVisual(idx, 'symbolSize')
            );
            var symbolPath = symbolUtil.createSymbol(
                symbolType, -1, -1, 2, 2, color
            );
            symbolPath.attr({
                style: {
                    strokeNoScale: true
                },
                z2: 100,
                scale: [symbolSize[0] / 2, symbolSize[1] / 2]
            });
P
pissang 已提交
74
            return symbolPath as RadarSymbol;
S
sushuang 已提交
75
        }
L
lang 已提交
76

P
pissang 已提交
77
        function updateSymbols(
1
100pah 已提交
78 79
            oldPoints: VectorArray[],
            newPoints: VectorArray[],
P
pissang 已提交
80 81 82 83 84
            symbolGroup: graphic.Group,
            data: List<RadarSeriesModel>,
            idx: number,
            isInit?: boolean
        ) {
S
sushuang 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
            // Simply rerender all
            symbolGroup.removeAll();
            for (var i = 0; i < newPoints.length - 1; i++) {
                var symbolPath = createSymbol(data, idx);
                if (symbolPath) {
                    symbolPath.__dimIdx = i;
                    if (oldPoints[i]) {
                        symbolPath.attr('position', oldPoints[i]);
                        graphic[isInit ? 'initProps' : 'updateProps'](
                            symbolPath, {
                                position: newPoints[i]
                            }, seriesModel, idx
                        );
L
lang 已提交
98
                    }
S
sushuang 已提交
99 100 101 102
                    else {
                        symbolPath.attr('position', newPoints[i]);
                    }
                    symbolGroup.add(symbolPath);
L
lang 已提交
103
                }
L
lang 已提交
104
            }
S
sushuang 已提交
105
        }
L
lang 已提交
106

P
pissang 已提交
107
        function getInitialPoints(points: number[][]) {
S
sushuang 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            return zrUtil.map(points, function (pt) {
                return [polar.cx, polar.cy];
            });
        }
        data.diff(oldData)
            .add(function (idx) {
                var points = data.getItemLayout(idx);
                if (!points) {
                    return;
                }
                var polygon = new graphic.Polygon();
                var polyline = new graphic.Polyline();
                var target = {
                    shape: {
                        points: points
L
lang 已提交
123
                    }
S
sushuang 已提交
124
                };
125

S
sushuang 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                polygon.shape.points = getInitialPoints(points);
                polyline.shape.points = getInitialPoints(points);
                graphic.initProps(polygon, target, seriesModel, idx);
                graphic.initProps(polyline, target, seriesModel, idx);

                var itemGroup = new graphic.Group();
                var symbolGroup = new graphic.Group();
                itemGroup.add(polyline);
                itemGroup.add(polygon);
                itemGroup.add(symbolGroup);

                updateSymbols(
                    polyline.shape.points, points, symbolGroup, data, idx, true
                );

                data.setItemGraphicEl(idx, itemGroup);
            })
            .update(function (newIdx, oldIdx) {
P
pissang 已提交
144 145 146 147
                var itemGroup = oldData.getItemGraphicEl(oldIdx) as graphic.Group;
                var polyline = itemGroup.childAt(0) as graphic.Polyline;
                var polygon = itemGroup.childAt(1) as graphic.Polygon;
                var symbolGroup = itemGroup.childAt(2) as graphic.Group;
S
sushuang 已提交
148 149 150 151 152
                var target = {
                    shape: {
                        points: data.getItemLayout(newIdx)
                    }
                };
153

S
sushuang 已提交
154 155 156 157
                if (!target.shape.points) {
                    return;
                }
                updateSymbols(
P
pissang 已提交
158 159 160 161 162 163
                    polyline.shape.points,
                    target.shape.points,
                    symbolGroup,
                    data,
                    newIdx,
                    false
L
lang 已提交
164
                );
S
sushuang 已提交
165 166 167 168 169 170 171 172 173 174 175

                graphic.updateProps(polyline, target, seriesModel);
                graphic.updateProps(polygon, target, seriesModel);

                data.setItemGraphicEl(newIdx, itemGroup);
            })
            .remove(function (idx) {
                group.remove(oldData.getItemGraphicEl(idx));
            })
            .execute();

P
pissang 已提交
176 177 178 179 180
        data.eachItemGraphicEl(function (itemGroup: graphic.Group, idx) {
            var itemModel = data.getItemModel<RadarSeriesDataItemOption>(idx);
            var polyline = itemGroup.childAt(0) as graphic.Polyline;
            var polygon = itemGroup.childAt(1) as graphic.Polygon;
            var symbolGroup = itemGroup.childAt(2) as graphic.Group;
S
sushuang 已提交
181 182 183 184 185 186
            var color = data.getItemVisual(idx, 'color');

            group.add(itemGroup);

            polyline.useStyle(
                zrUtil.defaults(
187
                    itemModel.getModel('lineStyle').getLineStyle(),
S
sushuang 已提交
188 189 190 191 192 193
                    {
                        fill: 'none',
                        stroke: color
                    }
                )
            );
P
pissang 已提交
194
            polyline.hoverStyle = itemModel.getModel(['emphasis', 'lineStyle']).getLineStyle();
S
sushuang 已提交
195

P
pissang 已提交
196
            var areaStyleModel = itemModel.getModel('areaStyle');
P
pissang 已提交
197
            var hoverAreaStyleModel = itemModel.getModel(['emphasis', 'areaStyle']);
S
sushuang 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
            var polygonIgnore = areaStyleModel.isEmpty() && areaStyleModel.parentModel.isEmpty();
            var hoverPolygonIgnore = hoverAreaStyleModel.isEmpty() && hoverAreaStyleModel.parentModel.isEmpty();

            hoverPolygonIgnore = hoverPolygonIgnore && polygonIgnore;
            polygon.ignore = polygonIgnore;

            polygon.useStyle(
                zrUtil.defaults(
                    areaStyleModel.getAreaStyle(),
                    {
                        fill: color,
                        opacity: 0.7
                    }
                )
            );
            polygon.hoverStyle = hoverAreaStyleModel.getAreaStyle();

215
            var itemStyle = itemModel.getModel('itemStyle').getItemStyle(['color']);
P
pissang 已提交
216
            var itemHoverStyle = itemModel.getModel(['emphasis', 'itemStyle']).getItemStyle();
217
            var labelModel = itemModel.getModel('label');
P
pissang 已提交
218 219
            var labelHoverModel = itemModel.getModel(['emphasis', 'label']);
            symbolGroup.eachChild(function (symbolPath: RadarSymbol) {
S
sushuang 已提交
220 221
                symbolPath.setStyle(itemStyle);
                symbolPath.hoverStyle = zrUtil.clone(itemHoverStyle);
222
                var defaultText = data.get(data.dimensions[symbolPath.__dimIdx], idx);
P
pissang 已提交
223
                (defaultText == null || isNaN(defaultText as number)) && (defaultText = '');
S
sushuang 已提交
224 225 226 227 228 229 230

                graphic.setLabelStyle(
                    symbolPath.style, symbolPath.hoverStyle, labelModel, labelHoverModel,
                    {
                        labelFetcher: data.hostModel,
                        labelDataIndex: idx,
                        labelDimIndex: symbolPath.__dimIdx,
P
pissang 已提交
231
                        defaultText: defaultText as string,
S
sushuang 已提交
232 233 234
                        autoColor: color,
                        isRectText: true
                    }
L
lang 已提交
235
                );
S
sushuang 已提交
236
            });
L
lang 已提交
237

P
pissang 已提交
238
            (itemGroup as ECElement).highDownOnUpdate = function (fromState: DisplayState, toState: DisplayState) {
S
Tweak  
sushuang 已提交
239 240
                polygon.attr('ignore', toState === 'emphasis' ? hoverPolygonIgnore : polygonIgnore);
            };
S
sushuang 已提交
241 242
            graphic.setHoverStyle(itemGroup);
        });
L
lang 已提交
243

S
sushuang 已提交
244
        this._data = data;
P
pissang 已提交
245
    }
L
lang 已提交
246

P
pissang 已提交
247
    remove() {
S
sushuang 已提交
248 249
        this.group.removeAll();
        this._data = null;
P
pissang 已提交
250 251
    }
}
1
100pah 已提交
252

P
pissang 已提交
253
ChartView.registerClass(RadarView);