SunburstView.js 4.4 KB
Newer Older
O
Ovilia 已提交
1 2 3 4
import * as zrUtil from 'zrender/src/core/util';
import * as graphic from '../../util/graphic';
import ChartView from '../../view/Chart';
import SunburstPiece from './SunburstPiece';
5
import DataDiffer from '../../data/DataDiffer';
O
Ovilia 已提交
6 7 8 9 10 11 12

/**
 * @param {module:echarts/model/Series} seriesModel
 * @param {boolean} hasAnimation
 * @inner
 */
function updateDataSelected(uid, seriesModel, hasAnimation, api) {
13
    var treeRoot = seriesModel.getData();
O
Ovilia 已提交
14
    var dataIndex = this.dataIndex;
15
    var name = treeRoot.getName(dataIndex);
O
Ovilia 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

    api.dispatchAction({
        type: 'SunburstToggleSelect',
        from: uid,
        name: name,
        seriesId: seriesModel.id
    });
}

var SunburstView = ChartView.extend({

    type: 'sunburst',

    init: function () {
        var sectorGroup = new graphic.Group();
        this._sectorGroup = sectorGroup;
    },

    render: function (seriesModel, ecModel, api, payload) {
        if (payload && (payload.from === this.uid)) {
            return;
        }

39
        var treeRoot = seriesModel.getData().tree.root;
O
Ovilia 已提交
40 41 42 43 44 45 46
        var oldData = this._data;
        var group = this.group;

        var hasAnimation = ecModel.get('animation');
        var isFirstRender = !oldData;
        var animationType = seriesModel.get('animationType');

47 48 49 50 51
        // var onSectorClick = zrUtil.curry(
        //     updateDataSelected, this.uid, seriesModel, hasAnimation, api
        // );

        treeRoot.eachNode(function (node) {
O
Ovilia 已提交
52
            if (node !== treeRoot) {
O
Ovilia 已提交
53
                var piece = new SunburstPiece(node, seriesModel, ecModel);
54
                node.piece = piece;
O
Ovilia 已提交
55 56
                group.add(piece);
            }
57
        });
O
Ovilia 已提交
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        // treeRoot.diff(oldData)
        //     .add(function (idx) {
        //         var sunburstPiece = new SunburstPiece(treeRoot, idx);
        //         // Default expansion animation
        //         if (isFirstRender && animationType !== 'scale') {
        //             sunburstPiece.eachChild(function (child) {
        //                 child.stopAnimation(true);
        //             });
        //         }

        //         treeRoot.setItemGraphicEl(idx, sunburstPiece);

        //         group.add(sunburstPiece);
        //     })
        //     .update(function (newIdx, oldIdx) {
        //         var sunburstPiece = oldData.getItemGraphicEl(oldIdx);

        //         sunburstPiece.updateData(treeRoot, newIdx);

        //         group.add(sunburstPiece);
        //         treeRoot.setItemGraphicEl(newIdx, sunburstPiece);
        //     })
        //     .remove(function (idx) {
        //         var sunburstPiece = oldData.getItemGraphicEl(idx);
        //         group.remove(sunburstPiece);
        //     })
        //     .execute();

        // if (
        //     hasAnimation && isFirstRender && treeRoot.count() > 0
        //     // Default expansion animation
        //     && animationType !== 'scale'
        // ) {
        //     var shape = treeRoot.getLayout();
        //     var r = Math.max(api.getWidth(), api.getHeight()) / 2;

        //     var removeClipPath = zrUtil.bind(group.removeClipPath, group);
        //     group.setClipPath(this._createClipPath(
        //         shape.cx, shape.cy, r, shape.startAngle, shape.clockwise, removeClipPath, seriesModel
        //     ));
        // }

        this._data = treeRoot;
O
Ovilia 已提交
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
    },

    dispose: function () {},

    _createClipPath: function (
        cx, cy, r, startAngle, clockwise, cb, seriesModel
    ) {
        var clipPath = new graphic.Sector({
            shape: {
                cx: cx,
                cy: cy,
                r0: 0,
                r: r,
                startAngle: startAngle,
                endAngle: startAngle,
                clockwise: clockwise
            }
        });

        graphic.initProps(clipPath, {
            shape: {
                endAngle: startAngle + (clockwise ? 1 : -1) * Math.PI * 2
            }
        }, seriesModel, cb);

        return clipPath;
    },

    /**
     * @implement
     */
    containPoint: function (point, seriesModel) {
134 135
        var treeRoot = seriesModel.getData();
        var itemLayout = treeRoot.getItemLayout(0);
O
Ovilia 已提交
136 137 138 139 140 141 142 143 144 145 146
        if (itemLayout) {
            var dx = point[0] - itemLayout.cx;
            var dy = point[1] - itemLayout.cy;
            var radius = Math.sqrt(dx * dx + dy * dy);
            return radius <= itemLayout.r && radius >= itemLayout.r0;
        }
    }

});

export default SunburstView;