From 9e4c3b6926d90e58c236661b720f6b0ab5f30cc5 Mon Sep 17 00:00:00 2001 From: sushuang Date: Fri, 15 Feb 2019 16:20:01 +0800 Subject: [PATCH] Fix: Remove nest, which is not needed. --- src/chart/sankey/sankeyLayout.js | 36 +++---- src/chart/themeRiver/ThemeRiverSeries.js | 51 ++++----- src/util/model.js | 25 +++++ src/util/nest.js | 127 ----------------------- 4 files changed, 60 insertions(+), 179 deletions(-) delete mode 100644 src/util/nest.js diff --git a/src/chart/sankey/sankeyLayout.js b/src/chart/sankey/sankeyLayout.js index ef77ff02b..084b756fa 100644 --- a/src/chart/sankey/sankeyLayout.js +++ b/src/chart/sankey/sankeyLayout.js @@ -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) { @@ -230,15 +230,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); @@ -254,15 +246,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; } /** diff --git a/src/chart/themeRiver/ThemeRiverSeries.js b/src/chart/themeRiver/ThemeRiverSeries.js index 3e7915a27..5b7183f04 100644 --- a/src/chart/themeRiver/ThemeRiverSeries.js +++ b/src/chart/themeRiver/ThemeRiverSeries.js @@ -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,29 +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(comparer); - } - - function comparer(index1, index2) { - return data.get(timeDim, index1) - data.get(timeDim, 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; }, diff --git a/src/util/model.js b/src/util/model.js index f80ea06d1..653a8ef00 100644 --- a/src/util/model.js +++ b/src/util/model.js @@ -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}; +} diff --git a/src/util/nest.js b/src/util/nest.js deleted file mode 100644 index a1f833cd3..000000000 --- a/src/util/nest.js +++ /dev/null @@ -1,127 +0,0 @@ -/* -* 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 ). -*/ - - -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 -- GitLab