SunburstSeries.js 4.7 KB
Newer Older
1 2 3
import * as zrUtil from 'zrender/src/core/util';
import SeriesModel from '../../model/Series';
import Tree from '../../data/Tree';
4
import {wrapTreePathInfo} from '../helper/treeHelper';
O
Ovilia 已提交
5

6
export default SeriesModel.extend({
O
Ovilia 已提交
7 8 9

    type: 'series.sunburst',

10 11
    /**
     * @type {module:echarts/data/Tree~Node}
O
Ovilia 已提交
12
     */
13
    _viewRoot: null,
O
Ovilia 已提交
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    getInitialData: function (option, ecModel) {
        // Create a virtual root.
        var root = { name: option.name, children: option.data };

        completeTreeValue(root);

        var levels = option.levels || [];

        // levels = option.levels = setDefault(levels, ecModel);

        var treeOption = {};

        treeOption.levels = levels;

        // 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.
        return Tree.createTree(root, this, treeOption).data;
O
Ovilia 已提交
33 34
    },

35 36
    optionUpdated: function () {
        this.resetViewRoot();
O
Ovilia 已提交
37 38 39 40 41 42
    },

    /*
     * @override
     */
    getDataParams: function (dataIndex) {
43 44 45 46 47
        var params = SeriesModel.prototype.getDataParams.apply(this, arguments);

        var node = this.getData().tree.getNodeByDataIndex(dataIndex);
        params.treePathInfo = wrapTreePathInfo(node, this);

O
Ovilia 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        return params;
    },

    defaultOption: {
        zlevel: 0,
        z: 2,
        legendHoverLink: true,

        hoverAnimation: true,
        // 默认全局居中
        center: ['50%', '50%'],
        radius: [0, '75%'],
        // 默认顺时针
        clockwise: true,
        startAngle: 90,
        // 最小角度改为0
        minAngle: 0,

        percentPrecision: 2,

        // If still show when all data zero.
        stillShowZeroSum: true,

71 72 73 74 75
        // Policy of highlighting pieces when hover on one
        // Valid values: 'none' (for not downplay others), 'descendant',
        // 'ancestor', 'self'
        highlightPolicy: 'descendant',

O
Ovilia 已提交
76 77
        // 'rootToNode', 'link', or false
        nodeClick: 'rootToNode',
78

O
Ovilia 已提交
79 80
        renderLabelForZeroData: false,

O
Ovilia 已提交
81
        label: {
O
Ovilia 已提交
82 83 84
            // could be: 'radial', 'tangential', or 'none'
            rotate: 'radial',
            show: true,
85
            opacity: 1,
O
Ovilia 已提交
86 87 88 89 90 91
            // could be 'inner', 'outside', 'left' or 'right'
            // 'left' is for inner side of inside, and 'right' is for outter
            // side for inside
            position: 'inner',
            padding: 5,
            silent: true,
O
Ovilia 已提交
92 93 94
            emphasis: {}
        },
        itemStyle: {
O
Ovilia 已提交
95 96
            borderWidth: 1,
            borderColor: 'white',
97
            opacity: 1,
98 99 100 101 102 103 104
            emphasis: {},
            highlight: {
                opacity: 1
            },
            downplay: {
                opacity: 0.6
            }
O
Ovilia 已提交
105 106 107 108
        },

        // Animation type canbe expansion, scale
        animationType: 'expansion',
O
Ovilia 已提交
109
        animationDuration: 1000,
O
Ovilia 已提交
110 111
        animationUpdateDuration: 500,
        animationEasing: 'cubicOut',
O
Ovilia 已提交
112

O
Ovilia 已提交
113 114
        data: [],

O
Ovilia 已提交
115 116
        levels: [],

O
Ovilia 已提交
117 118 119 120 121 122 123 124 125 126 127 128
        /**
         * Sort order.
         *
         * Valid values: 'desc', 'asc', null, or callback function.
         * 'desc' and 'asc' for descend and ascendant order;
         * null for not sorting;
         * example of callback function:
         * function(nodeA, nodeB) {
         *     return nodeA.getValue() - nodeB.getValue();
         * }
         */
        sort: 'desc'
129 130 131 132 133 134 135 136 137 138 139 140 141 142
    },

    getViewRoot: function () {
        return this._viewRoot;
    },

    /**
     * @param {module:echarts/data/Tree~Node} [viewRoot]
     */
    resetViewRoot: function (viewRoot) {
        viewRoot
            ? (this._viewRoot = viewRoot)
            : (viewRoot = this._viewRoot);

S
sushuang 已提交
143
        var root = this.getRawData().tree.root;
144 145 146 147 148 149

        if (!viewRoot
            || (viewRoot !== root && !root.contains(viewRoot))
        ) {
            this._viewRoot = root;
        }
O
Ovilia 已提交
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 187 188 189 190


/**
 * @param {Object} dataNode
 */
function completeTreeValue(dataNode) {
    // 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;

    zrUtil.each(dataNode.children, function (child) {

        completeTreeValue(child);

        var childValue = child.value;
        zrUtil.isArray(childValue) && (childValue = childValue[0]);

        sum += childValue;
    });

    var thisValue = dataNode.value;
    if (zrUtil.isArray(thisValue)) {
        thisValue = thisValue[0];
    }

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

    zrUtil.isArray(dataNode.value)
        ? (dataNode.value[0] = thisValue)
        : (dataNode.value = thisValue);
}