MarkPointView.js 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

S
sushuang 已提交
20
import * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
21 22 23 24 25
import SymbolDraw from '../../chart/helper/SymbolDraw';
import * as numberUtil from '../../util/number';
import List from '../../data/List';
import * as markerHelper from './markerHelper';
import MarkerView from './MarkerView';
L
lang 已提交
26

S
sushuang 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
function updateMarkerLayout(mpData, seriesModel, api) {
    var coordSys = seriesModel.coordinateSystem;
    mpData.each(function (idx) {
        var itemModel = mpData.getItemModel(idx);
        var point;
        var xPx = numberUtil.parsePercent(itemModel.get('x'), api.getWidth());
        var yPx = numberUtil.parsePercent(itemModel.get('y'), api.getHeight());
        if (!isNaN(xPx) && !isNaN(yPx)) {
            point = [xPx, yPx];
        }
        // Chart like bar may have there own marker positioning logic
        else if (seriesModel.getMarkerPosition) {
            // Use the getMarkerPoisition
            point = seriesModel.getMarkerPosition(
                mpData.getValues(mpData.dimensions, idx)
            );
        }
        else if (coordSys) {
            var x = mpData.get(coordSys.dimensions[0], idx);
            var y = mpData.get(coordSys.dimensions[1], idx);
            point = coordSys.dataToPoint([x, y]);
48

S
sushuang 已提交
49
        }
50

S
sushuang 已提交
51 52 53 54 55 56 57 58 59 60 61 62
        // Use x, y if has any
        if (!isNaN(xPx)) {
            point[0] = xPx;
        }
        if (!isNaN(yPx)) {
            point[1] = yPx;
        }

        mpData.setItemLayout(idx, point);
    });
}

S
sushuang 已提交
63
export default MarkerView.extend({
S
sushuang 已提交
64 65 66

    type: 'markPoint',

67 68 69 70 71 72 73 74 75
    // updateLayout: function (markPointModel, ecModel, api) {
    //     ecModel.eachSeries(function (seriesModel) {
    //         var mpModel = seriesModel.markPointModel;
    //         if (mpModel) {
    //             updateMarkerLayout(mpModel.getData(), seriesModel, api);
    //             this.markerGroupMap.get(seriesModel.id).updateLayout(mpModel);
    //         }
    //     }, this);
    // },
L
lang 已提交
76

77 78 79 80 81 82 83 84 85 86
    updateTransform: function (markPointModel, ecModel, api) {
        ecModel.eachSeries(function (seriesModel) {
            var mpModel = seriesModel.markPointModel;
            if (mpModel) {
                updateMarkerLayout(mpModel.getData(), seriesModel, api);
                this.markerGroupMap.get(seriesModel.id).updateLayout(mpModel);
            }
        }, this);
    },

S
sushuang 已提交
87 88 89 90
    renderSeries: function (seriesModel, mpModel, ecModel, api) {
        var coordSys = seriesModel.coordinateSystem;
        var seriesId = seriesModel.id;
        var seriesData = seriesModel.getData();
L
lang 已提交
91

S
sushuang 已提交
92 93 94
        var symbolDrawMap = this.markerGroupMap;
        var symbolDraw = symbolDrawMap.get(seriesId)
            || symbolDrawMap.set(seriesId, new SymbolDraw());
L
lang 已提交
95

S
sushuang 已提交
96
        var mpData = createList(coordSys, seriesModel, mpModel);
L
lang 已提交
97

S
sushuang 已提交
98 99
        // FIXME
        mpModel.setData(mpData);
L
lang 已提交
100

S
sushuang 已提交
101
        updateMarkerLayout(mpModel.getData(), seriesModel, api);
L
lang 已提交
102

S
sushuang 已提交
103 104
        mpData.each(function (idx) {
            var itemModel = mpData.getItemModel(idx);
105 106 107 108
            var symbol = itemModel.getShallow('symbol');
            if (typeof symbol === 'function') {
                symbol = symbol(mpModel.getRawValue(idx), mpModel.getDataParams(idx));
            }
S
sushuang 已提交
109 110 111 112 113 114 115 116 117
            var symbolSize = itemModel.getShallow('symbolSize');
            if (typeof symbolSize === 'function') {
                // FIXME 这里不兼容 ECharts 2.x,2.x 貌似参数是整个数据?
                symbolSize = symbolSize(
                    mpModel.getRawValue(idx), mpModel.getDataParams(idx)
                );
            }
            mpData.setItemVisual(idx, {
                symbolSize: symbolSize,
P
pissang 已提交
118
                color: itemModel.get('itemStyle.color')
S
sushuang 已提交
119
                    || seriesData.getVisual('color'),
120
                symbol: symbol
S
sushuang 已提交
121 122
            });
        });
L
lang 已提交
123

S
sushuang 已提交
124 125 126 127 128 129 130 131 132
        // TODO Text are wrong
        symbolDraw.updateData(mpData);
        this.group.add(symbolDraw.group);

        // Set host model for tooltip
        // FIXME
        mpData.eachItemGraphicEl(function (el) {
            el.traverse(function (child) {
                child.dataModel = mpModel;
L
lang 已提交
133
            });
S
sushuang 已提交
134
        });
L
lang 已提交
135

S
sushuang 已提交
136
        symbolDraw.__keep = true;
L
lang 已提交
137

S
sushuang 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        symbolDraw.group.silent = mpModel.get('silent') || seriesModel.get('silent');
    }
});

/**
 * @inner
 * @param {module:echarts/coord/*} [coordSys]
 * @param {module:echarts/model/Series} seriesModel
 * @param {module:echarts/model/Model} mpModel
 */
function createList(coordSys, seriesModel, mpModel) {
    var coordDimsInfos;
    if (coordSys) {
        coordDimsInfos = zrUtil.map(coordSys && coordSys.dimensions, function (coordDim) {
            var info = seriesModel.getData().getDimensionInfo(
153
                seriesModel.getData().mapDimension(coordDim)
154 155
            ) || {};
            // In map series data don't have lng and lat dimension. Fallback to same with coordSys
S
tweak  
sushuang 已提交
156
            return zrUtil.defaults({name: coordDim}, info);
S
sushuang 已提交
157 158 159
        });
    }
    else {
S
sushuang 已提交
160
        coordDimsInfos = [{
S
sushuang 已提交
161
            name: 'value',
S
tweak  
sushuang 已提交
162
            type: 'float'
S
sushuang 已提交
163 164 165 166 167 168 169 170 171 172
        }];
    }

    var mpData = new List(coordDimsInfos, mpModel);
    var dataOpt = zrUtil.map(mpModel.get('data'), zrUtil.curry(
            markerHelper.dataTransform, seriesModel
        ));
    if (coordSys) {
        dataOpt = zrUtil.filter(
            dataOpt, zrUtil.curry(markerHelper.dataFilter, coordSys)
L
lang 已提交
173
        );
L
lang 已提交
174
    }
175

S
sushuang 已提交
176 177 178 179 180
    mpData.initData(dataOpt, null,
        coordSys ? markerHelper.dimValueGetter : function (item) {
            return item.value;
        }
    );
S
sushuang 已提交
181

S
sushuang 已提交
182 183
    return mpData;
}