TreemapSeries.js 11.0 KB
Newer Older
P
pah100 已提交
1 2 3 4 5
define(function(require) {

    var SeriesModel = require('../../model/Series');
    var Tree = require('../../data/Tree');
    var zrUtil = require('zrender/core/util');
P
pah100 已提交
6
    var Model = require('../../model/Model');
P
pah100 已提交
7 8 9 10
    var formatUtil = require('../../util/format');
    var encodeHTML = formatUtil.encodeHTML;
    var addCommas = formatUtil.addCommas;

P
pah100 已提交
11 12 13 14 15 16 17 18 19 20 21

    return SeriesModel.extend({

        type: 'series.treemap',

        dependencies: ['grid', 'polar'],

        defaultOption: {
            calculable: false,
            clickable: true,
            // center: ['50%', '50%'],             // not supported in ec3.
P
pah100 已提交
22
            // size: ['80%', '80%'],               // deprecated, compatible with ec2.
23 24 25 26
            left: 'center',
            top: 'middle',
            right: null,
            bottom: null,
P
pah100 已提交
27 28
            width: '80%',
            height: '80%',
P
tweak  
pah100 已提交
29 30
            sort: true,                         // Can be null or false or true
                                                // (order by desc default, asc not supported yet (strange effect))
P
pah100 已提交
31 32
            clipWindow: 'origin',                      // 缩放时窗口大小。'origin' or 'fullscreen'
            squareRatio: 0.5 * (1 + Math.sqrt(5)), // golden ratio
P
pah100 已提交
33
            root: null,                             // default: tree root. This feature doesnt work unless node have id.
P
pah100 已提交
34
            visualDimension: 'value',                    // 默认第一个维度。
P
pah100 已提交
35
            zoomToNodeRatio: 0.32 * 0.32,                 // zoom to node时 node占可视区域的面积比例。
P
pah100 已提交
36
            roam: true,
37 38
            animation: true,
            animationDurationUpdate: 1500,
L
Tweak  
lang 已提交
39
            animationEasing: 'quinticInOut',
P
pah100 已提交
40 41
            breadcrumb: {
                show: true,
P
pah100 已提交
42
                height: 22,
L
tweak  
lang 已提交
43 44 45 46
                left: 'center',
                top: 'bottom',
                // right
                // bottom
P
pah100 已提交
47
                emptyItemWidth: 25,                    // 空节点宽度
P
pah100 已提交
48 49
                itemStyle: {
                    normal: {
P
pah100 已提交
50 51 52 53 54 55 56 57
                        color: 'rgba(0,0,0,0.7)', //'#5793f3',
                        borderColor: 'rgba(255,255,255,0.7)',
                        borderWidth: 1,
                        shadowColor: 'rgba(150,150,150,1)',
                        shadowBlur: 3,
                        shadowOffsetX: 0,
                        shadowOffsetY: 0,
                        textStyle: {
L
tweak  
lang 已提交
58
                            color: '#fff'
P
pah100 已提交
59
                        }
P
pah100 已提交
60 61 62 63 64 65 66 67
                    },
                    emphasis: {
                        textStyle: {}
                    }
                }
            },
            label: {
                normal: {
68
                    show: true,
P
pah100 已提交
69
                    position: ['50%', '50%'],      // 可以是 5 '5%' 'insideTopLeft', ...
P
pah100 已提交
70
                    textStyle: {
P
pah100 已提交
71
                        align: 'center',
P
pah100 已提交
72
                        baseline: 'middle',
P
pah100 已提交
73
                        color: '#fff',
P
tweak  
pah100 已提交
74
                        ellipsis: true
P
pah100 已提交
75 76 77 78 79
                    }
                }
            },
            itemStyle: {
                normal: {
P
pah100 已提交
80
                    color: null,         // 各异 如不需,可设为'none'
81 82
                    colorAlpha: null,        // 默认不设置 如不需,可设为'none'
                    colorSaturation: null,        // 默认不设置 如不需,可设为'none'
P
pah100 已提交
83 84
                    borderWidth: 0,
                    gapWidth: 0,
P
pah100 已提交
85
                    borderColor: '#fff',
86
                    borderColorSaturation: null   // 如果设置,则borderColor的设置无效,而是取当前节点计算出的颜色,再经由borderColorSaturation处理。
P
pah100 已提交
87 88 89
                },
                emphasis: {}
            },
90
            color: 'none',    // 为数组,表示同一level的color 选取列表。默认空,在level[0].color中取系统color列表。
91 92
            colorAlpha: null,   // 为数组,表示同一level的color alpha 选取范围。
            colorSaturation: null,   // 为数组,表示同一level的color alpha 选取范围。
P
pah100 已提交
93
            colorMappingBy: 'index', // 'value' or 'index' or 'id'.
P
pah100 已提交
94
            visibleMin: 10,    // If area less than this threshold (unit: pixel^2), node will not be rendered.
95 96 97 98 99 100
                               // Only works when sort is 'asc' or 'desc'.
            childrenVisibleMin: null, // If area of a node less than this threshold (unit: pixel^2),
                                      // grandchildren will not show.
                                      // Why grandchildren? If not grandchildren but children,
                                      // some siblings show children and some not,
                                      // the appearance may be mess and not consistent,
P
pah100 已提交
101
            levels: []         // Each item: {
102
                               //     visibleMin, itemStyle, visualDimension, label
P
pah100 已提交
103 104 105 106 107 108 109
                               // }
        },

        /**
         * @override
         */
        getInitialData: function (option, ecModel) {
P
treemap  
pah100 已提交
110
            var data = option.data || [];
P
pah100 已提交
111
            var rootName = option.name;
112
            rootName == null && (rootName = option.name);
P
pah100 已提交
113

P
pah100 已提交
114
            // Create a virtual root.
P
pah100 已提交
115
            var root = {name: rootName, children: option.data};
116
            var value0 = (data[0] || {}).value;
P
pah100 已提交
117

118
            completeTreeValue(root, zrUtil.isArray(value0) ? value0.length : -1);
P
pah100 已提交
119

P
treemap  
pah100 已提交
120 121
            // FIXME
            // sereis.mergeOption 的 getInitData是否放在merge后,从而能直接获取merege后的结果而非手动判断。
122
            var levels = option.levels || [];
P
treemap  
pah100 已提交
123

P
pah100 已提交
124 125
            levels = option.levels = setDefault(levels, ecModel);

P
pah100 已提交
126 127 128
            // Make sure always a new tree is created when setOption,
            // in TreemapView, we check whether oldTree === newTree
            // to choose mappings approach among old shapes and new shapes.
L
lang 已提交
129
            return Tree.createTree(root, this, levels).data;
P
pah100 已提交
130 131 132 133 134 135 136 137
        },

        /**
         * @public
         */
        getViewRoot: function () {
            var optionRoot = this.option.root;
            var treeRoot = this.getData().tree.root;
P
pah100 已提交
138
            return optionRoot && treeRoot.getNodeById(optionRoot) || treeRoot;
P
pah100 已提交
139 140
        },

P
pah100 已提交
141 142 143 144 145 146 147
        /**
         * @override
         * @param {number} dataIndex
         * @param {boolean} [mutipleSeries=false]
         */
        formatTooltip: function (dataIndex) {
            var data = this.getData();
L
lang 已提交
148
            var value = this.getRawValue(dataIndex);
P
pah100 已提交
149 150
            var formattedValue = zrUtil.isArray(value)
                ? addCommas(value[0]) : addCommas(value);
L
lang 已提交
151
            var name = data.getName(dataIndex);
P
pah100 已提交
152 153 154 155 156 157 158 159 160 161 162

            return encodeHTML(name) + ': ' + formattedValue;
        },

        /**
         * Add tree path to tooltip param
         *
         * @override
         * @param {number} dataIndex
         * @return {Object}
         */
163 164
        getDataParams: function (dataIndex) {
            var params = SeriesModel.prototype.getDataParams.apply(this, arguments);
P
pah100 已提交
165 166 167 168 169 170 171 172 173 174

            var data = this.getData();
            var node = data.tree.getNodeByDataIndex(dataIndex);
            var treePathInfo = params.treePathInfo = [];

            while (node) {
                var nodeDataIndex = node.dataIndex;
                treePathInfo.push({
                    name: node.name,
                    dataIndex: nodeDataIndex,
L
lang 已提交
175
                    value: this.getRawValue(nodeDataIndex)
P
pah100 已提交
176 177 178 179 180 181 182 183 184
                });
                node = node.parentNode;
            }

            treePathInfo.reverse();

            return params;
        },

P
pah100 已提交
185 186
        /**
         * @public
187 188 189 190 191 192
         * @param {Object} layoutInfo {
         *                                x: containerGroup x
         *                                y: containerGroup y
         *                                width: containerGroup width
         *                                height: containerGroup height
         *                            }
P
pah100 已提交
193
         */
194
        setLayoutInfo: function (layoutInfo) {
P
pah100 已提交
195 196
            /**
             * @readOnly
197
             * @type {Object}
P
pah100 已提交
198
             */
199 200
            this.layoutInfo = this.layoutInfo || {};
            zrUtil.extend(this.layoutInfo, layoutInfo);
P
pah100 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        },

        /**
         * @param  {string} id
         * @return {number} index
         */
        mapIdToIndex: function (id) {
            // A feature is implemented:
            // index is monotone increasing with the sequence of
            // input id at the first time.
            // This feature can make sure that each data item and its
            // mapped color have the same index between data list and
            // color list at the beginning, which is useful for user
            // to adjust data-color mapping.

            /**
             * @private
             * @type {Object}
             */
            var idIndexMap = this._idIndexMap;

            if (!idIndexMap) {
                idIndexMap = this._idIndexMap = {};
                /**
                 * @private
                 * @type {number}
                 */
                this._idIndexMapCount = 0;
            }

            var index = idIndexMap[id];
            if (index == null) {
                idIndexMap[id] = index = this._idIndexMapCount++;
            }

            return index;
P
pah100 已提交
237 238 239 240
        }
    });

    /**
P
pah100 已提交
241
     * @param {Object} dataNode
P
pah100 已提交
242
     */
243
    function completeTreeValue(dataNode, arrValueLength) {
P
pah100 已提交
244 245 246 247 248
        // Postorder travel tree.
        // If value of none-leaf node is not set,
        // calculate it by suming up the value of all children.
        var sum = 0;

P
pah100 已提交
249
        zrUtil.each(dataNode.children, function (child) {
P
pah100 已提交
250

251
            completeTreeValue(child, arrValueLength);
P
pah100 已提交
252

P
pah100 已提交
253 254
            var childValue = child.value;
            zrUtil.isArray(childValue) && (childValue = childValue[0]);
P
treemap  
pah100 已提交
255

P
pah100 已提交
256
            sum += childValue;
P
pah100 已提交
257 258
        });

P
pah100 已提交
259 260
        var thisValue = dataNode.value;

261
        if (arrValueLength >= 0) {
P
pah100 已提交
262
            if (!zrUtil.isArray(thisValue)) {
263
                dataNode.value = new Array(arrValueLength);
P
pah100 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277
            }
            else {
                thisValue = thisValue[0];
            }
        }

        if (thisValue == null || isNaN(thisValue)) {
            thisValue = sum;
        }
        // Value should not less than 0.
        if (thisValue < 0) {
            thisValue = 0;
        }

278
        arrValueLength >= 0
P
pah100 已提交
279 280
            ? (dataNode.value[0] = thisValue)
            : (dataNode.value = thisValue);
P
pah100 已提交
281 282
    }

P
pah100 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296
    /**
     * set default to level configuration
     */
    function setDefault(levels, ecModel) {
        var globalColorList = ecModel.get('color');

        if (!globalColorList) {
            return;
        }

        levels = levels || [];
        var hasColorDefine;
        zrUtil.each(levels, function (levelDefine) {
            var model = new Model(levelDefine);
297
            var modelColor = model.get('color');
P
pah100 已提交
298
            if (model.get('itemStyle.normal.color')
299
                || (modelColor && modelColor !== 'none')
P
pah100 已提交
300 301 302 303 304 305 306
            ) {
                hasColorDefine = true;
            }
        });

        if (!hasColorDefine) {
            var level0 = levels[0] || (levels[0] = {});
307
            level0.color = globalColorList.slice();
P
pah100 已提交
308 309 310 311 312
        }

        return levels;
    }

P
pah100 已提交
313
});