SunburstSeries.js 4.5 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',

76 77 78
        // 'zoomToNode', 'link', or false
        nodeClick: 'zoomToNode',

O
Ovilia 已提交
79 80
        label: {
            normal: {
81 82
                // could be: 'radial', 'tangential', or 'none'
                rotate: 'radial',
O
Ovilia 已提交
83
                show: true,
O
Ovilia 已提交
84 85 86 87 88 89
                // 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 已提交
90 91 92 93 94
            },
            emphasis: {}
        },
        itemStyle: {
            normal: {
95 96
                borderWidth: 1,
                borderColor: 'white'
O
Ovilia 已提交
97
            },
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 110 111
        animationDuration: 1000,
        animationUpdateDuration: 300,
        animationEasing: 'sinusoidalInOut',
O
Ovilia 已提交
112

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

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

        // null for not sorting,
        // 'desc' and 'asc' for descend and ascendant order
        sortOrder: 'desc'
120 121 122 123 124 125 126 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);

        var root = this.getData().tree.root;

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


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