markerHelper.js 8.1 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
import * as numberUtil from '../../util/number';
S
sushuang 已提交
22
import {isDimensionStacked} from '../../data/helper/dataStackHelper';
L
lang 已提交
23

S
sushuang 已提交
24 25 26 27 28 29 30 31 32 33
var indexOf = zrUtil.indexOf;

function hasXOrY(item) {
    return !(isNaN(parseFloat(item.x)) && isNaN(parseFloat(item.y)));
}

function hasXAndY(item) {
    return !isNaN(parseFloat(item.x)) && !isNaN(parseFloat(item.y));
}

S
sushuang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// Make it simple, do not visit all stacked value to count precision.
// function getPrecision(data, valueAxisDim, dataIndex) {
//     var precision = -1;
//     var stackedDim = data.mapDimension(valueAxisDim);
//     do {
//         precision = Math.max(
//             numberUtil.getPrecision(data.get(stackedDim, dataIndex)),
//             precision
//         );
//         var stackedOnSeries = data.getCalculationInfo('stackedOnSeries');
//         if (stackedOnSeries) {
//             var byValue = data.get(data.getCalculationInfo('stackedByDimension'), dataIndex);
//             data = stackedOnSeries.getData();
//             dataIndex = data.indexOf(data.getCalculationInfo('stackedByDimension'), byValue);
//             stackedDim = data.getCalculationInfo('stackedDimension');
//         }
//         else {
//             data = null;
//         }
//     } while (data);

//     return precision;
// }
S
sushuang 已提交
57 58 59 60 61 62

function markerTypeCalculatorWithExtent(
    mlType, data, otherDataDim, targetDataDim, otherCoordIndex, targetCoordIndex
) {
    var coordArr = [];

S
sushuang 已提交
63 64 65 66 67 68 69 70 71 72
    var stacked = isDimensionStacked(data, targetDataDim, otherDataDim);
    var calcDataDim = stacked
        ? data.getCalculationInfo('stackResultDimension')
        : targetDataDim;

    var value = numCalculate(data, calcDataDim, mlType);

    var dataIndex = data.indicesOfNearest(calcDataDim, value)[0];
    coordArr[otherCoordIndex] = data.get(otherDataDim, dataIndex);
    coordArr[targetCoordIndex] = data.get(targetDataDim, dataIndex);
S
sushuang 已提交
73

S
sushuang 已提交
74 75
    // Make it simple, do not visit all stacked value to count precision.
    var precision = numberUtil.getPrecision(data.get(targetDataDim, dataIndex));
S
sushuang 已提交
76 77 78
    precision = Math.min(precision, 20);
    if (precision >= 0) {
        coordArr[targetCoordIndex] = +coordArr[targetCoordIndex].toFixed(precision);
L
tweak  
lang 已提交
79
    }
L
lang 已提交
80

S
sushuang 已提交
81 82
    return coordArr;
}
L
lang 已提交
83

S
sushuang 已提交
84 85 86
var curry = zrUtil.curry;
// TODO Specified percent
var markerTypeCalculator = {
L
tweak  
lang 已提交
87
    /**
S
sushuang 已提交
88 89 90 91
     * @method
     * @param {module:echarts/data/List} data
     * @param {string} baseAxisDim
     * @param {string} valueAxisDim
L
tweak  
lang 已提交
92
     */
S
sushuang 已提交
93 94 95 96 97 98 99 100
    min: curry(markerTypeCalculatorWithExtent, 'min'),
    /**
     * @method
     * @param {module:echarts/data/List} data
     * @param {string} baseAxisDim
     * @param {string} valueAxisDim
     */
    max: curry(markerTypeCalculatorWithExtent, 'max'),
L
tweak  
lang 已提交
101 102

    /**
S
sushuang 已提交
103 104 105 106
     * @method
     * @param {module:echarts/data/List} data
     * @param {string} baseAxisDim
     * @param {string} valueAxisDim
L
tweak  
lang 已提交
107
     */
S
sushuang 已提交
108 109 110 111 112 113 114 115 116 117 118 119
    average: curry(markerTypeCalculatorWithExtent, 'average')
};

/**
 * Transform markPoint data item to format used in List by do the following
 * 1. Calculate statistic like `max`, `min`, `average`
 * 2. Convert `item.xAxis`, `item.yAxis` to `item.coord` array
 * @param  {module:echarts/model/Series} seriesModel
 * @param  {module:echarts/coord/*} [coordSys]
 * @param  {Object} item
 * @return {Object}
 */
S
sushuang 已提交
120
export function dataTransform(seriesModel, item) {
S
sushuang 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    var data = seriesModel.getData();
    var coordSys = seriesModel.coordinateSystem;

    // 1. If not specify the position with pixel directly
    // 2. If `coord` is not a data array. Which uses `xAxis`,
    // `yAxis` to specify the coord on each dimension

    // parseFloat first because item.x and item.y can be percent string like '20%'
    if (item && !hasXAndY(item) && !zrUtil.isArray(item.coord) && coordSys) {
        var dims = coordSys.dimensions;
        var axisInfo = getAxisInfo(item, data, coordSys, seriesModel);

        // Clone the option
        // Transform the properties xAxis, yAxis, radiusAxis, angleAxis, geoCoord to value
        item = zrUtil.clone(item);

        if (item.type
            && markerTypeCalculator[item.type]
            && axisInfo.baseAxis && axisInfo.valueAxis
        ) {
            var otherCoordIndex = indexOf(dims, axisInfo.baseAxis.dim);
            var targetCoordIndex = indexOf(dims, axisInfo.valueAxis.dim);

            item.coord = markerTypeCalculator[item.type](
                data, axisInfo.baseDataDim, axisInfo.valueDataDim,
                otherCoordIndex, targetCoordIndex
            );
            // Force to use the value of calculated value.
            item.value = item.coord[targetCoordIndex];
150 151
        }
        else {
S
sushuang 已提交
152 153 154 155 156 157 158 159
            // FIXME Only has one of xAxis and yAxis.
            var coord = [
                item.xAxis != null ? item.xAxis : item.radiusAxis,
                item.yAxis != null ? item.yAxis : item.angleAxis
            ];
            // Each coord support max, min, average
            for (var i = 0; i < 2; i++) {
                if (markerTypeCalculator[coord[i]]) {
160
                    coord[i] = numCalculate(data, data.mapDimension(dims[i]), coord[i]);
S
sushuang 已提交
161 162 163
                }
            }
            item.coord = coord;
164
        }
S
sushuang 已提交
165 166
    }
    return item;
S
sushuang 已提交
167
}
S
sushuang 已提交
168

S
sushuang 已提交
169
export function getAxisInfo(item, data, coordSys, seriesModel) {
S
sushuang 已提交
170 171 172 173 174
    var ret = {};

    if (item.valueIndex != null || item.valueDim != null) {
        ret.valueDataDim = item.valueIndex != null
            ? data.getDimension(item.valueIndex) : item.valueDim;
175
        ret.valueAxis = coordSys.getAxis(dataDimToCoordDim(seriesModel, ret.valueDataDim));
S
sushuang 已提交
176
        ret.baseAxis = coordSys.getOtherAxis(ret.valueAxis);
177
        ret.baseDataDim = data.mapDimension(ret.baseAxis.dim);
S
sushuang 已提交
178 179 180 181
    }
    else {
        ret.baseAxis = seriesModel.getBaseAxis();
        ret.valueAxis = coordSys.getOtherAxis(ret.baseAxis);
182 183
        ret.baseDataDim = data.mapDimension(ret.baseAxis.dim);
        ret.valueDataDim = data.mapDimension(ret.valueAxis.dim);
S
sushuang 已提交
184 185 186
    }

    return ret;
S
sushuang 已提交
187
}
S
sushuang 已提交
188

189 190 191 192 193 194 195 196 197 198 199 200
function dataDimToCoordDim(seriesModel, dataDim) {
    var data = seriesModel.getData();
    var dimensions = data.dimensions;
    dataDim = data.getDimension(dataDim);
    for (var i = 0; i < dimensions.length; i++) {
        var dimItem = data.getDimensionInfo(dimensions[i]);
        if (dimItem.name === dataDim) {
            return dimItem.coordDim;
        }
    }
}

S
sushuang 已提交
201 202 203 204 205 206 207
/**
 * Filter data which is out of coordinateSystem range
 * [dataFilter description]
 * @param  {module:echarts/coord/*} [coordSys]
 * @param  {Object} item
 * @return {boolean}
 */
S
sushuang 已提交
208
export function dataFilter(coordSys, item) {
S
sushuang 已提交
209 210 211
    // Alwalys return true if there is no coordSys
    return (coordSys && coordSys.containData && item.coord && !hasXOrY(item))
        ? coordSys.containData(item.coord) : true;
S
sushuang 已提交
212
}
S
sushuang 已提交
213

S
sushuang 已提交
214
export function dimValueGetter(item, dimName, dataIndex, dimIndex) {
S
sushuang 已提交
215 216 217 218 219
    // x, y, radius, angle
    if (dimIndex < 2) {
        return item.coord && item.coord[dimIndex];
    }
    return item.value;
S
sushuang 已提交
220
}
S
sushuang 已提交
221

S
sushuang 已提交
222
export function numCalculate(data, valueDataDim, type) {
S
sushuang 已提交
223 224 225 226 227 228 229 230
    if (type === 'average') {
        var sum = 0;
        var count = 0;
        data.each(valueDataDim, function (val, idx) {
            if (!isNaN(val)) {
                sum += val;
                count++;
            }
S
sushuang 已提交
231
        });
S
sushuang 已提交
232 233
        return sum / count;
    }
H
hustcc 已提交
234 235 236
    else if (type === 'median') {
        return data.getMedian(valueDataDim);
    }
S
sushuang 已提交
237
    else {
H
hustcc 已提交
238
        // max & min
S
sushuang 已提交
239 240
        return data.getDataExtent(valueDataDim, true)[type === 'max' ? 1 : 0];
    }
S
sushuang 已提交
241
}