List.js 6.2 KB
Newer Older
L
lang 已提交
1 2 3 4
define(function(require) {
    'use strict';

    var zrUtil = require('zrender/core/util');
L
lang 已提交
5
    var Model = require('../model/Model');
L
lang 已提交
6 7 8 9 10 11 12 13

    function createArrayIterWithDepth(maxDepth, properties, cb, context, iterType) {
        // Simple optimization to avoid read the undefined value in properties array
        var nestedProperties = properties.length > 0;
        return function eachAxis(array, depth) {
            if (depth === maxDepth) {
                return zrUtil[iterType](array, cb, context);
            }
L
lang 已提交
14 15 16 17 18 19 20
            else if (array) {
                var property = properties[depth];
                for (var i = 0; i < array.length; i++) {
                    var item = array[i];
                    // Access property of each item
                    if (nestedProperties && property && item) {
                        item = item[property];
L
lang 已提交
21
                    }
L
lang 已提交
22
                    array[i] = eachAxis(item, depth);
L
lang 已提交
23 24 25 26 27
                }
            }
        }
    }

L
lang 已提交
28 29 30 31
    var Entry = Model.extend({

        layout: null,

L
lang 已提交
32
        init: function (option, parentModel, ecModel, dataIndex) {
L
lang 已提交
33 34 35 36 37 38

            /**
             * @type {string}
             * @memeberOf module:echarts/data/List~Entry
             * @public
             */
L
lang 已提交
39 40
            this.name = option.name || '';

L
lang 已提交
41
            this.option = option;
L
lang 已提交
42

L
lang 已提交
43 44 45 46 47
            /**
             * @type {number|Array}
             * @memeberOf module:echarts/data/List~Entry
             * @private
             */
L
lang 已提交
48 49
            this._value = option.value === null ? option : option.value

L
lang 已提交
50 51 52 53 54
            /**
             * @private
             * @readOnly
             */
            this.rawIndex = dataIndex || 0;
L
lang 已提交
55 56 57 58 59 60 61 62 63 64 65
        },

        /**
         * @return {number}
         */
        getX: function () {
            // Use idx as x if data is 1d
            // Usually when xAxis is category axis
            return this.dimension === 1 ? this.rawIndex : this._value[0];
        },

L
lang 已提交
66 67 68
        /**
         * @param {number} x
         */
L
lang 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        setX: function (x) {
            if (this.dimension > 1) {
                this._value[0] = x;
            }
        },

        /**
         * @return {number}
         */
        getY: function () {
            if (this.dimension > 1) {
                return this._value[1];
            }
            else {
                // Value is a single number if data is 1d
                return this._value;
            }
        },

L
lang 已提交
88 89 90
        /**
         * @param {number} y
         */
L
lang 已提交
91 92 93 94 95 96 97 98 99
        setY: function (y) {
            if (this.dimension > 1) {
                this._value[1] = y;
            }
            else {
                this._value = y;
            }
        },

L
lang 已提交
100 101 102
        /**
         * @return {number}
         */
L
lang 已提交
103 104 105 106 107 108
        getZ: function () {
            if (this.dimension > 2) {
                return this._value[2];
            }
        },

L
lang 已提交
109 110 111
        /**
         * @param {number} z
         */
L
lang 已提交
112 113 114 115 116 117
        setZ: function (z) {
            if (this.dimension > 2) {
                this._value[2] = z;
            }
        },

L
lang 已提交
118 119 120
        /**
         * @return {number}
         */
L
lang 已提交
121 122 123 124
        getValue: function () {
            return this._value[this.dimension];
        },

L
lang 已提交
125 126 127
        /**
         * @param {number} value
         */
L
lang 已提交
128 129 130 131 132
        setValue: function (value) {
            this._value[this.dimensino] = value
        }
    });

L
lang 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    function List() {

        this.elements = this.elements || [];

        // Depth and properties is useful in nested Array.
        // For example in eventRiver, data structure is a nested 2d array as following
        // [{evolution: []}, {evolution: []}]
        // In this situation. depth should be 2 and properties should be ['evolution']
        this.depth = 1;

        this.properties = [];
    }

    List.prototype = {

        constructor: List,

L
lang 已提交
150 151
        type: 'list',

L
lang 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        each: function (cb, context) {
            context = context || this;
            if (this.depth > 1) {
                createArrayIterWithDepth(
                    this.depth, this.properties, cb, context, 'each'
                )(this.elements, 0);
            }
            else {
                zrUtil.each(this.elements, cb, context);
            }
        },

        /**
         * In-place filter
         */
L
lang 已提交
167
        filter: function (cb, context) {
L
lang 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181
            context = context || this;
            if (this.depth > 1) {
                createArrayIterWithDepth(
                    this.depth, this.properties, cb, context, 'filter'
                )(this.elements, 0);
            }
            else {
                this.elements = zrUtil.filter(this.elements, cb, context);
            }
        },

        /**
         * In-place map
         */
L
lang 已提交
182
        map: function (cb, context) {
L
lang 已提交
183 184 185 186 187 188 189 190 191 192 193
            context = context || this;
            if (this.depth > 1) {
                createArrayIterWithDepth(
                    this.depth, this.properties, cb, context, 'map'
                )(this.elements, 0);
            }
            else {
                this.elements = zrUtil.map(this.elements, cb, context);
            }
        },

L
lang 已提交
194
        getItemByName: function (name) {
L
lang 已提交
195 196 197 198 199 200 201
            // var elements = this.elements;
            // for (var i = 0; i < elements.length; i++) {
            //     if (elements[i].name === name) {
            //         return elements[i];
            //     }
            // }
            // TODO
L
lang 已提交
202 203
        },

L
lang 已提交
204
        clone: function () {
L
lang 已提交
205
            // Clone
L
lang 已提交
206 207 208
        }
    };

L
lang 已提交
209 210 211 212
    zrUtil.each(['X', 'Y', 'Z', 'Value'], function (name) {
        zrUtil.each(['each', 'map', 'filter'], function (iterType) {
            List.prototype[iterType + name] = function (cb, context) {
                this[iterType](function (item, idx) {
L
lang 已提交
213
                    return cb && cb.call(context || this, item['get' + name](idx));
L
lang 已提交
214 215 216 217 218
                }, context);
            };
        });
    });

L
lang 已提交
219
    List.fromArray = function (data, dimension, parentModel, ecModel) {
L
lang 已提交
220 221
        var list = new List();
        // Normalize data
L
lang 已提交
222
        list.elements = zrUtil.map(data, function (dataItem, index) {
L
lang 已提交
223
            var entry = new Entry(dataItem, parentModel, ecModel, index);
L
lang 已提交
224
            entry.dimension = dimension;
L
lang 已提交
225
            return entry;
L
lang 已提交
226 227
        });
        return list;
L
lang 已提交
228 229 230
    };

    List.Entry = Entry;
L
lang 已提交
231 232 233

    return List;
});