SunburstSeries.js 4.6 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
        return params;
    },

    defaultOption: {
        zlevel: 0,
        z: 2,

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

        percentPrecision: 2,

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

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

O
Ovilia 已提交
74 75
        // 'rootToNode', 'link', or false
        nodeClick: 'rootToNode',
76

O
Ovilia 已提交
77 78
        renderLabelForZeroData: false,

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

        // Animation type canbe expansion, scale
        animationType: 'expansion',
O
Ovilia 已提交
107
        animationDuration: 1000,
O
Ovilia 已提交
108
        animationDurationUpdate: 500,
O
Ovilia 已提交
109
        animationEasing: 'cubicOut',
O
Ovilia 已提交
110

O
Ovilia 已提交
111 112
        data: [],

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

O
Ovilia 已提交
115 116 117 118 119 120 121 122 123 124 125 126
        /**
         * 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'
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    },

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

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

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

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


/**
 * @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);
}