ContinuousModel.js 5.9 KB
Newer Older
P
pah100 已提交
1 2 3 4 5
/**
 * @file Data zoom model
 */
define(function(require) {

P
pah100 已提交
6
    var ControllerModel = require('./ControllerModel');
P
pah100 已提交
7
    var zrUtil = require('zrender/core/util');
P
pah100 已提交
8
    var numberUtil = require('../../util/number');
P
pah100 已提交
9 10 11

    // Constant
    var DEFAULT_BAR_BOUND = [20, 140];
P
pah100 已提交
12

P
pah100 已提交
13
    var ContinuousModel = ControllerModel.extend({
P
pah100 已提交
14

15
        type: 'visualMap.continuous',
P
pah100 已提交
16 17 18 19 20

        /**
         * @protected
         */
        defaultOption: {
P
pah100 已提交
21 22 23
            align: 'auto',          // 'auto', 'left', 'right', 'top', 'bottom'
            calculable: false,      // This prop effect default component type determine,
                                    // See echarts/component/visualMap/typeDefaulter.
P
pah100 已提交
24 25 26
            range: null,            // selected range. In default case `range` is [min, max]
                                    // and can auto change along with modification of min max,
                                    // util use specifid a range.
P
pah100 已提交
27 28 29
            realtime: true,         // Whether realtime update.
            itemHeight: null,       // The length of the range control edge.
            itemWidth: null,        // The length of the other side.
P
pah100 已提交
30 31 32
            hoverLink: true,        // Enable hover highlight.
            hoverLinkDataSize: null,// The size of hovered data.
            hoverLinkOnHandle: true // Whether trigger hoverLink when hover handle.
P
pah100 已提交
33 34 35 36 37
        },

        /**
         * @override
         */
P
pah100 已提交
38 39
        optionUpdated: function (newOption, isInit) {
            ContinuousModel.superApply(this, 'optionUpdated', arguments);
P
pah100 已提交
40

P
pah100 已提交
41
            this.resetTargetSeries();
P
pah100 已提交
42 43
            this.resetExtent();

P
pah100 已提交
44 45 46 47
            this.resetVisual('controller', this.controllerVisuals, fillVisualOption);
            this.resetVisual('target', this.targetVisuals, fillVisualOption);

            function fillVisualOption(mappingOption) {
P
pah100 已提交
48
                mappingOption.mappingMethod = 'linear';
P
pah100 已提交
49 50
                mappingOption.dataExtent = this.getExtent();
            }
P
pah100 已提交
51 52 53 54

            this._resetRange();
        },

P
pah100 已提交
55 56 57 58 59
        /**
         * @protected
         * @override
         */
        resetItemSize: function () {
P
pah100 已提交
60
            ContinuousModel.superApply(this, 'resetItemSize', arguments);
P
pah100 已提交
61 62 63 64 65 66 67 68 69

            var itemSize = this.itemSize;

            this._orient === 'horizontal' && itemSize.reverse();

            (itemSize[0] == null || isNaN(itemSize[0])) && (itemSize[0] = DEFAULT_BAR_BOUND[0]);
            (itemSize[1] == null || isNaN(itemSize[1])) && (itemSize[1] = DEFAULT_BAR_BOUND[1]);
        },

P
pah100 已提交
70 71 72 73 74 75
        /**
         * @private
         */
        _resetRange: function () {
            var dataExtent = this.getExtent();
            var range = this.option.range;
P
pah100 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88

            if (!range || range.auto) {
                // `range` should always be array (so we dont use other
                // value like 'auto') for user-friend. (consider getOption).
                dataExtent.auto = 1;
                this.option.range = dataExtent;
            }
            else if (zrUtil.isArray(range)) {
                if (range[0] > range[1]) {
                    range.reverse();
                }
                range[0] = Math.max(range[0], dataExtent[0]);
                range[1] = Math.min(range[1], dataExtent[1]);
P
pah100 已提交
89 90 91
            }
        },

P
pah100 已提交
92 93 94 95 96
        /**
         * @protected
         * @override
         */
        completeVisualOption: function () {
P
pah100 已提交
97
            ControllerModel.prototype.completeVisualOption.apply(this, arguments);
P
pah100 已提交
98 99 100 101 102 103 104 105 106

            zrUtil.each(this.stateList, function (state) {
                var symbolSize = this.option.controller[state].symbolSize;
                if (symbolSize && symbolSize[0] !== symbolSize[1]) {
                    symbolSize[0] = 0; // For good looking.
                }
            }, this);
        },

P
pah100 已提交
107 108 109 110 111 112 113
        /**
         * @public
         * @override
         */
        setSelected: function (selected) {
            this.option.range = selected.slice();
            this._resetRange();
P
pah100 已提交
114 115
        },

P
pah100 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        /**
         * @public
         */
        getSelected: function () {
            var dataExtent = this.getExtent();

            var dataInterval = numberUtil.asc(
                (this.get('range') || []).slice()
            );

            // Clamp
            dataInterval[0] > dataExtent[1] && (dataInterval[0] = dataExtent[1]);
            dataInterval[1] > dataExtent[1] && (dataInterval[1] = dataExtent[1]);
            dataInterval[0] < dataExtent[0] && (dataInterval[0] = dataExtent[0]);
            dataInterval[1] < dataExtent[0] && (dataInterval[1] = dataExtent[0]);

            return dataInterval;
        },

P
pah100 已提交
135 136 137 138 139
        /**
         * @public
         * @override
         */
        getValueState: function (value) {
P
pah100 已提交
140
            var range = this.option.range;
141 142 143 144 145 146 147 148
            var dataExtent = this.getExtent();

            // When range[0] === dataExtent[0], any value larger than dataExtent[0] maps to 'inRange'.
            // range[1] is processed likewise.
            return (
                (range[0] <= dataExtent[0] || range[0] <= value)
                && (range[1] >= dataExtent[1] || value <= range[1])
            ) ? 'inRange' : 'outOfRange';
P
pah100 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        },

        /**
         * @public
         * @params {Array.<number>} range target value: range[0] <= value && value <= range[1]
         * @return {Array.<Object>} [{seriesId, dataIndices: <Array.<number>>}, ...]
         */
        findTargetDataIndices: function (range) {
            var result = [];

            this.eachTargetSeries(function (seriesModel) {
                var dataIndices = [];
                var data = seriesModel.getData();

                data.each(this.getDataDimension(data), function (value, dataIndex) {
                    range[0] <= value && value <= range[1] && dataIndices.push(dataIndex);
                }, true, this);

P
pah100 已提交
167
                result.push({seriesId: seriesModel.id, dataIndex: dataIndices});
P
pah100 已提交
168 169 170
            }, this);

            return result;
P
pah100 已提交
171 172 173 174
        }

    });

P
pah100 已提交
175 176
    return ContinuousModel;

P
pah100 已提交
177
});