TreemapSeries.js 9.9 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.
P
pah100 已提交
23 24 25 26 27 28
            x: 'center',
            y: 'middle',
            x2: null,
            y2: null,
            width: '80%',
            height: '80%',
29
            sort: true,                         // Can be null or true or 'asc' or 'desc'
P
pah100 已提交
30 31 32
            clipWindow: 'origin',                      // 缩放时窗口大小。'origin' or 'fullscreen'
            squareRatio: 0.5 * (1 + Math.sqrt(5)), // golden ratio
            root: '',
P
pah100 已提交
33
            visualDimension: 'value',                    // 默认第一个维度。
P
pah100 已提交
34
            zoomToNodeRatio: 0.32 * 0.32,                 // zoom to node时 node占可视区域的面积比例。
P
pah100 已提交
35
            roam: true,
36 37 38
            animation: true,
            animationDurationUpdate: 1500,
            animationEasing: 'quadraticInOut',
P
pah100 已提交
39 40
            breadcrumb: {
                show: true,
P
pah100 已提交
41
                height: 22,
P
pah100 已提交
42
                x: 'center',
P
pah100 已提交
43 44
                y: 'bottom',
                emptyItemWidth: 25,                    // 空节点宽度
P
pah100 已提交
45 46
                itemStyle: {
                    normal: {
P
pah100 已提交
47 48 49 50 51 52 53 54 55 56
                        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: {
                            color: '#fff',
                            fontFamily: 'Arial',
P
pah100 已提交
57
                            fontSize: 12,
P
pah100 已提交
58 59
                            fontWeight: 'normal'
                        }
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
                        color: '#fff',
P
pah100 已提交
73 74 75 76 77 78 79 80 81
                        fontFamily: 'Arial',
                        fontSize: 13,
                        fontStyle: 'normal',
                        fontWeight: 'normal'
                    }
                }
            },
            itemStyle: {
                normal: {
P
pah100 已提交
82 83 84
                    color: null,         // 各异 如不需,可设为'none'
                    colorA: null,        // 默认不设置 如不需,可设为'none'
                    colorS: null,        // 默认不设置 如不需,可设为'none'
P
pah100 已提交
85 86
                    borderWidth: 0,
                    gapWidth: 0,
P
pah100 已提交
87 88
                    borderColor: '#fff',
                    borderColorS: null   // 如果设置,则borderColor的设置无效,而是取当前节点计算出的颜色,再经由borderColorS处理。
P
pah100 已提交
89 90 91
                },
                emphasis: {}
            },
92 93 94 95
            color: 'none',    // 为数组,表示同一level的color 选取列表。默认空,在level[0].color中取系统color列表。
            colorA: null,   // 为数组,表示同一level的color alpha 选取范围。
            colorS: null,   // 为数组,表示同一level的color alpha 选取范围。
            colorMapping: 'byIndex', // 'byIndex' or 'byValue'
P
pah100 已提交
96
            visibleMin: 10,    // If area less than this threshold (unit: pixel^2), node will not be rendered.
97 98 99 100 101 102
                               // 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 已提交
103
            levels: []         // Each item: {
104
                               //     visibleMin, itemStyle, visualDimension, label
P
pah100 已提交
105 106 107 108 109 110 111
                               // }
        },

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

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

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

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

P
pah100 已提交
126 127
            levels = option.levels = setDefault(levels, ecModel);

P
pah100 已提交
128 129 130
            // 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 已提交
131
            return Tree.createTree(root, this, levels).data;
P
pah100 已提交
132 133 134 135 136 137 138 139 140
        },

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

P
pah100 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        /**
         * @override
         * @param {number} dataIndex
         * @param {boolean} [mutipleSeries=false]
         */
        formatTooltip: function (dataIndex) {
            var data = this.getData();
            var value = data.getRawValue(dataIndex);
            var formattedValue = zrUtil.isArray(value)
                ? addCommas(value[0]) : addCommas(value);
            var name = data.getName(dataIndex, true);

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

        /**
         * Add tree path to tooltip param
         *
         * @override
         * @param {number} dataIndex
         * @return {Object}
         */
        getFormatParams: function (dataIndex) {
            var params = SeriesModel.prototype.getFormatParams.apply(this, arguments);

            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,
                    value: data.getRawValue(nodeDataIndex)
                });
                node = node.parentNode;
            }

            treePathInfo.reverse();

            return params;
        },

P
pah100 已提交
187 188
        /**
         * @public
189 190 191 192 193 194
         * @param {Object} layoutInfo {
         *                                x: containerGroup x
         *                                y: containerGroup y
         *                                width: containerGroup width
         *                                height: containerGroup height
         *                            }
P
pah100 已提交
195
         */
196
        setLayoutInfo: function (layoutInfo) {
P
pah100 已提交
197 198
            /**
             * @readOnly
199
             * @type {Object}
P
pah100 已提交
200
             */
201 202
            this.layoutInfo = this.layoutInfo || {};
            zrUtil.extend(this.layoutInfo, layoutInfo);
P
pah100 已提交
203 204 205 206 207
        }

    });

    /**
P
pah100 已提交
208
     * @param {Object} dataNode
P
pah100 已提交
209
     */
210
    function completeTreeValue(dataNode, arrValueLength) {
P
pah100 已提交
211 212 213 214 215
        // 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 已提交
216
        zrUtil.each(dataNode.children, function (child) {
P
pah100 已提交
217

218
            completeTreeValue(child, arrValueLength);
P
pah100 已提交
219

P
pah100 已提交
220 221
            var childValue = child.value;
            zrUtil.isArray(childValue) && (childValue = childValue[0]);
P
treemap  
pah100 已提交
222

P
pah100 已提交
223
            sum += childValue;
P
pah100 已提交
224 225
        });

P
pah100 已提交
226 227
        var thisValue = dataNode.value;

228
        if (arrValueLength >= 0) {
P
pah100 已提交
229
            if (!zrUtil.isArray(thisValue)) {
230
                dataNode.value = new Array(arrValueLength);
P
pah100 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244
            }
            else {
                thisValue = thisValue[0];
            }
        }

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

245
        arrValueLength >= 0
P
pah100 已提交
246 247
            ? (dataNode.value[0] = thisValue)
            : (dataNode.value = thisValue);
P
pah100 已提交
248 249
    }

P
pah100 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263
    /**
     * 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);
264
            var modelColor = model.get('color');
P
pah100 已提交
265
            if (model.get('itemStyle.normal.color')
266
                || (modelColor && modelColor !== 'none')
P
pah100 已提交
267 268 269 270 271 272 273
            ) {
                hasColorDefine = true;
            }
        });

        if (!hasColorDefine) {
            var level0 = levels[0] || (levels[0] = {});
274
            level0.color = globalColorList.slice();
P
pah100 已提交
275 276 277 278 279
        }

        return levels;
    }

P
pah100 已提交
280
});