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

    var graphic = require('../../util/graphic');
    var zrUtil = require('zrender/core/util');

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

P
pah100 已提交
8
        type: 'parallel',
P
pah100 已提交
9 10

        init: function () {
P
pah100 已提交
11 12 13 14 15 16 17 18 19 20 21 22

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

            /**
             * @type {module:echarts/data/List}
             */
            this._data;
P
pah100 已提交
23 24
        },

P
pah100 已提交
25 26 27
        /**
         * @override
         */
P
pah100 已提交
28 29
        render: function (seriesModel, ecModel, api, payload) {

P
pah100 已提交
30
            var dataGroup = this._dataGroup;
P
pah100 已提交
31 32 33 34 35
            var data = seriesModel.getData();
            var oldData = this._data;
            var coordSys = seriesModel.coordinateSystem;
            var dimensions = coordSys.dimensions;

P
pah100 已提交
36
            // var hasAnimation = ecModel.get('animation');
P
pah100 已提交
37
            var lineStyleModel = seriesModel.getModel('lineStyle.normal');
P
tweak  
pah100 已提交
38
            var lineStyle = zrUtil.extend(
P
pah100 已提交
39
                lineStyleModel.getLineStyle(),
P
tweak  
pah100 已提交
40
                {stroke: data.getVisual('color')}
P
pah100 已提交
41
            );
P
pah100 已提交
42 43

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

P
pah100 已提交
49
            this._data = data;
P
pah100 已提交
50

P
pah100 已提交
51
            function add(newDataIndex) {
P
pah100 已提交
52
                var values = data.getValues(dimensions, newDataIndex);
P
pah100 已提交
53 54
                var elGroup = new graphic.Group();
                dataGroup.add(elGroup);
P
pah100 已提交
55 56 57 58 59 60 61

                eachAxisPair(
                    values, dimensions, coordSys,
                    function (pointPair, pairIndex) {
                        // FIXME
                        // init animation
                        if (pointPair) {
P
pah100 已提交
62
                            elGroup.add(createEl(pointPair));
P
pah100 已提交
63 64 65
                        }
                    }
                );
P
pah100 已提交
66

P
pah100 已提交
67 68
                setStyle(elGroup, data, newDataIndex, lineStyle);
                data.setItemGraphicEl(newDataIndex, elGroup);
P
pah100 已提交
69
            }
P
pah100 已提交
70

P
pah100 已提交
71
            function update(newDataIndex, oldDataIndex) {
P
pah100 已提交
72
                var values = data.getValues(dimensions, newDataIndex);
P
pah100 已提交
73 74 75
                var elGroup = oldData.getItemGraphicEl(oldDataIndex);
                var newEls = [];
                var elGroupIndex = 0;
P
pah100 已提交
76 77 78 79

                eachAxisPair(
                    values, dimensions, coordSys,
                    function (pointPair, pairIndex) {
P
pah100 已提交
80
                        var el = elGroup.childAt(elGroupIndex++);
P
pah100 已提交
81 82

                        if (pointPair && !el) {
P
pah100 已提交
83
                            newEls.push(createEl(pointPair));
P
pah100 已提交
84 85 86 87 88 89 90 91
                        }
                        else if (pointPair) {
                            el.setShape({points: pointPair});
                        }
                    }
                );

                // Remove redundent els
P
pah100 已提交
92 93 94 95 96 97 98
                for (var i = elGroup.childCount() - 1; i >= elGroupIndex; i--) {
                    elGroup.remove(elGroup.childAt(i));
                }

                // Add new els
                for (var i = 0, len = newEls.length; i < len; i++) {
                    elGroup.add(newEls[i]);
P
pah100 已提交
99 100
                }

P
pah100 已提交
101 102
                setStyle(elGroup, data, newDataIndex, lineStyle);
                data.setItemGraphicEl(newDataIndex, elGroup);
P
pah100 已提交
103 104 105
            }

            function remove(oldDataIndex) {
P
pah100 已提交
106 107
                var elGroup = oldData.getItemGraphicEl(oldDataIndex);
                dataGroup.remove(elGroup);
P
pah100 已提交
108
            }
P
pah100 已提交
109 110
        },

P
pah100 已提交
111 112 113
        /**
         * @override
         */
P
pah100 已提交
114
        remove: function () {
P
pah100 已提交
115
            this._dataGroup && this._dataGroup.removeAll();
P
pah100 已提交
116
            this._data = null;
P
pah100 已提交
117
        }
P
pah100 已提交
118 119
    });

P
pah100 已提交
120
    function eachAxisPair(values, dimensions, coordSys, cb) {
P
pah100 已提交
121
        for (var i = 0, len = dimensions.length - 1; i < len; i++) {
P
pah100 已提交
122 123 124 125 126
            var dimA = dimensions[i];
            var dimB = dimensions[i + 1];
            var valueA = values[i];
            var valueB = values[i + 1];

P
pah100 已提交
127
            cb(
P
pah100 已提交
128 129 130
                (isEmptyValue(valueA, coordSys.getAxis(dimA).type)
                    || isEmptyValue(valueB, coordSys.getAxis(dimB).type)
                )
P
pah100 已提交
131 132
                    ? null
                    : [
P
pah100 已提交
133 134
                        coordSys.dataToPoint(valueA, dimA),
                        coordSys.dataToPoint(valueB, dimB)
P
pah100 已提交
135 136 137 138 139
                    ],
                i
            );
        }
    }
P
pah100 已提交
140

P
pah100 已提交
141 142
    function createEl(pointPair) {
        return new graphic.Polyline({
P
pah100 已提交
143 144 145 146
            shape: {points: pointPair},
            silent: true
        });
    }
P
pah100 已提交
147

P
pah100 已提交
148 149
    function setStyle(elGroup, data, dataIndex, lineStyle) {
        elGroup.eachChild(function (el) {
P
pah100 已提交
150
            el.setStyle(lineStyle);
P
pah100 已提交
151
            var opacity = data.getItemVisual(dataIndex, 'opacity', true);
P
pah100 已提交
152
            el.setStyle('opacity', opacity);
P
pah100 已提交
153
        });
P
pah100 已提交
154 155
    }

P
pah100 已提交
156 157
    // FIXME
    // 公用方法?
P
pah100 已提交
158 159
    function isEmptyValue(val, axisType) {
        return axisType === 'category'
P
pah100 已提交
160 161 162 163
            ? val == null
            : (val == null || isNaN(val)); // axisType === 'value'
    }

P
pah100 已提交
164 165
    return ParallelView;
});