ThemeRiverSeries.js 8.4 KB
Newer Older
1 2 3 4
/**
 * @file  Define the themeRiver view's series model
 * @author Deqing Li(annong035@gmail.com)
 */
D
deqingli 已提交
5

S
sushuang 已提交
6 7 8
import completeDimensions from '../../data/helper/completeDimensions';
import SeriesModel from '../../model/Series';
import List from '../../data/List';
S
sushuang 已提交
9
import * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import {encodeHTML} from '../../util/format';
import nest from '../../util/array/nest';

var DATA_NAME_INDEX = 2;

var ThemeRiverSeries = SeriesModel.extend({

    type: 'series.themeRiver',

    dependencies: ['singleAxis'],

    /**
     * @readOnly
     * @type {module:zrender/core/util#HashMap}
     */
    nameMap: null,

    /**
     * @override
     */
    init: function (option) {
        ThemeRiverSeries.superApply(this, 'init', arguments);

33
        // Put this function here is for the sake of consistency of code style.
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 57 58 59 60 61
        // Enable legend selection for each data item
        // Use a function instead of direct access because data reference may changed
        this.legendDataProvider = function () {
            return this.getRawData();
        };
    },

    /**
     * If there is no value of a certain point in the time for some event,set it value to 0.
     *
     * @param {Array} data  initial data in the option
     * @return {Array}
     */
    fixData: function (data) {
        var rawDataLength = data.length;

        // grouped data by name
        var dataByName = nest()
            .key(function (dataItem) {
                return dataItem[2];
            })
            .entries(data);

        // data group in each layer
        var layData = zrUtil.map(dataByName, function (d) {
            return {
                name: d.key,
                dataList: d.values
D
deqingli 已提交
62
            };
S
sushuang 已提交
63 64 65 66 67 68 69 70 71 72
        });

        var layerNum = layData.length;
        var largestLayer = -1;
        var index = -1;
        for (var i = 0; i < layerNum; ++i) {
            var len = layData[i].dataList.length;
            if (len > largestLayer) {
                largestLayer = len;
                index = i;
D
deqingli 已提交
73
            }
S
sushuang 已提交
74
        }
D
deqingli 已提交
75

S
sushuang 已提交
76 77 78
        for (var k = 0; k < layerNum; ++k) {
            if (k === index) {
                continue;
D
deqingli 已提交
79
            }
S
sushuang 已提交
80 81 82 83 84 85 86 87 88 89 90
            var name = layData[k].name;
            for (var j = 0; j < largestLayer; ++j) {
                var timeValue = layData[index].dataList[j][0];
                var length = layData[k].dataList.length;
                var keyIndex = -1;
                for (var l = 0; l < length; ++l) {
                    var value = layData[k].dataList[l][0];
                    if (value === timeValue) {
                        keyIndex = l;
                        break;
                    }
D
deqingli 已提交
91
                }
S
sushuang 已提交
92 93 94 95 96 97 98
                if (keyIndex === -1) {
                    data[rawDataLength] = [];
                    data[rawDataLength][0] = timeValue;
                    data[rawDataLength][1] = 0;
                    data[rawDataLength][2] = name;
                    rawDataLength++;

D
deqingli 已提交
99 100
                }
            }
S
sushuang 已提交
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 130 131 132 133 134 135 136 137 138 139
        }
        return data;
    },

    /**
     * @override
     * @param  {Object} option  the initial option that user gived
     * @param  {module:echarts/model/Model} ecModel  the model object for themeRiver option
     * @return {module:echarts/data/List}
     */
    getInitialData: function (option, ecModel) {

        var dimensions = [];

        var singleAxisModel = ecModel.queryComponents({
            mainType: 'singleAxis',
            index: this.get('singleAxisIndex'),
            id: this.get('singleAxisId')
        })[0];

        var axisType = singleAxisModel.get('type');

        dimensions = [
            {
                name: 'time',
                // FIXME common?
                type: axisType === 'category'
                    ? 'ordinal'
                    : axisType === 'time'
                    ? 'time'
                    : 'float'
            },
            {
                name: 'value',
                type: 'float'
            },
            {
                name: 'name',
                type: 'ordinal'
D
deqingli 已提交
140
            }
S
sushuang 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        ];

        // filter the data item with the value of label is undefined
        var filterData = zrUtil.filter(option.data, function (dataItem) {
            return dataItem[2] !== undefined;
        });

        var data = this.fixData(filterData || []);
        var nameList = [];
        var nameMap = this.nameMap = zrUtil.createHashMap();
        var count = 0;

        for (var i = 0; i < data.length; ++i) {
            nameList.push(data[i][DATA_NAME_INDEX]);
            if (!nameMap.get(data[i][DATA_NAME_INDEX])) {
                nameMap.set(data[i][DATA_NAME_INDEX], count);
                count++;
P
pah100 已提交
158
            }
S
sushuang 已提交
159
        }
P
pah100 已提交
160

S
sushuang 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        dimensions = completeDimensions(dimensions, data);

        var list = new List(dimensions, this);

        list.initData(data, nameList);

        return list;
    },

    /**
     * Used by single coordinate
     *
     * @param {string} axisDim  the dimension for single coordinate
     * @return {Array.<string> } specified dimensions on the axis.
     */
    coordDimToDataDim: function (axisDim) {
        return ['time'];
    },

    /**
     * The raw data is divided into multiple layers and each layer
     *     has same name.
     *
     * @return {Array.<Array.<number>>}
     */
    getLayerSeries: function () {
        var data = this.getData();
        var lenCount = data.count();
        var indexArr = [];

        for (var i = 0; i < lenCount; ++i) {
            indexArr[i] = i;
        }
        // data group by name
        var dataByName = nest()
            .key(function (index) {
                return data.get('name', index);
            })
            .entries(indexArr);

        var layerSeries = zrUtil.map(dataByName, function (d) {
            return {
                name: d.key,
                indices: d.values
            };
        });
D
deqingli 已提交
207

S
sushuang 已提交
208 209 210
        for (var j = 0; j < layerSeries.length; ++j) {
            layerSeries[j].indices.sort(comparer);
        }
D
deqingli 已提交
211

S
sushuang 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        function comparer(index1, index2) {
            return data.get('time', index1) - data.get('time', index2);
        }

        return layerSeries;
    },

    /**
     * Get data indices for show tooltip content
     *
     * @param {Array.<string>|string} dim  single coordinate dimension
     * @param {number} value axis value
     * @param {module:echarts/coord/single/SingleAxis} baseAxis  single Axis used
     *     the themeRiver.
     * @return {Object} {dataIndices, nestestValue}
     */
    getAxisTooltipData: function (dim, value, baseAxis) {
        if (!zrUtil.isArray(dim)) {
            dim = dim ? [dim] : [];
        }

        var data = this.getData();
        var layerSeries = this.getLayerSeries();
        var indices = [];
        var layerNum = layerSeries.length;
        var nestestValue;

        for (var i = 0; i < layerNum; ++i) {
            var minDist = Number.MAX_VALUE;
            var nearestIdx = -1;
            var pointNum = layerSeries[i].indices.length;
            for (var j = 0; j < pointNum; ++j) {
                var theValue = data.get(dim[0], layerSeries[i].indices[j]);
                var dist = Math.abs(theValue - value);
                if (dist <= minDist) {
                    nestestValue = theValue;
                    minDist = dist;
                    nearestIdx = layerSeries[i].indices[j];
D
deqingli 已提交
250 251
                }
            }
S
sushuang 已提交
252 253
            indices.push(nearestIdx);
        }
254

S
sushuang 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        return {dataIndices: indices, nestestValue: nestestValue};
    },

    /**
     * @override
     * @param {number} dataIndex  index of data
     */
    formatTooltip: function (dataIndex) {
        var data = this.getData();
        var htmlName = data.get('name', dataIndex);
        var htmlValue = data.get('value', dataIndex);
        if (isNaN(htmlValue) || htmlValue == null) {
            htmlValue = '-';
        }
        return encodeHTML(htmlName + ' : ' + htmlValue);
    },
D
deqingli 已提交
271

S
sushuang 已提交
272 273 274
    defaultOption: {
        zlevel: 0,
        z: 2,
D
deqingli 已提交
275

S
sushuang 已提交
276
        coordinateSystem: 'singleAxis',
D
deqingli 已提交
277

S
sushuang 已提交
278 279
        // gap in axis's orthogonal orientation
        boundaryGap: ['10%', '10%'],
D
deqingli 已提交
280

S
sushuang 已提交
281
        // legendHoverLink: true,
D
deqingli 已提交
282

S
sushuang 已提交
283
        singleAxisIndex: 0,
D
deqingli 已提交
284

S
sushuang 已提交
285
        animationEasing: 'linear',
L
lang 已提交
286

S
sushuang 已提交
287 288 289 290 291 292 293 294 295 296 297
        label: {
            normal: {
                margin: 4,
                textAlign: 'right',
                show: true,
                position: 'left',
                color: '#000',
                fontSize: 11
            },
            emphasis: {
                show: true
D
deqingli 已提交
298 299
            }
        }
S
sushuang 已提交
300
    }
301
});
S
sushuang 已提交
302 303

export default ThemeRiverSeries;