setOption.test.ts 11.1 KB
Newer Older
S
sushuang 已提交
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.
*/
1
100pah 已提交
20

21
import { createChart, getECModel } from '../../../core/utHelper';
1
100pah 已提交
22
import { EChartsType } from '../../../../../src/echarts';
P
pissang 已提交
23
import { EChartsOption } from '../../../../../src/export/option';
1
100pah 已提交
24 25 26 27
import { ContinousVisualMapOption } from '../../../../../src/component/visualMap/ContinuousModel';
import { PiecewiseVisualMapOption } from '../../../../../src/component/visualMap/PiecewiseModel';
import VisualMapModel from '../../../../../src/component/visualMap/VisualMapModel';

S
sushuang 已提交
28

29
describe('vsiaulMap_setOption', function () {
1
100pah 已提交
30
    let chart: EChartsType;
31
    beforeEach(function () {
1
100pah 已提交
32
        chart = createChart();
33
    });
34

35
    afterEach(function () {
P
pissang 已提交
36
        chart.dispose();
37
    });
38

39 40
    it('defaultTargetController', function (done) {
        chart.setOption({
41 42 43 44 45 46 47 48 49 50
            xAxis: {},
            yAxis: {},
            series: [{type: 'scatter', data: [[12, 223]]}],
            visualMap: {
                inRange: {
                    color: ['red', 'blue', 'yellow']
                }
            }
        });

1
100pah 已提交
51 52
        const option = chart.getOption();
        const visualMapOptionGotten = option.visualMap as (ContinousVisualMapOption | PiecewiseVisualMapOption)[];
53

1
100pah 已提交
54 55 56 57
        expect(visualMapOptionGotten.length).toEqual(1);
        expect(visualMapOptionGotten[0].inRange.color).toEqual(['red', 'blue', 'yellow']);
        expect(visualMapOptionGotten[0].target.inRange.color).toEqual(['red', 'blue', 'yellow']);
        expect(visualMapOptionGotten[0].controller.inRange.color).toEqual(['red', 'blue', 'yellow']);
58
        done();
59 60
    });

61 62
    it('ec2ColorCompatiable', function (done) {
        chart.setOption({
63 64 65 66 67 68 69 70
            xAxis: {},
            yAxis: {},
            series: [{type: 'scatter', data: [[12, 223]]}],
            visualMap: {
                color: ['yellow', 'blue', 'red']
            }
        });

1
100pah 已提交
71 72
        const option = chart.getOption();
        const visualMapOptionGotten = option.visualMap as (ContinousVisualMapOption | PiecewiseVisualMapOption)[];
73

1
100pah 已提交
74 75 76 77
        expect(visualMapOptionGotten.length).toEqual(1);
        expect(visualMapOptionGotten[0].color).toEqual(['yellow', 'blue', 'red']);
        expect(visualMapOptionGotten[0].target.inRange.color).toEqual(['red', 'blue', 'yellow']);
        expect(visualMapOptionGotten[0].controller.inRange.color).toEqual(['red', 'blue', 'yellow']);
78
        done();
79 80
    });

81 82
    it('remainVisualProp', function (done) {
        chart.setOption({
83 84 85 86 87 88 89 90 91 92
            xAxis: {},
            yAxis: {},
            series: [{type: 'scatter', data: [[12, 223]]}],
            visualMap: {
                inRange: {
                    color: ['red', 'blue', 'yellow']
                }
            }
        });

93
        chart.setOption({
94 95 96
            visualMap: {}
        });

P
pissang 已提交
97
        expectTheSame(chart.getOption() as EChartsOption);
98

99
        chart.setOption({
100 101 102
            series: [{data: [[44, 55]]}] // visualMap depends series
        });

P
pissang 已提交
103
        expectTheSame(chart.getOption() as EChartsOption);
104

P
pissang 已提交
105
        function expectTheSame(option: EChartsOption) {
1
100pah 已提交
106 107 108 109 110
            const visualMapOptionGotten = option.visualMap as (ContinousVisualMapOption | PiecewiseVisualMapOption)[];
            expect(visualMapOptionGotten.length).toEqual(1);
            expect(visualMapOptionGotten[0].inRange.color).toEqual(['red', 'blue', 'yellow']);
            expect(visualMapOptionGotten[0].target.inRange.color).toEqual(['red', 'blue', 'yellow']);
            expect(visualMapOptionGotten[0].controller.inRange.color).toEqual(['red', 'blue', 'yellow']);
111
            done();
112 113 114
        }
    });

115 116
    it('eraseAllVisualProps_notRelative', function (done) {
        chart.setOption({
117 118 119 120 121 122 123 124 125 126 127
            xAxis: {},
            yAxis: {},
            series: [{type: 'scatter', data: [[12, 223]]}],
            visualMap: {
                inRange: {
                    color: ['red', 'blue', 'yellow'],
                    symbolSize: [0.3, 0.5]
                }
            }
        });

1
100pah 已提交
128
        // const option = chart.getOption();
129

130
        chart.setOption({
131 132 133 134 135 136 137
            visualMap: {
                inRange: {
                    symbolSize: [0.4, 0.6]
                }
            }
        });

1
100pah 已提交
138 139
        const option = chart.getOption();
        const visualMapOptionGotten = option.visualMap as (ContinousVisualMapOption | PiecewiseVisualMapOption)[];
140

1
100pah 已提交
141 142 143 144 145 146
        expect(visualMapOptionGotten.length).toEqual(1);
        expect(visualMapOptionGotten[0].inRange.hasOwnProperty('color')).toEqual(false);
        expect(visualMapOptionGotten[0].target.inRange.hasOwnProperty('color')).toEqual(false);
        expect(visualMapOptionGotten[0].controller.inRange.hasOwnProperty('color')).toEqual(false);
        expect(visualMapOptionGotten[0].inRange.symbolSize).toEqual([0.4, 0.6]);
        expect(visualMapOptionGotten[0].target.inRange.symbolSize).toEqual([0.4, 0.6]);
147
        done();
148 149 150 151
        // Do not compare controller.inRange.symbolSize, which will be amplified to controller size.
        // expect(option.visualMap[0].controller.inRange.symbolSize).toEqual([?, ?]);
    });

152 153
    it('eraseAllVisualProps_reletive', function (done) {
        chart.setOption({
154 155 156 157 158 159 160 161 162 163 164
            xAxis: {},
            yAxis: {},
            series: [{type: 'scatter', data: [[12, 223]]}],
            visualMap: {
                inRange: {
                    color: ['red', 'blue', 'yellow'],
                    colorAlpha: [0.3, 0.5]
                }
            }
        });

165
        chart.setOption({
166 167 168 169 170 171 172
            visualMap: {
                inRange: {
                    colorAlpha: [0.4, 0.6]
                }
            }
        });

1
100pah 已提交
173 174 175 176 177 178 179 180 181
        let visualMapOptionGotten: (ContinousVisualMapOption | PiecewiseVisualMapOption)[];
        visualMapOptionGotten = chart.getOption().visualMap as typeof visualMapOptionGotten;
        expect(visualMapOptionGotten.length).toEqual(1);
        expect(visualMapOptionGotten[0].inRange.hasOwnProperty('color')).toEqual(false);
        expect(visualMapOptionGotten[0].target.inRange.hasOwnProperty('color')).toEqual(false);
        expect(visualMapOptionGotten[0].controller.inRange.hasOwnProperty('color')).toEqual(false);
        expect(visualMapOptionGotten[0].inRange.colorAlpha).toEqual([0.4, 0.6]);
        expect(visualMapOptionGotten[0].target.inRange.colorAlpha).toEqual([0.4, 0.6]);
        expect(visualMapOptionGotten[0].controller.inRange.colorAlpha).toEqual([0.4, 0.6]);
182

183
        chart.setOption({
184 185 186 187 188
            visualMap: {
                color: ['red', 'blue', 'green']
            }
        });

1
100pah 已提交
189 190 191 192 193 194
        visualMapOptionGotten = chart.getOption().visualMap as typeof visualMapOptionGotten;
        expect(visualMapOptionGotten.length).toEqual(1);
        expect(visualMapOptionGotten[0].target.inRange.hasOwnProperty('colorAlpha')).toEqual(false);
        expect(visualMapOptionGotten[0].controller.inRange.hasOwnProperty('colorAlpha')).toEqual(false);
        expect(visualMapOptionGotten[0].target.inRange.color).toEqual(['green', 'blue', 'red']);
        expect(visualMapOptionGotten[0].controller.inRange.color).toEqual(['green', 'blue', 'red']);
195

196
        chart.setOption({
197 198 199 200 201 202 203 204 205
            visualMap: {
                controller: {
                    outOfRange: {
                        symbol: ['diamond']
                    }
                }
            }
        });

1
100pah 已提交
206
        visualMapOptionGotten = chart.getOption().visualMap as typeof visualMapOptionGotten;
207

1
100pah 已提交
208 209 210 211
        expect(visualMapOptionGotten.length).toEqual(1);
        expect(!!visualMapOptionGotten[0].target.inRange).toEqual(true);
        let onlyColor = true;
        for (const i in visualMapOptionGotten[0].target.inRange) {
1
100pah 已提交
212 213 214 215
            if (i !== 'color') {
                onlyColor = false;
            }
        }
1
100pah 已提交
216
        const inRangeColor = visualMapOptionGotten[0].target.inRange.color;
1
100pah 已提交
217 218
        expect(onlyColor).toEqual(true);
        expect(inRangeColor).toEqual(['#f6efa6', '#d88273', '#bf444c']);
1
100pah 已提交
219
        expect(visualMapOptionGotten[0].controller.outOfRange.symbol).toEqual(['diamond']);
220
        done();
221 222
    });

223 224
    it('setOpacityWhenUseColor', function (done) {
        chart.setOption({
225 226 227 228 229 230 231 232 233
            xAxis: {},
            yAxis: {},
            series: [{type: 'scatter', data: [[12, 223]]}],
            visualMap: {
                inRange: {
                    color: ['red', 'blue', 'yellow']
                }
            }
        });
234

1
100pah 已提交
235 236 237 238
        const visualMapOptionGotten = chart.getOption().visualMap as (
            ContinousVisualMapOption | PiecewiseVisualMapOption
        )[];
        expect(!!visualMapOptionGotten[0].target.outOfRange.opacity).toEqual(true);
239
        done();
240
    });
241

242
    it('normalizeVisualRange', function (done) {
P
pissang 已提交
243
        chart.setOption({
P
pah100 已提交
244 245 246 247 248 249 250 251 252 253
            xAxis: {},
            yAxis: {},
            series: [{type: 'scatter', data: [[12, 223]]}],
            visualMap: [
                {type: 'continuous', inRange: {color: 'red'}},
                {type: 'continuous', inRange: {opacity: 0.4}},
                {type: 'piecewise', inRange: {color: 'red'}},
                {type: 'piecewise', inRange: {opacity: 0.4}},
                {type: 'piecewise', inRange: {symbol: 'diamond'}},
                {type: 'piecewise', inRange: {color: 'red'}, categories: ['a', 'b']},
254 255
                {type: 'piecewise', inRange: {color: {a: 'red'}}, categories: ['a', 'b']},
                {type: 'piecewise', inRange: {opacity: 0.4}, categories: ['a', 'b']}
P
pah100 已提交
256 257 258
            ]
        });

259
        const ecModel = getECModel(chart);
P
pah100 已提交
260

1
100pah 已提交
261 262
        function getVisual(idx: number, visualType: 'color' | 'opacity' | 'symbol') {
            return (ecModel.getComponent('visualMap', idx) as VisualMapModel)
P
pah100 已提交
263 264 265
                .targetVisuals.inRange[visualType].option.visual;
        }

1
100pah 已提交
266 267 268 269 270
        function makeCategoryVisual(...args: unknown[]) {
            const CATEGORY_DEFAULT_VISUAL_INDEX = -1;
            const arr = [];
            if (args[0] != null) {
                arr[CATEGORY_DEFAULT_VISUAL_INDEX] = args[0];
P
pah100 已提交
271
            }
1
100pah 已提交
272 273
            for (let i = 1; i < args.length; i++) {
                arr.push(args[i]);
P
pah100 已提交
274 275 276 277 278 279 280 281 282 283 284 285
            }
            return arr;
        }

        expect(getVisual(0, 'color')).toEqual(['red']);
        expect(getVisual(1, 'opacity')).toEqual([0.4, 0.4]);
        expect(getVisual(2, 'color')).toEqual(['red']);
        expect(getVisual(3, 'opacity')).toEqual([0.4, 0.4]);
        expect(getVisual(4, 'symbol')).toEqual(['diamond']);
        expect(getVisual(5, 'color')).toEqual(makeCategoryVisual('red'));
        expect(getVisual(6, 'color')).toEqual(makeCategoryVisual(null, 'red'));
        expect(getVisual(7, 'opacity')).toEqual(makeCategoryVisual(0.4));
286
        done();
P
pah100 已提交
287
    });
288 289

});