提交 4d1a2262 编写于 作者: S sushuang

Fix: Remove nest, which is not needed.

上级 2b86ddd8
......@@ -23,8 +23,8 @@
*/
import * as layout from '../../util/layout';
import nest from '../../util/nest';
import * as zrUtil from 'zrender/src/core/util';
import {groupData} from '../../util/model';
import { __DEV__ } from '../../config';
export default function (ecModel, api, payload) {
......@@ -294,15 +294,7 @@ function scaleNodeBreadths(nodes, kx, orient) {
* @param {number} iterations the number of iterations for the algorithm
*/
function computeNodeDepths(nodes, edges, height, width, nodeGap, iterations, orient) {
var nodesByBreadth = nest()
.key(getKeyFunction(orient))
.sortKeys(function (a, b) {
return a - b;
})
.entries(nodes)
.map(function (d) {
return d.values;
});
var nodesByBreadth = prepareNodesByBreadth(nodes, orient);
initializeNodeDepth(nodes, nodesByBreadth, edges, height, width, nodeGap, orient);
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient);
......@@ -318,15 +310,21 @@ function computeNodeDepths(nodes, edges, height, width, nodeGap, iterations, ori
}
}
function getKeyFunction(orient) {
if (orient === 'vertical') {
return function (d) {
return d.getLayout().y;
};
}
return function (d) {
return d.getLayout().x;
};
function prepareNodesByBreadth(nodes, orient) {
var nodesByBreadth = [];
var keyAttr = orient === 'vertical' ? 'y' : 'x';
var groupResult = groupData(nodes, function (node) {
return node.getLayout()[keyAttr];
});
groupResult.keys.sort(function (a, b) {
return a - b;
});
zrUtil.each(groupResult.keys, function (key) {
nodesByBreadth.push(groupResult.buckets.get(key));
});
return nodesByBreadth;
}
/**
......
......@@ -27,8 +27,8 @@ import createDimensions from '../../data/helper/createDimensions';
import {getDimensionTypeByAxis} from '../../data/helper/dimensionHelper';
import List from '../../data/List';
import * as zrUtil from 'zrender/src/core/util';
import {groupData} from '../../util/model';
import {encodeHTML} from '../../util/format';
import nest from '../../util/nest';
var DATA_NAME_INDEX = 2;
......@@ -69,18 +69,12 @@ var ThemeRiverSeries = SeriesModel.extend({
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
};
var groupResult = groupData(data, function (item) {
return item[2];
});
var layData = [];
groupResult.buckets.each(function (items, key) {
layData.push({name: key, dataList: items});
});
var layerNum = layData.length;
......@@ -201,27 +195,20 @@ var ThemeRiverSeries = SeriesModel.extend({
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
};
});
var timeDim = data.mapDimension('single');
for (var j = 0; j < layerSeries.length; ++j) {
layerSeries[j].indices.sort(function (index1, index2) {
// data group by name
var groupResult = groupData(indexArr, function (index) {
return data.get('name', index);
});
var layerSeries = [];
groupResult.buckets.each(function (items, key) {
items.sort(function (index1, index2) {
return data.get(timeDim, index1) - data.get(timeDim, index2);
});
}
layerSeries.push({name: key, indices: items});
});
return layerSeries;
},
......
......@@ -534,3 +534,28 @@ export function getTooltipRenderMode(renderModeOption) {
return renderModeOption || 'html';
}
}
/**
* Group a list by key.
*
* @param {Array} array
* @param {Function} getKey
* param {*} Array item
* return {string} key
* @return {Object} Result
* {Array}: keys,
* {module:zrender/core/util/HashMap} buckets: {key -> Array}
*/
export function groupData(array, getKey) {
var buckets = zrUtil.createHashMap();
var keys = [];
zrUtil.each(array, function (item) {
var key = getKey(item);
(buckets.get(key)
|| (keys.push(key), buckets.set(key, []))
).push(item);
});
return {keys: keys, buckets: buckets};
}
/*
* 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.
*/
/*
* The implementation references to d3.js. The use of the source
* code of this file is also subject to the terms and consitions
* of its license (BSD-3Clause, see <echarts/src/licenses/LICENSE-d3>).
*/
import * as zrUtil from 'zrender/src/core/util';
/**
* nest helper used to group by the array.
* can specified the keys and sort the keys.
*/
export default function nest() {
var keysFunction = [];
var sortKeysFunction = [];
/**
* map an Array into the mapObject.
* @param {Array} array
* @param {number} depth
*/
function map(array, depth) {
if (depth >= keysFunction.length) {
return array;
}
var i = -1;
var n = array.length;
var keyFunction = keysFunction[depth++];
var mapObject = {};
var valuesByKey = {};
while (++i < n) {
var keyValue = keyFunction(array[i]);
var values = valuesByKey[keyValue];
if (values) {
values.push(array[i]);
}
else {
valuesByKey[keyValue] = [array[i]];
}
}
zrUtil.each(valuesByKey, function (value, key) {
mapObject[key] = map(value, depth);
});
return mapObject;
}
/**
* transform the Map Object to multidimensional Array
* @param {Object} map
* @param {number} depth
*/
function entriesMap(mapObject, depth) {
if (depth >= keysFunction.length) {
return mapObject;
}
var array = [];
var sortKeyFunction = sortKeysFunction[depth++];
zrUtil.each(mapObject, function (value, key) {
array.push({
key: key, values: entriesMap(value, depth)
});
});
if (sortKeyFunction) {
return array.sort(function (a, b) {
return sortKeyFunction(a.key, b.key);
});
}
return array;
}
return {
/**
* specified the key to groupby the arrays.
* users can specified one more keys.
* @param {Function} d
*/
key: function (d) {
keysFunction.push(d);
return this;
},
/**
* specified the comparator to sort the keys
* @param {Function} order
*/
sortKeys: function (order) {
sortKeysFunction[keysFunction.length - 1] = order;
return this;
},
/**
* the array to be grouped by.
* @param {Array} array
*/
entries: function (array) {
return entriesMap(map(array, 0), 0);
}
};
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册