RadarModel.ts 6.8 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 zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
21 22
import axisDefault from '../axisDefault';
import Model from '../../model/Model';
23
import {AxisModelCommonMixin} from '../axisModelCommonMixin';
24 25 26 27 28 29 30 31 32 33 34
import ComponentModel from '../../model/Component';
import {
    ComponentOption,
    CircleLayoutOptionMixin,
    LabelOption,
    ColorString
} from '../../util/types';
import { AxisBaseOption } from '../axisCommonTypes';
import { AxisBaseModel } from '../AxisBaseModel';
import Radar from './Radar';
import {CoordinateSystemHostModel} from '../../coord/CoordinateSystem';
L
lang 已提交
35

36
const valueAxisDefault = axisDefault.value;
S
sushuang 已提交
37

38
function defaultsShow(opt: object, show: boolean) {
S
sushuang 已提交
39 40 41 42 43
    return zrUtil.defaults({
        show: show
    }, opt);
}

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
export interface RadarIndicatorOption {
    text?: string
    min?: number
    max?: number
    color?: ColorString

    axisType?: 'value' | 'log'
}

export interface RadarOption extends ComponentOption, CircleLayoutOptionMixin {
    startAngle?: number

    shape?: 'polygon' | 'circle'

    // TODO. axisType seems to have issue.
    // axisType?: 'value' | 'log'

    axisLine?: AxisBaseOption['axisLine']
    axisTick?: AxisBaseOption['axisTick']
    axisLabel?: AxisBaseOption['axisLabel']
    splitLine?: AxisBaseOption['splitLine']
    splitArea?: AxisBaseOption['splitArea']

    // TODO Use axisName?
    axisName?: {
        show?: boolean
        formatter?: string | ((name?: string, indicatorOpt?: InnerIndicatorAxisOption) => string)
    } & LabelOption
    axisNameGap?: number

    triggerEvent?: boolean

    scale?: boolean
    splitNumber?: number

    boundaryGap?: AxisBaseOption['boundaryGap']

    indicator?: RadarIndicatorOption[]
}

export interface InnerIndicatorAxisOption extends AxisBaseOption {
    // TODO Use type?
    // axisType?: 'value' | 'log'
}

class RadarModel extends ComponentModel<RadarOption> implements CoordinateSystemHostModel {
1
100pah 已提交
90 91
    static readonly type = 'radar';
    readonly type = RadarModel.type;
92

1
100pah 已提交
93
    coordinateSystem: Radar;
S
sushuang 已提交
94

1
100pah 已提交
95
    private _indicatorModels: AxisBaseModel<InnerIndicatorAxisOption>[];
S
sushuang 已提交
96

97
    optionUpdated() {
98 99 100 101 102
        const boundaryGap = this.get('boundaryGap');
        const splitNumber = this.get('splitNumber');
        const scale = this.get('scale');
        const axisLine = this.get('axisLine');
        const axisTick = this.get('axisTick');
103
        // let axisType = this.get('axisType');
104 105 106 107 108 109 110 111
        const axisLabel = this.get('axisLabel');
        const nameTextStyle = this.get('axisName');
        const showName = this.get(['axisName', 'show']);
        const nameFormatter = this.get(['axisName', 'formatter']);
        const nameGap = this.get('axisNameGap');
        const triggerEvent = this.get('triggerEvent');

        const indicatorModels = zrUtil.map(this.get('indicator') || [], function (indicatorOpt) {
S
sushuang 已提交
112 113 114 115 116 117 118
            // PENDING
            if (indicatorOpt.max != null && indicatorOpt.max > 0 && !indicatorOpt.min) {
                indicatorOpt.min = 0;
            }
            else if (indicatorOpt.min != null && indicatorOpt.min < 0 && !indicatorOpt.max) {
                indicatorOpt.max = 0;
            }
119
            let iNameTextStyle = nameTextStyle;
S
sushuang 已提交
120
            if (indicatorOpt.color != null) {
121 122 123
                iNameTextStyle = zrUtil.defaults({
                    color: indicatorOpt.color
                }, nameTextStyle);
S
sushuang 已提交
124 125
            }
            // Use same configuration
126
            const innerIndicatorOpt: InnerIndicatorAxisOption = zrUtil.merge(zrUtil.clone(indicatorOpt), {
S
sushuang 已提交
127 128 129 130 131
                boundaryGap: boundaryGap,
                splitNumber: splitNumber,
                scale: scale,
                axisLine: axisLine,
                axisTick: axisTick,
132
                // axisType: axisType,
S
sushuang 已提交
133
                axisLabel: axisLabel,
Z
zifix 已提交
134
                // Compatible with 2 and use text
S
sushuang 已提交
135 136 137 138 139 140 141 142
                name: indicatorOpt.text,
                nameLocation: 'end',
                nameGap: nameGap,
                // min: 0,
                nameTextStyle: iNameTextStyle,
                triggerEvent: triggerEvent
            }, false);
            if (!showName) {
143
                innerIndicatorOpt.name = '';
S
sushuang 已提交
144 145
            }
            if (typeof nameFormatter === 'string') {
146
                const indName = innerIndicatorOpt.name;
147
                innerIndicatorOpt.name = nameFormatter.replace('{value}', indName != null ? indName : '');
S
sushuang 已提交
148 149
            }
            else if (typeof nameFormatter === 'function') {
150 151
                innerIndicatorOpt.name = nameFormatter(
                    innerIndicatorOpt.name, innerIndicatorOpt
L
lang 已提交
152
                );
S
sushuang 已提交
153
            }
154

155
            const model = new Model(innerIndicatorOpt, null, this.ecModel) as AxisBaseModel<InnerIndicatorAxisOption>;
156
            zrUtil.mixin(model, AxisModelCommonMixin.prototype);
S
sushuang 已提交
157 158 159
            // For triggerEvent.
            model.mainType = 'radar';
            model.componentIndex = this.componentIndex;
160

S
sushuang 已提交
161 162
            return model;
        }, this);
L
lang 已提交
163

164 165 166 167 168 169
        this._indicatorModels = indicatorModels;
    }

    getIndicatorModels() {
        return this._indicatorModels;
    }
L
lang 已提交
170

171
    static defaultOption: RadarOption = {
L
lang 已提交
172

S
sushuang 已提交
173
        zlevel: 0,
L
lang 已提交
174

S
sushuang 已提交
175
        z: 0,
L
lang 已提交
176

S
sushuang 已提交
177
        center: ['50%', '50%'],
L
lang 已提交
178

S
sushuang 已提交
179
        radius: '75%',
L
lang 已提交
180

S
sushuang 已提交
181
        startAngle: 90,
L
lang 已提交
182

183
        axisName: {
S
sushuang 已提交
184 185 186 187
            show: true
            // formatter: null
            // textStyle: {}
        },
L
lang 已提交
188

S
sushuang 已提交
189
        boundaryGap: [0, 0],
L
lang 已提交
190

S
sushuang 已提交
191
        splitNumber: 5,
L
lang 已提交
192

193
        axisNameGap: 15,
L
lang 已提交
194

S
sushuang 已提交
195
        scale: false,
L
lang 已提交
196

S
sushuang 已提交
197 198
        // Polygon or circle
        shape: 'polygon',
L
lang 已提交
199

S
sushuang 已提交
200 201 202 203 204 205 206 207 208 209
        axisLine: zrUtil.merge(
            {
                lineStyle: {
                    color: '#bbb'
                }
            },
            valueAxisDefault.axisLine
        ),
        axisLabel: defaultsShow(valueAxisDefault.axisLabel, false),
        axisTick: defaultsShow(valueAxisDefault.axisTick, false),
210
        // axisType: 'value',
S
sushuang 已提交
211 212 213 214 215
        splitLine: defaultsShow(valueAxisDefault.splitLine, true),
        splitArea: defaultsShow(valueAxisDefault.splitArea, true),

        // {text, min, max}
        indicator: []
1
100pah 已提交
216
    };
217 218
}

1
100pah 已提交
219
ComponentModel.registerClass(RadarModel);
L
lang 已提交
220

E
earo-Lau 已提交
221
export default RadarModel;