npmtest-echarts (v0.0.1)

Code coverage report for node-npmtest-echarts/node_modules/echarts/lib/chart/helper/LargeLineDraw.js

Statements: 16.95% (10 / 59)      Branches: 0% (0 / 18)      Functions: 0% (0 / 7)      Lines: 16.95% (10 / 59)      Ignored: none     

All files » node-npmtest-echarts/node_modules/echarts/lib/chart/helper/ » LargeLineDraw.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144        1   1 1   1                                                                                                                                             1           1           1                                                                           1             1       1    
// TODO Batch by color
 
 
 
    var graphic = require('../../util/graphic');
 
    var quadraticContain = require('zrender/lib/contain/quadratic');
    var lineContain = require('zrender/lib/contain/line');
 
    var LargeLineShape = graphic.extendShape({
        shape: {
            polyline: false,
 
            segs: []
        },
 
        buildPath: function (path, shape) {
            var segs = shape.segs;
            var isPolyline = shape.polyline;
 
            for (var i = 0; i < segs.length; i++) {
                var seg = segs[i];
                if (isPolyline) {
                    path.moveTo(seg[0][0], seg[0][1]);
                    for (var j = 1; j < seg.length; j++) {
                        path.lineTo(seg[j][0], seg[j][1]);
                    }
                }
                else {
                    path.moveTo(seg[0][0], seg[0][1]);
                    if (seg.length > 2) {
                        path.quadraticCurveTo(seg[2][0], seg[2][1], seg[1][0], seg[1][1]);
                    }
                    else {
                        path.lineTo(seg[1][0], seg[1][1]);
                    }
                }
            }
        },
 
        findDataIndex: function (x, y) {
            var shape = this.shape;
            var segs = shape.segs;
            var isPolyline = shape.polyline;
            var lineWidth = Math.max(this.style.lineWidth, 1);
 
            // Not consider transform
            for (var i = 0; i < segs.length; i++) {
                var seg = segs[i];
                if (isPolyline) {
                    for (var j = 1; j < seg.length; j++) {
                        if (lineContain.containStroke(
                            seg[j - 1][0], seg[j - 1][1], seg[j][0], seg[j][1], lineWidth, x, y
                        )) {
                            return i;
                        }
                    }
                }
                else {
                    if (seg.length > 2) {
                        if (quadraticContain.containStroke(
                            seg[0][0], seg[0][1], seg[2][0], seg[2][1], seg[1][0], seg[1][1], lineWidth, x, y
                        )) {
                            return i;
                        }
                    }
                    else {
                        if (lineContain.containStroke(
                            seg[0][0], seg[0][1], seg[1][0], seg[1][1], lineWidth, x, y
                        )) {
                            return i;
                        }
                    }
                }
            }
 
            return -1;
        }
    });
 
    function LargeLineDraw() {
        this.group = new graphic.Group();
 
        this._lineEl = new LargeLineShape();
    }
 
    var largeLineProto = LargeLineDraw.prototype;
 
    /**
     * Update symbols draw by new data
     * @param {module:echarts/data/List} data
     */
    largeLineProto.updateData = function (data) {
        this.group.removeAll();
 
        var lineEl = this._lineEl;
 
        var seriesModel = data.hostModel;
 
        lineEl.setShape({
            segs: data.mapArray(data.getItemLayout),
            polyline: seriesModel.get('polyline')
        });
 
        lineEl.useStyle(
            seriesModel.getModel('lineStyle.normal').getLineStyle()
        );
 
        var visualColor = data.getVisual('color');
        if (visualColor) {
            lineEl.setStyle('stroke', visualColor);
        }
        lineEl.setStyle('fill');
 
        // Enable tooltip
        // PENDING May have performance issue when path is extremely large
        lineEl.seriesIndex = seriesModel.seriesIndex;
        lineEl.on('mousemove', function (e) {
            lineEl.dataIndex = null;
            var dataIndex = lineEl.findDataIndex(e.offsetX, e.offsetY);
            if (dataIndex > 0) {
                // Provide dataIndex for tooltip
                lineEl.dataIndex = dataIndex;
            }
        });
 
        // Add back
        this.group.add(lineEl);
    };
 
    largeLineProto.updateLayout = function (seriesModel) {
        var data = seriesModel.getData();
        this._lineEl.setShape({
            segs: data.mapArray(data.getItemLayout)
        });
    };
 
    largeLineProto.remove = function () {
        this.group.removeAll();
    };
 
    module.exports = LargeLineDraw;