DataSymbol.js 6.1 KB
Newer Older
L
lang 已提交
1 2 3 4 5
define(function (require) {

    var zrUtil = require('zrender/core/util');
    var Group = require('zrender/container/Group');
    var symbolCreators = require('../../util/symbol');
L
lang 已提交
6
    var graphic = require('../../util/graphic');
L
lang 已提交
7

L
lang 已提交
8
    function createSymbol(data, idx, enableAnimation) {
L
lang 已提交
9
        var point = data.getItemLayout(idx);
L
lang 已提交
10
        var color = data.getItemVisual(idx, 'color');
L
lang 已提交
11

L
lang 已提交
12 13
        var symbolSize = data.getItemVisual(idx, 'symbolSize');
        var symbolType = data.getItemVisual(idx, 'symbol') || 'circle';
L
lang 已提交
14

L
lang 已提交
15 16 17 18
        if (!symbolType || symbolType === 'none') {
            return;
        }

L
lang 已提交
19 20 21 22 23
        var symbolEl = symbolCreators.createSymbol(
            symbolType, -0.5, -0.5, 1, 1, color
        );

        symbolEl.position = point;
L
lang 已提交
24

L
lang 已提交
25 26
        symbolEl.z2 = 100;

L
lang 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        if (enableAnimation) {

            symbolEl.scale = [0.1, 0.1];

            symbolEl.animateTo({
                scale: [symbolSize, symbolSize]
            }, 500);

            // symbolEl
            //     .on('mouseover', function () {
            //         this.animateTo({
            //             scale: [symbolSize * 1.4, symbolSize * 1.4]
            //         }, 400, 'elasticOut');
            //     })
            //     .on('mouseout', function () {
            //         this.animateTo({
            //             scale: [symbolSize, symbolSize]
            //         }, 400, 'elasticOut');
            //     });
        }
        else {
            symbolEl.scale = [symbolSize, symbolSize];
        }

        return symbolEl;
    }

L
lang 已提交
54 55 56 57 58 59 60 61 62 63 64
    function DataSymbol() {

        this.group = new Group();
    }

    DataSymbol.prototype = {

        getData: function () {
            return this._data;
        },

L
lang 已提交
65
        updateData: function (data, enableAnimation) {
L
lang 已提交
66 67

            var group = this.group;
L
lang 已提交
68
            var oldData = this._data;
L
lang 已提交
69

L
lang 已提交
70 71
            data.diff(oldData)
                .add(function (newIdx) {
L
lang 已提交
72 73
                    // 空数据
                    // TODO
L
lang 已提交
74
                    if (!data.hasValue(newIdx)) {
75 76
                        return;
                    }
L
lang 已提交
77

78 79 80
                    var symbolEl = createSymbol(
                        data, newIdx, enableAnimation
                    );
L
lang 已提交
81

L
lang 已提交
82 83
                    if (symbolEl) {
                        data.setItemGraphicEl(newIdx, symbolEl);
L
lang 已提交
84

L
lang 已提交
85 86
                        group.add(symbolEl);
                    }
L
lang 已提交
87
                })
L
lang 已提交
88
                .update(function (newIdx, oldIdx) {
L
lang 已提交
89 90
                    var el = oldData.getItemGraphicEl(oldIdx);

91
                    // Empty data
L
lang 已提交
92
                    if (!data.hasValue(newIdx)) {
93
                        group.remove(el);
94 95
                        return;
                    }
L
lang 已提交
96

L
lang 已提交
97
                    var symbolSize = data.getItemVisual(newIdx, 'symbolSize');
L
lang 已提交
98
                    var point = data.getItemLayout(newIdx);
L
lang 已提交
99

100 101 102
                    // Symbol changed
                    if (oldData.getItemVisual(newIdx, 'symbol') !== data.getItemVisual(oldIdx, 'symbol')) {
                        // Remove the old one
L
lang 已提交
103
                        el && group.remove(el);
104 105
                        el = createSymbol(data, newIdx, enableAnimation);
                    }
L
lang 已提交
106 107 108 109
                    // Disable symbol by setting `symbol: 'none'`
                    if (!el) {
                        return;
                    }
110

P
pah100 已提交
111 112 113 114 115 116
                    // Color changed
                    var newColor = data.getItemVisual(newIdx, 'color');
                    if (oldData.getItemVisual(newIdx, 'color') !== newColor) {
                        el.setStyle('fill', newColor);
                    }

L
lang 已提交
117 118 119 120
                    // TODO Merge animateTo and attr methods into one
                    if (enableAnimation) {
                        el.animateTo({
                            scale: [symbolSize, symbolSize],
L
lang 已提交
121
                            position: point
L
lang 已提交
122 123 124 125 126
                        }, 300, 'cubicOut');
                    }
                    else {
                        el.attr({
                            scale: [symbolSize, symbolSize],
L
lang 已提交
127
                            position: point.slice()
L
lang 已提交
128 129 130
                        });
                    }

L
lang 已提交
131
                    data.setItemGraphicEl(newIdx, el);
L
lang 已提交
132 133 134 135

                    // Add back
                    group.add(el);
                })
L
lang 已提交
136 137 138 139 140 141
                .remove(function (oldIdx) {
                    var el = oldData.getItemGraphicEl(oldIdx);
                    if (enableAnimation) {
                        el.animateTo({
                            scale: [0, 0]
                        }, 200, 'cubicOut', function () {
142
                            group.remove(el);
L
lang 已提交
143 144 145
                        });
                    }
                    else {
146
                        // console.log(oldIdx);
L
lang 已提交
147
                        group.remove(el);
L
lang 已提交
148 149 150 151 152
                    }
                })
                .execute();

            // Update common properties
L
lang 已提交
153
            data.eachItemGraphicEl(function (el, idx) {
L
lang 已提交
154

L
lang 已提交
155
                var itemModel = data.getItemModel(idx);
L
lang 已提交
156 157 158 159
                zrUtil.extend(
                    el.style,
                    itemModel.getModel('itemStyle.normal').getItemStyle(['color'])
                );
L
lang 已提交
160 161 162 163

                graphic.setHoverStyle(
                    el,
                    itemModel.getModel('itemStyle.emphasis').getItemStyle()
L
lang 已提交
164 165
                );

L
lang 已提交
166
                var symbolSize = data.getItemVisual(idx, 'symbolSize');
L
lang 已提交
167 168 169
                // Adjust the line width
                el.__lineWidth = el.__lineWidth || el.style.lineWidth;
                el.style.lineWidth = el.__lineWidth / symbolSize;
L
lang 已提交
170
            }, this);
L
lang 已提交
171 172 173 174

            this._data = data;
        },

L
lang 已提交
175
        remove: function (enableAnimation) {
L
lang 已提交
176 177
            if (this._data) {
                var group = this.group;
L
lang 已提交
178 179 180 181 182 183 184
                if (enableAnimation) {
                    this._data.eachItemGraphicEl(function (el) {
                        el.animateTo({
                            scale: [0, 0]
                        }, 200, 'cubicOut', function () {
                            group.remove(el);
                        });
L
lang 已提交
185
                    });
L
lang 已提交
186 187 188 189
                }
                else {
                    group.removeAll();
                }
L
lang 已提交
190 191 192 193 194 195
            }
        }
    }

    return DataSymbol;
});