ParallelView.js 4.8 KB
Newer Older
P
pah100 已提交
1 2 3 4
define(function (require) {

    var graphic = require('../../util/graphic');
    var zrUtil = require('zrender/core/util');
P
pah100 已提交
5 6 7
    var polyHelper = require('../line/poly');

    var SMOOTH = 0.3;
P
pah100 已提交
8 9 10

    var ParallelView = require('../../view/Chart').extend({

P
pah100 已提交
11
        type: 'parallel',
P
pah100 已提交
12 13

        init: function () {
P
pah100 已提交
14 15 16 17 18 19 20

            /**
             * @type {module:zrender/container/Group}
             * @private
             */
            this._dataGroup = new graphic.Group();

21
            this.group.add(this._dataGroup);
P
pah100 已提交
22 23 24 25
            /**
             * @type {module:echarts/data/List}
             */
            this._data;
P
pah100 已提交
26 27
        },

P
pah100 已提交
28 29 30
        /**
         * @override
         */
P
pah100 已提交
31
        render: function (seriesModel, ecModel, api, payload) {
P
pah100 已提交
32
            var dataGroup = this._dataGroup;
P
pah100 已提交
33 34 35 36
            var data = seriesModel.getData();
            var oldData = this._data;
            var coordSys = seriesModel.coordinateSystem;
            var dimensions = coordSys.dimensions;
P
pah100 已提交
37 38 39
            var option = seriesModel.option;
            var progressive = option.progressive;
            var smooth = option.smooth ? SMOOTH : null;
P
pah100 已提交
40 41

            data.diff(oldData)
P
pah100 已提交
42 43 44
                .add(add)
                .update(update)
                .remove(remove)
P
pah100 已提交
45 46
                .execute();

L
lang 已提交
47
            // Update style
P
pah100 已提交
48
            data.eachItemGraphicEl(function (line, idx) {
L
lang 已提交
49 50
                var itemModel = data.getItemModel(idx);
                var lineStyleModel = itemModel.getModel('lineStyle.normal');
P
pah100 已提交
51 52 53 54 55 56 57 58 59

                line.useStyle(zrUtil.extend(
                    lineStyleModel.getLineStyle(),
                    {
                        fill: null,
                        stroke: data.getItemVisual(idx, 'color'),
                        opacity: data.getItemVisual(idx, 'opacity')
                    }
                ));
L
lang 已提交
60 61
            });

62 63 64 65 66 67 68 69 70
            // First create
            if (!this._data) {
                dataGroup.setClipPath(createGridClipShape(
                    coordSys, seriesModel, function () {
                        dataGroup.removeClipPath();
                    }
                ));
            }

P
pah100 已提交
71
            this._data = data;
P
pah100 已提交
72

P
pah100 已提交
73
            function add(newDataIndex) {
P
pah100 已提交
74 75 76 77
                var points = createLinePoints(data, newDataIndex, dimensions, coordSys);
                var line = createPoly(points, newDataIndex, progressive, smooth);
                dataGroup.add(line);
                data.setItemGraphicEl(newDataIndex, line);
P
pah100 已提交
78
            }
P
pah100 已提交
79

P
pah100 已提交
80
            function update(newDataIndex, oldDataIndex) {
P
pah100 已提交
81 82 83 84 85
                var line = oldData.getItemGraphicEl(oldDataIndex);
                var points = createLinePoints(data, newDataIndex, dimensions, coordSys);
                line.shape.points = points;
                line.dirty();
                data.setItemGraphicEl(newDataIndex, line);
P
pah100 已提交
86 87 88
            }

            function remove(oldDataIndex) {
P
pah100 已提交
89 90
                var line = oldData.getItemGraphicEl(oldDataIndex);
                dataGroup.remove(line);
P
pah100 已提交
91
            }
P
pah100 已提交
92 93
        },

P
pah100 已提交
94 95 96
        /**
         * @override
         */
P
pah100 已提交
97
        remove: function () {
P
pah100 已提交
98
            this._dataGroup && this._dataGroup.removeAll();
P
pah100 已提交
99
            this._data = null;
P
pah100 已提交
100
        }
P
pah100 已提交
101 102
    });

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    function createGridClipShape(coordSys, seriesModel, cb) {
        var parallelModel = coordSys.model;
        var rect = coordSys.getRect();
        var rectEl = new graphic.Rect({
            shape: {
                x: rect.x,
                y: rect.y,
                width: rect.width,
                height: rect.height
            }
        });
        var dim = parallelModel.get('layout') === 'horizontal' ? 'width' : 'height';
        rectEl.setShape(dim, 0);
        graphic.initProps(rectEl, {
            shape: {
                width: rect.width,
                height: rect.height
            }
        }, seriesModel, cb);
        return rectEl;
    }

P
pah100 已提交
125 126 127 128 129 130 131 132 133 134
    function createPoly(points, dataIndex, progressive, smooth) {
        return new polyHelper.Polyline({
            shape: {
                points: points,
                smooth: smooth
            },
            silent: true,
            progressive: progressive ? Math.round(dataIndex / progressive) : 0,
            z2: 10
        });
P
pah100 已提交
135
    }
P
pah100 已提交
136

P
pah100 已提交
137 138 139 140 141 142 143
    function createLinePoints(data, dataIndex, dimensions, coordSys) {
        var points = [];
        zrUtil.each(dimensions, function (dimName) {
            var value = data.get(dimName, dataIndex);
            if (!isEmptyValue(value, coordSys.getAxis(dimName).type)) {
                points.push(coordSys.dataToPoint(value, dimName));
            }
P
pah100 已提交
144
        });
P
pah100 已提交
145
        return points;
P
pah100 已提交
146
    }
P
pah100 已提交
147 148 149

    // FIXME
    // 公用方法?
P
pah100 已提交
150 151
    function isEmptyValue(val, axisType) {
        return axisType === 'category'
P
pah100 已提交
152 153 154 155
            ? val == null
            : (val == null || isNaN(val)); // axisType === 'value'
    }

P
pah100 已提交
156 157
    return ParallelView;
});