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

    var zrUtil = require('zrender/core/util');
    var graphic = require('../../util/graphic');
    var layout = require('../../util/layout');
P
pah100 已提交
6
    var DataDiffer = require('../../data/DataDiffer');
P
pah100 已提交
7 8 9 10 11 12 13 14 15 16 17 18
    var Group = graphic.Group;
    var Rect = graphic.Rect;

    return require('../../echarts').extendChartView({

        type: 'treemap',

        /**
         * @override
         */
        init: function () {

P
pah100 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
            /**
             * @private
             * @type {module:zrender/container/Group}
             */
            this._drawGroup;

            /**
             * @private
             * @type {Object.<string, Array.<module:zrender/container/Group>>}
             */
            this._storage;

            /**
             * @private
             * @type {module:echarts/data/Tree}
             */
            this._oldTree;
P
pah100 已提交
36 37 38 39 40 41
        },

        /**
         * @override
         */
        render: function (seriesModel, ecModel, api) {
P
pah100 已提交
42
            this.seriesModel = seriesModel;
P
pah100 已提交
43

P
pah100 已提交
44 45 46 47 48 49 50 51
            var drawGroup = this._drawGroup;

            if (!drawGroup) {
                drawGroup = this._drawGroup = new Group({
                    onclick: zrUtil.bind(this._onClick, this)
                });
                this.group.add(drawGroup);
            }
P
pah100 已提交
52

P
pah100 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            var thisTree = seriesModel.getData().tree;
            var oldTree = this._oldTree;
            this._storage = [];

            var thisStorage = {nodeGroup: [], background: [], content: []};
            var oldStorage = this._storage;
            var renderNode = zrUtil.bind(this._renderNode, this);
            var viewRoot = seriesModel.getViewRoot();

            dualTravel(
                [thisTree.root],
                oldTree ? [oldTree.root] : [],
                drawGroup,
                viewRoot === thisTree.root
            );

            this._oldTree = thisTree;
            this._storage = thisStorage;
P
pah100 已提交
71 72

            layout.positionGroup(
P
pah100 已提交
73
                drawGroup,
P
pah100 已提交
74 75 76 77 78 79 80 81 82 83 84
                {
                    x: seriesModel.get('x'),
                    y: seriesModel.get('y'),
                    x2: seriesModel.get('x2'),
                    y2: seriesModel.get('y2')
                },
                {
                    width: api.getWidth(),
                    height: api.getHeight()
                }
            );
P
pah100 已提交
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 137 138 139 140

            function dualTravel(thisViewChildren, oldViewChildren, parentGroup, inView) {
                // When update, 'this' and 'old' are in the same tree.
                if (thisViewChildren === oldViewChildren
                    || !thisViewChildren.length
                    || !oldViewChildren.length
                ) {
                    zrUtil.each(thisViewChildren, function (child, index) {
                        processNode(index, index);
                    });
                }
                // When setOption, data changed, use data differ.
                else {
                    // Diff hierarchically (diff only in each subtree, but not whole).
                    // because, consistency of view is important.
                    (new DataDiffer(
                        oldViewChildren,
                        thisViewChildren,
                        function (node) {
                            // Identify by name or raw index.
                            return node.name != null ? node.name : node.getRawIndex();
                        }
                    ))
                    .add(function (newIndex) {
                        processNode(newIndex);
                    })
                    .update(function (newIndex, oldIndex) {
                        processNode(newIndex, oldIndex);
                    })
                    .remove(function (oldIndex) {
                        processNode(null, oldIndex);
                    });
                }

                function processNode(newIndex, oldIndex) {
                    var thisNode = newIndex != null ? thisViewChildren[newIndex] : null;
                    var oldNode = oldIndex != null ? oldViewChildren[oldIndex] : null;

                    // Whether under viewRoot.
                    var subInView = inView || thisNode === viewRoot;
                    // If not under viewRoot, only remove.
                    if (!subInView) {
                        thisNode = null;
                    }

                    var group = renderNode(
                        thisNode, oldNode, parentGroup, thisStorage, oldStorage
                    );
                    dualTravel(
                        thisNode && thisNode.viewChildren || [],
                        oldNode && oldNode.viewChildren || [],
                        group,
                        subInView
                    );
                }
            }
P
pah100 已提交
141 142 143 144 145
        },

        /**
         * @private
         */
P
pah100 已提交
146 147 148 149 150 151 152 153 154 155 156 157
        _renderNode: function (thisNode, oldNode, parentGroup, thisStorage, oldStorage) {
            // FIXME
            // 考虑 viewRoot ??????????????
            var thisRawIndex = thisNode && thisNode.getRawIndex();
            var oldRawIndex = oldNode && oldNode.getRawIndex();

            if (!thisNode) {
                oldNode && oldStorage && parentGroup.remove(oldStorage.nodeGroup[oldRawIndex]);
                return;
            }

            var layout = thisNode.getLayout();
P
pah100 已提交
158 159 160
            var thisWidth = layout.width;
            var thisHeight = layout.height;

P
pah100 已提交
161
            var group = makeGraphic('nodeGroup', Group);
P
pah100 已提交
162
            group.position = [layout.x, layout.y];
P
pah100 已提交
163 164 165 166
            parentGroup.add(group);

            var itemStyleModel = thisNode.getModel('itemStyle.normal');
            var borderColor = itemStyleModel.get('borderColor') || itemStyleModel.get('gapColor');
P
pah100 已提交
167 168

            // Background
P
pah100 已提交
169 170 171 172 173 174
            var background = makeGraphic('background', Rect);
            background.setShape({x: 0, y: 0, width: thisWidth, height: thisHeight});
            background.setStyle({fill: borderColor});
            group.add(background);

            var thisViewChildren = thisNode.viewChildren;
P
pah100 已提交
175 176

            // No children, render content.
P
pah100 已提交
177
            if (!thisViewChildren || !thisViewChildren.length) {
P
pah100 已提交
178
                var borderWidth = layout.borderWidth;
P
pah100 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191

                var content = makeGraphic('content', Rect);
                content.setShape({
                    x: borderWidth,
                    y: borderWidth,
                    width: Math.max(thisWidth - 2 * borderWidth, 0),
                    height: Math.max(thisHeight - 2 * borderWidth, 0)
                });
                content.setStyle({
                    fill: thisNode.getVisual('color', true)
                });

                group.add(content);
P
pah100 已提交
192
            }
P
pah100 已提交
193
            // Remove old content for children rendering.
P
pah100 已提交
194
            else {
P
pah100 已提交
195 196 197
                var content = (oldNode && oldStorage)
                    ? oldStorage.content[oldRawIndex] : null;
                content && group.remove(content);
P
pah100 已提交
198 199
            }

P
pah100 已提交
200 201 202 203 204 205 206
            return group;

            function makeGraphic(storage, Ctor) {
                return thisStorage[storage][thisRawIndex] = (oldNode && oldStorage)
                    ? oldStorage[storage][oldRawIndex]
                    : new Ctor({});
            }
P
pah100 已提交
207 208 209 210 211 212
        },

        /**
         * @override
         */
        remove: function () {
P
pah100 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
            this.group.removeAll();
            this._drawGroup = null;
            this._storage = null;
            this._oldTree = null;
        },

        /**
         * @private
         */
        _onClick: function (e) {

            var zoomStep = this.seriesModel.get('zoomToNode');

            var targetInfo = this.findTarget(e.offsetX, e.offsetY);

            if (!targetInfo) {
                return;
            }

            if (zoomStep <= 0) {
                return;
            }

            this.api.dispatch({
                type: 'zoomToNode',
                from: this.uid,
                targetNode: ''
            });
        },

        /**
         * @public
         * @return {Object} info If not found, return undefined;
         * @return {number} info.node Target node.
         * @return {number} info.offsetX x refer to target node.
         * @return {number} info.offsetY y refer to target node.
         */
        findTarget: function (x, y) {
            var targetInfo;
            var viewRoot = this.seriesModel.getViewRoot();

            viewRoot.eachNode({attr: 'viewChildren', order: 'preorder'}, function (node) {
                var nodeGroup = this._storage.nodeGroup[node.getRawIndex()];
                var point = nodeGroup.transformCoordToLocal(x, y);
                if (nodeGroup.getBoundingRect().contain(point[0], point[1])) {
                    targetInfo = {node: node, offsetX: point[0], offsetY: point[1]};
                }
                else {
                    return false; // Suppress visit subtree.
                }
            }, this);

            return targetInfo;
P
pah100 已提交
266 267 268 269
        }

    });
});