GraphView.js 6.8 KB
Newer Older
L
lang 已提交
1

L
lang 已提交
2 3
define(function (require) {

L
lang 已提交
4 5
    var SymbolDraw = require('../helper/SymbolDraw');
    var LineDraw = require('../helper/LineDraw');
L
lang 已提交
6
    var RoamController = require('../../component/helper/RoamController');
L
lang 已提交
7

L
lang 已提交
8 9
    var modelUtil = require('../../util/model');

L
lang 已提交
10 11 12 13
    require('../../echarts').extendChartView({

        type: 'graph',

L
lang 已提交
14
        init: function (ecModel, api) {
L
lang 已提交
15
            var symbolDraw = new SymbolDraw();
L
lang 已提交
16
            var lineDraw = new LineDraw();
L
lang 已提交
17
            var group = this.group;
L
lang 已提交
18

L
lang 已提交
19 20
            var controller = new RoamController(api.getZr(), group);

L
lang 已提交
21 22
            group.add(symbolDraw.group);
            group.add(lineDraw.group);
L
lang 已提交
23

L
lang 已提交
24
            this._symbolDraw = symbolDraw;
L
lang 已提交
25
            this._lineDraw = lineDraw;
L
lang 已提交
26
            this._controller = controller;
L
lang 已提交
27 28 29
        },

        render: function (seriesModel, ecModel, api) {
L
lang 已提交
30 31 32 33 34 35
            var coordSys = seriesModel.coordinateSystem;
            // Only support view and geo coordinate system
            if (coordSys.type !== 'geo' && coordSys.type !== 'view') {
                return;
            }

L
lang 已提交
36
            var data = seriesModel.getData();
L
lang 已提交
37
            this._model = seriesModel;
L
lang 已提交
38

L
lang 已提交
39 40 41
            var symbolDraw = this._symbolDraw;
            var lineDraw = this._lineDraw;

42
            symbolDraw.updateData(data);
L
lang 已提交
43

L
lang 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            var edgeData = data.graph.edgeData;
            var rawOption = seriesModel.option;
            var formatModel = modelUtil.createDataFormatModel(
                seriesModel, edgeData, rawOption.edges || rawOption.links
            );
            formatModel.formatTooltip = function (dataIndex) {
                var params = this.getDataParams(dataIndex);
                var rawDataOpt = params.data;
                var html = rawDataOpt.source + ' > ' + rawDataOpt.target;
                if (params.value) {
                    html += ':' + params.value;
                }
                return html;
            };
            lineDraw.updateData(edgeData, null, null);
            edgeData.eachItemGraphicEl(function (el) {
                el.traverse(function (child) {
                    child.hostModel = formatModel;
                });
            });

L
lang 已提交
65 66 67 68 69 70 71 72 73 74 75 76
            // Save the original lineWidth
            data.graph.eachEdge(function (edge) {
                edge.__lineWidth = edge.getModel('lineStyle.normal').get('width');
            });

            var group = this.group;
            group.attr({
                position: coordSys.position,
                scale: coordSys.scale
            });

            this._nodeScaleRatio = seriesModel.get('nodeScaleRatio');
L
lang 已提交
77
            // this._edgeScaleRatio = seriesModel.get('edgeScaleRatio');
L
lang 已提交
78 79 80

            this._updateNodeAndLinkScale();

L
lang 已提交
81
            this._updateController(seriesModel, coordSys, api);
L
lang 已提交
82

L
lang 已提交
83
            clearTimeout(this._layoutTimeout);
L
lang 已提交
84
            var forceLayout = seriesModel.forceLayout;
L
lang 已提交
85
            var layoutAnimation = seriesModel.get('force.layoutAnimation');
L
lang 已提交
86
            if (forceLayout) {
L
lang 已提交
87
                this._startForceLayoutIteration(forceLayout, layoutAnimation);
L
lang 已提交
88
            }
L
lang 已提交
89 90 91 92 93 94
            // Update draggable
            data.eachItemGraphicEl(function (el, idx) {
                var draggable = data.getItemModel(idx).get('draggable');
                if (draggable && forceLayout) {
                    el.on('drag', function () {
                        forceLayout.warmUp();
L
lang 已提交
95 96
                        !this._layouting
                            && this._startForceLayoutIteration(forceLayout, layoutAnimation);
L
lang 已提交
97 98 99
                        forceLayout.setFixed(idx);
                        // Write position back to layout
                        data.setItemLayout(idx, el.position);
L
lang 已提交
100
                    }, this).on('dragend', function () {
L
lang 已提交
101
                        forceLayout.setUnfixed(idx);
L
lang 已提交
102
                    }, this);
L
lang 已提交
103 104 105 106 107 108
                }
                else {
                    el.off('drag');
                }
                el.setDraggable(draggable);
            }, this);
L
lang 已提交
109 110
        },

L
lang 已提交
111
        _startForceLayoutIteration: function (forceLayout, layoutAnimation) {
L
lang 已提交
112 113 114 115
            var self = this;
            (function step() {
                forceLayout.step(function (stopped) {
                    self.updateLayout();
L
lang 已提交
116 117 118 119 120
                    (this._layouting = !stopped) && (
                        layoutAnimation
                            ? (self._layoutTimeout = setTimeout(step, 16))
                            : step()
                    );
L
lang 已提交
121 122
                });
            })();
L
lang 已提交
123 124 125
        },

        _updateController: function (seriesModel, coordSys, api) {
L
lang 已提交
126 127 128
            var controller = this._controller;
            controller.rect = coordSys.getViewRect();

L
lang 已提交
129
            controller.enable(seriesModel.get('roam'));
L
lang 已提交
130 131 132 133 134

            controller
                .off('pan')
                .off('zoom')
                .on('pan', function (dx, dy) {
L
lang 已提交
135
                    api.dispatchAction({
P
pah100 已提交
136
                        seriesId: seriesModel.id,
L
lang 已提交
137 138 139 140 141 142
                        type: 'graphRoam',
                        dx: dx,
                        dy: dy
                    });
                })
                .on('zoom', function (zoom, mouseX, mouseY) {
L
lang 已提交
143
                    api.dispatchAction({
P
pah100 已提交
144
                        seriesId: seriesModel.id,
L
lang 已提交
145 146 147 148 149 150 151
                        type: 'graphRoam',
                        zoom:  zoom,
                        originX: mouseX,
                        originY: mouseY
                    });
                })
                .on('zoom', this._updateNodeAndLinkScale, this);
L
lang 已提交
152 153 154 155 156 157 158 159
        },

        _updateNodeAndLinkScale: function () {
            var seriesModel = this._model;
            var data = seriesModel.getData();

            var group = this.group;
            var nodeScaleRatio = this._nodeScaleRatio;
L
lang 已提交
160
            // var edgeScaleRatio = this._edgeScaleRatio;
L
lang 已提交
161 162 163 164 165

            // Assume scale aspect is 1
            var groupScale = group.scale[0];

            var nodeScale = (groupScale - 1) * nodeScaleRatio + 1;
L
lang 已提交
166
            // var edgeScale = (groupScale - 1) * edgeScaleRatio + 1;
L
lang 已提交
167 168 169 170 171
            var invScale = [
                nodeScale / groupScale,
                nodeScale / groupScale
            ];

L
lang 已提交
172
            data.eachItemGraphicEl(function (el, idx) {
L
lang 已提交
173 174
                el.attr('scale', invScale);
            });
L
lang 已提交
175 176 177 178 179 180 181 182
            // data.graph.eachEdge(function (edge) {
            //     var lineGroup = edge.getGraphicEl();
            //     // FIXME
            //     lineGroup.childOfName('line').setStyle(
            //         'lineWidth',
            //         edge.__lineWidth * edgeScale / groupScale
            //     );
            // });
L
lang 已提交
183 184
        },

L
lang 已提交
185 186 187 188
        updateLayout: function (seriesModel, ecModel) {
            this._symbolDraw.updateLayout();
            this._lineDraw.updateLayout();
        },
L
lang 已提交
189

L
lang 已提交
190
        remove: function (ecModel, api) {
L
lang 已提交
191 192
            this._symbolDraw && this._symbolDraw.remove();
            this._lineDraw && this._lineDraw.remove();
L
lang 已提交
193 194 195
        }
    });
});