提交 3e676fde 编写于 作者: L lang

List#map, List#mapArray

上级 cbc05ff8
......@@ -57,7 +57,7 @@ define(function(require) {
// var dims = coordSys.type === 'cartesian2d' ? ['x', 'y'] : ['radius', 'angle'];
var baseDataOffset = valueDim === 'x' || valueDim === 'radius' ? 1 : 0;
return data.map([valueDim], function (val, idx) {
return data.mapArray([valueDim], function (val, idx) {
var stackedOnSameSign;
var stackedOn = data.stackedOn;
// Find first stacked value with same sign
......@@ -93,7 +93,7 @@ define(function(require) {
var lineStyleModel = seriesModel.getModel('itemStyle.normal.lineStyle');
var areaStyleModel = seriesModel.getModel('itemStyle.normal.areaStyle');
var points = data.map(data.getItemLayout, true);
var points = data.mapArray(data.getItemLayout, true);
var isCoordSysPolar = coordSys.type === 'polar';
var prevCoordSys = this._coordSys;
......
......@@ -43,8 +43,8 @@ define(function (require) {
oldCoordSys, newCoordSys
) {
var newNameList = newData.map(newData.getName);
var oldNameList = oldData.map(oldData.getName);
var newNameList = newData.mapArray(newData.getName);
var oldNameList = oldData.mapArray(oldData.getName);
var currPoints = [];
var nextPoints = [];
......
......@@ -31,7 +31,7 @@ define(function(require) {
ecModel.eachSeries(function (seriesModel) {
if (seriesModel.legendDataProvider) {
var data = seriesModel.legendDataProvider();
availableNames = availableNames.concat(data.map(data.getName));
availableNames = availableNames.concat(data.mapArray(data.getName));
}
});
/**
......
......@@ -104,7 +104,7 @@ define(function (require) {
if (!zrUtil.isArray(symbolType)) {
symbolType = [symbolType, symbolType];
}
if (typeof (+symbolSize) === 'number') {
if (typeof symbolSize === 'number') {
symbolSize = [symbolSize, symbolSize];
}
......
......@@ -59,7 +59,7 @@ define(function(require) {
* `[[10, 10], [20, 20], [30, 30]]`
*/
dataToPoints: function (data, stack) {
return data.map(['x', 'y'], function (x, y) {
return data.mapArray(['x', 'y'], function (x, y) {
return this.dataToPoint([x, y]);
}, stack, this);
},
......
......@@ -46,8 +46,8 @@ define(function(require, factory) {
*/
function ifAxisCrossZero (axis) {
var dataExtent = axis.scale.getExtent();
return (dataExtent[0] > 0 && dataExtent[1] > 0)
|| (dataExtent[0] < 0 && dataExtent[1] < 0)
return !((dataExtent[0] > 0 && dataExtent[1] > 0)
|| (dataExtent[0] < 0 && dataExtent[1] < 0))
|| ifAxisNeedsCrossZero(axis);
}
/**
......@@ -226,6 +226,17 @@ define(function(require, factory) {
this._coordsMap[key] = cartesian;
this._coordsList.push(cartesian);
cartesian.addAxis(xAxis);
cartesian.addAxis(yAxis);
}, this);
}, this);
this._updateCartesianFromSeries(ecModel, gridModel);
// Fix configuration
zrUtil.each(axesMap.x, function (xAxis) {
zrUtil.each(axesMap.y, function (yAxis) {
// onZero can not be used in these two situations
// 1. When other axis is a category axis
// 2. When other axis not across 0 point
......@@ -247,14 +258,9 @@ define(function(require, factory) {
xAxis.scale.unionExtent([0, 0]);
}
cartesian.addAxis(xAxis);
cartesian.addAxis(yAxis);
}, this);
}, this);
this._updateCartesianFromSeries(ecModel, gridModel);
function createAxisCreator(axisType) {
return function (axisModel, idx) {
if (!isAxisUsedInTheGrid(axisModel, gridModel, ecModel)) {
......
......@@ -12,7 +12,6 @@ define(function(require) {
dependencies: ['xAxis', 'yAxis'],
/**
/**
* @type {module:echarts/coord/cartesian/Grid}
*/
......
......@@ -139,7 +139,7 @@ define(function(require) {
* `[[10, 10], [20, 20], [30, 30]]`
*/
dataToPoints: function (data) {
return data.map(['radius', 'angle'], function (radius, angle) {
return data.mapArray(['radius', 'angle'], function (radius, angle) {
return this.dataToPoint([radius, angle]);
}, this);
},
......
......@@ -542,13 +542,14 @@ define(function (require) {
};
/**
* Data mapping
* @param {string|Array.<string>}
* Data mapping to a plain array
* @param {string|Array.<string>} [dimensions]
* @param {Function} cb
* @param {boolean} [stack=false]
* @param {*} [context=this]
* @return {Array}
*/
listProto.map = function (dimensions, cb, stack, context) {
listProto.mapArray = function (dimensions, cb, stack, context) {
if (typeof dimensions === 'function') {
context = stack;
stack = cb;
......@@ -563,6 +564,72 @@ define(function (require) {
return result;
};
/**
* Data mapping to a new List with given dimensions
* @param {string|Array.<string>} dimensions
* @param {Function} cb
* @param {boolean} [stack=false]
* @param {*} [context=this]
* @return {Array}
*/
listProto.map = function (dimensions, cb, stack, context) {
var list = new List(
zrUtil.map(dimensions, this.getDimensionInfo, this),
this.hostModel
);
// Following properties are all immutable.
// So we can reference to the same value
list._nameList = this._nameList;
var indices = list.indices = this.indices;
list._rawValueDims = this._rawValueDims;
list._optionModels = this._optionModels;
var storage = list._storage = {};
var thisStorage = this._storage;
// Init storage
for (var i = 0; i < dimensions.length; i++) {
var dim = dimensions[i];
var dimStore = thisStorage[dim];
if (dimStore) {
storage[dim] = new dimStore.constructor(
thisStorage[dim].length
);
}
}
storage.$optionModelIndices = thisStorage.$optionModelIndices;
var tmpRetValue = [];
this.each(dimensions, function () {
var idx = arguments[arguments.length - 1];
var retValue = cb && cb.apply(this, arguments);
if (retValue != null) {
// a number
if (typeof retValue === 'number') {
tmpRetValue[0] = retValue;
retValue = tmpRetValue;
}
for (var i = 0; i < retValue.length; i++) {
var dim = dimensions[i];
var dimStore = storage[dim];
var rawIdx = indices[idx];
if (dimStore) {
dimStore[rawIdx] = retValue[i];
}
}
}
});
// FIXME Value may already been stacked
list.stackedOn = this.stackedOn;
return list;
};
var temporaryModel = new Model(null);
/**
* Get model of one data item.
......
......@@ -123,5 +123,8 @@
})
</script>
<script src="js/memory-stats.js"></script>
<script src="js/memory.js"></script>
</body>
</html>
\ No newline at end of file
......@@ -57,8 +57,14 @@ describe('List', function () {
});
testCase('Stacked data', function (List) {
var list1 = new List(['x', 'y']);
var list2 = new List(['x', 'y']);
var list1 = new List(['x', {
name: 'y',
stackable: true
}]);
var list2 = new List(['x', {
name: 'y',
stackable: true
}]);
list1.initData([1, '-', 2, -2]);
list2.initData([1, 2, 3, 2]);
......@@ -102,6 +108,16 @@ describe('List', function () {
var list = new List(['x', 'y']);
list.initData([[10, 15], [20, 25], [30, 35]]);
expect(list.map(['x', 'y'], function (x, y) {
return [x + 2, y + 2];
}).mapArray('x', function (x) {
return x;
})).toEqual([12, 22, 32]);
});
testCase('mapArray', function (List) {
var list = new List(['x', 'y']);
list.initData([[10, 15], [20, 25], [30, 35]]);
expect(list.mapArray(['x', 'y'], function (x, y) {
return [x, y];
})).toEqual([[10, 15], [20, 25], [30, 35]]);
});
......@@ -111,7 +127,7 @@ describe('List', function () {
list.initData([[10, 15], [20, 25], [30, 35]]);
expect(list.filterSelf(['x', 'y'], function (x, y) {
return x < 30 && x > 10;
}).map('x', function (x) {
}).mapArray('x', function (x) {
return x;
})).toEqual([20]);
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册