ThemeRiverView.js 5.9 KB
Newer Older
1 2 3 4
/**
 * @file  The file used to draw themeRiver view
 * @author  Deqing Li(annong035@gmail.com)
 */
D
deqingli 已提交
5 6 7 8
define(function (require) {

    var poly = require('../line/poly');
    var graphic = require('../../util/graphic');
L
lang 已提交
9 10
    var zrUtil = require('zrender/core/util');
    var DataDiffer = require('../../data/DataDiffer');
D
deqingli 已提交
11 12 13 14 15

    return require('../../echarts').extendChartView({

        type: 'themeRiver',

L
lang 已提交
16 17 18 19
        init: function () {
            this._layers = [];
        },

D
deqingli 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
        render: function (seriesModel, ecModel, api) {
            var data = seriesModel.getData();
            var rawData = seriesModel.getRawData();

            if (!data.count()) {
                return;
            }

            var group = this.group;

            var layerSeries = seriesModel.getLayerSeries();

            var layoutInfo = data.getLayout('layoutInfo');
            var rect = layoutInfo.rect;
            var boundaryGap = layoutInfo.boundaryGap;

L
lang 已提交
36
            group.attr('position', [0, rect.y + boundaryGap[0]]);
D
deqingli 已提交
37

L
lang 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
            function keyGetter(item) {
                return item.name;
            }
            var dataDiffer = new DataDiffer(
                this._layersSeries || [], layerSeries,
                keyGetter, keyGetter
            );

            var newLayersGroups = {};

            dataDiffer.add(zrUtil.bind(zrUtil.curry(process, 'add'), this))
                .update(zrUtil.bind(zrUtil.curry(process, 'update'), this))
                .remove(zrUtil.bind(zrUtil.curry(process, 'remove'), this))
                .execute();

            function process(status, idx, oldIdx) {
                var oldLayersGroups = this._layers;
                if (status === 'remove') {
                    group.remove(oldLayersGroups[idx]);
                    return;
                }
D
deqingli 已提交
59 60 61
                var points0 = [];
                var points1 = [];
                var color;
L
lang 已提交
62
                var indices = layerSeries[idx].indices;
L
tweak  
lang 已提交
63
                for (var j = 0; j < indices.length; j++) {
L
lang 已提交
64
                    var layout = data.getItemLayout(indices[j]);
D
deqingli 已提交
65 66 67 68 69 70 71
                    var x = layout.x;
                    var y0 = layout.y0;
                    var y = layout.y;

                    points0.push([x, y0]);
                    points1.push([x, y0 + y]);

1
100pah 已提交
72
                    color = rawData.getItemVisual(indices[j], 'color');
L
lang 已提交
73
                }
D
deqingli 已提交
74

L
lang 已提交
75 76 77 78
                var polygon;
                var text;
                var textLayout = data.getItemLayout(indices[0]);
                var itemModel = data.getItemModel(indices[j - 1]);
D
deqingli 已提交
79 80
                var labelModel = itemModel.getModel('label.normal');
                var margin = labelModel.get('margin');
L
lang 已提交
81 82 83 84 85 86 87 88 89
                if (status === 'add') {
                    var layerGroup = newLayersGroups[idx] = new graphic.Group();
                    polygon = new poly.Polygon({
                        shape: {
                            points: points0,
                            stackedOnPoints: points1,
                            smooth: 0.4,
                            stackedOnSmooth: 0.4,
                            smoothConstraint: false
L
lang 已提交
90 91
                        },
                        z2: 0
L
lang 已提交
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
                    });
                    text = new graphic.Text({
                        style: {
                            x: textLayout.x - margin,
                            y: textLayout.y0 + textLayout.y / 2
                        }
                    });
                    layerGroup.add(polygon);
                    layerGroup.add(text);
                    group.add(layerGroup);

                    polygon.setClipPath(createGridClipShape(polygon.getBoundingRect(), seriesModel, function () {
                        polygon.removeClipPath();
                    }));
                }
                else {
                    var layerGroup = oldLayersGroups[oldIdx];
                    polygon = layerGroup.childAt(0);
                    text = layerGroup.childAt(1);
                    group.add(layerGroup);

                    newLayersGroups[idx] = layerGroup;

                    graphic.updateProps(polygon, {
                        shape: {
                            points: points0,
                            stackedOnPoints: points1
                        }
                    }, seriesModel);

                    graphic.updateProps(text, {
                        style: {
                            x: textLayout.x - margin,
                            y: textLayout.y0 + textLayout.y / 2
                        }
                    }, seriesModel);
                }

L
lang 已提交
130
                var hoverItemStyleModel = itemModel.getModel('itemStyle.emphasis');
J
Jaroslav Benc 已提交
131
                var itemStyleModel = itemModel.getModel('itemStyle.normal');
D
deqingli 已提交
132

P
pah100 已提交
133
                graphic.setTextStyle(text.style, labelModel, {
D
deqingli 已提交
134
                    text: labelModel.get('show')
L
lang 已提交
135 136
                        ? seriesModel.getFormattedLabel(indices[j - 1], 'normal')
                            || data.getName(indices[j - 1])
P
pah100 已提交
137
                        : null,
L
lang 已提交
138
                    textVerticalAlign: 'middle'
D
deqingli 已提交
139 140
                });

L
lang 已提交
141
                polygon.setStyle(zrUtil.extend({
D
deqingli 已提交
142
                    fill: color
L
lang 已提交
143
                }, itemStyleModel.getItemStyle(['color'])));
D
deqingli 已提交
144

L
lang 已提交
145
                graphic.setHoverStyle(polygon, hoverItemStyleModel.getItemStyle());
D
deqingli 已提交
146
            }
L
lang 已提交
147 148 149

            this._layersSeries = layerSeries;
            this._layers = newLayersGroups;
1
100pah 已提交
150 151 152
        },

        dispose: function () {}
D
deqingli 已提交
153 154
    });

155
    // add animation to the view
L
lang 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    function createGridClipShape(rect, seriesModel, cb) {
        var rectEl = new graphic.Rect({
            shape: {
                x: rect.x - 10,
                y: rect.y - 10,
                width: 0,
                height: rect.height + 20
            }
        });
        graphic.initProps(rectEl, {
            shape: {
                width: rect.width + 20,
                height: rect.height + 20
            }
        }, seriesModel, cb);

        return rectEl;
    }

175
});