PiecewiseModel.js 8.1 KB
Newer Older
P
pah100 已提交
1 2
define(function(require) {

3
    var VisualMapModel = require('./VisualMapModel');
P
pah100 已提交
4
    var zrUtil = require('zrender/core/util');
P
pah100 已提交
5
    var VisualMapping = require('../../visual/VisualMapping');
P
pah100 已提交
6

7
    return VisualMapModel.extend({
P
pah100 已提交
8

9
        type: 'visualMap.piecewise',
P
pah100 已提交
10 11 12 13 14 15

        /**
         * @protected
         */
        defaultOption: {
            selected: null,
P
pah100 已提交
16
            align: 'auto',             // 'auto', 'left', 'right'
P
pah100 已提交
17 18 19
            itemWidth: 20,             // 值域图形宽度,线性渐变水平布局宽度为该值 * 10
            itemHeight: 14,            // 值域图形高度,线性渐变垂直布局高度为该值 * 10
            itemSymbol: 'roundRect',
P
pah100 已提交
20
            splitList: null,           // 值顺序:由高到低, item can be:
21
                                    // {min, max, value, color, colorSaturation, colorAlpha, symbol, symbolSize}
P
pah100 已提交
22 23 24 25 26 27 28 29 30 31 32 33
            selectedMode: 'multiple',
            itemGap: 10                // 各个item之间的间隔,单位px,默认为10,
                                       // 横向布局时为水平间隔,纵向布局时为纵向间隔
        },

        /**
         * @override
         */
        mergeOption: function (newOption, isInit) {
            this.baseMergeOption(newOption);

            /**
34
             * Compatible with ec2, value order is [high, ..., low]
P
pah100 已提交
35 36 37 38 39 40 41 42 43 44
             * [{text: string, interval: Array.<number>}, ...]
             *
             * @private
             * @type {Array.<Object>}
             */
            this._pieceList = [];

            this.resetTargetSeries(newOption, isInit);
            this.resetExtent();

P
pah100 已提交
45 46
            var categories = this.option.categories;

P
pah100 已提交
47 48
            this.useCustomizedSplit()
                ? this._resetForCustomizedSplit()
P
pah100 已提交
49 50
                : categories
                ? this._resetForCategory()
P
pah100 已提交
51 52 53 54
                : this._resetForAutoSplit();

            this._resetSelected();

P
pah100 已提交
55
            var mappingMethod = categories ? 'category' : 'piecewise';
P
pah100 已提交
56 57

            this.resetVisual(function (mappingOption, state) {
P
pah100 已提交
58
                mappingOption.mappingMethod = mappingMethod;
L
lang 已提交
59
                mappingOption.categories = categories && zrUtil.clone(categories);
P
pah100 已提交
60

P
pah100 已提交
61
                var intervals = mappingOption.intervals = [];
P
pah100 已提交
62 63
                var specifiedVisuals = mappingOption.specifiedVisuals = [];

P
pah100 已提交
64
                zrUtil.each(this._pieceList, function (piece, index) {
P
pah100 已提交
65 66 67 68 69 70
                    if (piece.interval) {
                        intervals[index] = piece.interval;
                    }
                    if (state === 'inRange') {
                        specifiedVisuals[index] = piece.visuals;
                    }
P
pah100 已提交
71
                });
P
pah100 已提交
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
            });
        },

        _resetSelected: function () {
            var thisOption = this.option;
            var selected = thisOption.selected;
            var pieceList = this._pieceList;

            if (thisOption.selectedMode === 'single') {
                if (!selected) {
                    selected = thisOption.selected = [];
                }

                // Ensure there is only one selected.
                var hasSel = false;
                zrUtil.each(pieceList, function (piece, index) {
                    if (selected[index]) {
                        hasSel
                            ? (selected[index] = false)
                            : (hasSel = true);
                    }
                });

                // Ensure there is at least one selected.
                if (!hasSel) {
                    selected[0] = true;
                }
            }
            else { // thisOption.selectedMode === 'multiple'
                if (!selected) {
                    // Default: all selected.
                    selected = thisOption.selected = [];
                    zrUtil.each(pieceList, function () {
                        selected.push(true);
                    });
                }
            }
        },

        _resetForAutoSplit: function () {
            var thisOption = this.option;
            var precision = thisOption.precision;
            var dataExtent = this.getExtent();
            var splitNumber = thisOption.splitNumber;
            splitNumber = Math.max(parseInt(splitNumber, 10), 1);
            thisOption.splitNumber = splitNumber;

            var splitStep = (dataExtent[1] - dataExtent[0]) / splitNumber;
            // Precision auto-adaption
            while (+splitStep.toFixed(precision) !== splitStep && precision < 5) {
                precision++;
            }
            thisOption.precision = precision;
            splitStep = +splitStep.toFixed(precision);

            for (var i = 0, curr = dataExtent[0]; i < splitNumber; i++, curr += splitStep) {
                var max = i === splitNumber - 1 ? dataExtent[1] : (curr + splitStep);

                this._pieceList.push({
                    text: this.formatValueText(curr, max),
                    interval: [curr, max]
                });
            }
        },

P
pah100 已提交
137 138 139 140 141 142 143 144 145
        _resetForCategory: function () {
            zrUtil.each(this.option.categories, function (cate) {
                this._pieceList.push({
                    text: this.formatValueText(cate, null, true),
                    value: cate
                });
            }, this);
        },

P
pah100 已提交
146 147 148 149 150 151 152 153
        _resetForCustomizedSplit: function () {
            var option = this.option;
            var splitList = option.splitList;
            var splitNumber = splitList.length;

            for (var i = 0; i < splitNumber; i++) {
                var splitListItem = splitList[splitNumber - 1 - i];

P
pah100 已提交
154 155 156 157 158 159
                if (!zrUtil.isObject(splitListItem)) {
                    splitListItem = {value: splitListItem};
                }

                var item = {text: ''};
                var hasLabel;
P
pah100 已提交
160 161

                if (splitListItem.label != null) {
P
pah100 已提交
162 163
                    item.text = splitListItem.label;
                    hasLabel = true;
P
pah100 已提交
164
                }
P
pah100 已提交
165 166 167 168 169 170 171

                if (splitListItem.hasOwnProperty('value')) {
                    item.value = splitListItem.value;

                    if (!hasLabel) {
                        item.text = this.formatValueText(item.value);
                    }
P
pah100 已提交
172 173
                }
                else {
P
pah100 已提交
174 175 176 177 178 179 180 181 182
                    var min = splitListItem.min;
                    var max = splitListItem.max;
                    min == null && (min = -Infinity);
                    max == null && (max = Infinity);
                    item.interval = [min, max];

                    if (!hasLabel) {
                        item.text = this.formatValueText(min, max);
                    }
P
pah100 已提交
183 184
                }

P
pah100 已提交
185 186 187
                item.visuals = VisualMapping.retrieveVisuals(splitListItem);

                this._pieceList.unshift(item);
P
pah100 已提交
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
            }
        },

        /**
         * @public
         */
        getPieceList: function () {
            return this._pieceList;
        },

        /**
         * @protected
         * @return {boolean}
         */
        useCustomizedSplit: function () {
            var option = this.option;
            return option.splitList && option.splitList.length > 0;
        },

        /**
         * @public
         * @override
         */
        setSelected: function (selected) {
            this.option.selected = selected.slice();
        },

        /**
         * @public
P
pah100 已提交
217
         * @override
P
pah100 已提交
218 219 220
         */
        getValueState: function (value) {
            var pieceList = this._pieceList;
P
pah100 已提交
221
            var targetIndex;
P
pah100 已提交
222 223
            for (var i = 0, len = pieceList.length; i < len; i++) {
                var targetPiece = pieceList[i];
P
pah100 已提交
224 225 226 227 228 229 230 231 232
                if (targetPiece.hasOwnProperty('value')) {
                    if (targetPiece.value === value) {
                        targetIndex = i;
                        break;
                    }
                }
                else if (targetPiece.interval[0] <= value && value <= targetPiece.interval[1]) {
                    targetIndex = i;
                    break;
P
pah100 已提交
233 234
                }
            }
P
pah100 已提交
235 236 237
            return targetIndex != null
                ? (this.option.selected[targetIndex] ? 'inRange' : 'outOfRange')
                : 'outOfRange';
P
pah100 已提交
238 239 240 241 242
        }

    });

});