提交 69764d37 编写于 作者: P pah100

Merge branch 'master' of https://github.com/pissang/echarts-next

......@@ -8,8 +8,8 @@ define(function(require) {
type: 'bar',
getInitialData: function (option) {
return List.fromArray(option.data, 1, this);
getInitialData: function (option, ecModel) {
return List.fromArray(option.data, this, ecModel);
},
defaultOption: {
......
......@@ -8,8 +8,8 @@ define(function(require) {
type: 'line',
getInitialData: function (option) {
return List.fromArray(option.data, 1, this);
getInitialData: function (option, ecModel) {
return List.fromArray(option.data, this, ecModel);
},
defaultOption: {
......
......@@ -29,8 +29,8 @@ define(function(require) {
var cartesian = seriesModel.coordinateSystem;
var xAxis = cartesian.getAxis('x');
var yAxis = cartesian.getAxis('y');
var xExtent = xAxis.getCoordExtent();
var yExtent = yAxis.getCoordExtent();
var xExtent = xAxis.getExtent();
var yExtent = yAxis.getExtent();
var clipPath = new api.Rect({
shape: {
......
......@@ -50,7 +50,7 @@ define(function(require) {
var grid = gridModel.coordinateSystem;
var rect = grid.getRect();
var otherAxis = grid.getAxis(axis.type === 'x' ? 'y' : 'x');
var otherAxis = grid.getAxis(axis.dim === 'x' ? 'y' : 'x');
var position = 0;
if (axisModel.get('axisLine.onZero')) {
......@@ -148,8 +148,7 @@ define(function(require) {
var isOrdinalAxis = axis.scale.type === 'ordinal';
var axisPosition = axis.position;
var ticksCoords = isOrdinalAxis && axis.boundaryGap
? axis.getBandsCoords(true) : axis.getTicksCoords();
var ticksCoords = axis.getTicksPositions();
var tickLines = [];
for (var i = 0; i < ticksCoords.length; i++) {
......@@ -388,9 +387,7 @@ define(function(require) {
var splitLines = [];
var lineCount = 0;
var isOrdinalAxis = axis.scale.type === 'ordinal';
var ticksCoords = isOrdinalAxis && axis.boundaryGap
? axis.getBandsCoords(true) : axis.getTicksCoords();
var ticksCoords = axis.getTicksPositions();
var p1 = [];
var p2 = [];
......@@ -452,9 +449,7 @@ define(function(require) {
var areaColors = splitAreaModel.get('areaStyle.color');
var gridRect = gridModel.coordinateSystem.getRect();
var isOrdinalAxis = axis.scale.type === 'ordinal';
var ticksCoords = isOrdinalAxis && axis.boundaryGap
? axis.getBandsCoords(true) : axis.getTicksCoords();
var ticksCoords = axis.getTicksPositions();
var prevX;
var prevY;
......
define(function (require) {
var linearMap = require('../../util/number').linearMap;
var linearMap = require('../util/number').linearMap;
var zrUtil = require('zrender/core/util');
function fixExtentWithBands(extent, nTick) {
var size = extent[1] - extent[0];
var len = nTick;
var margin = size / len / 2;
extent[0] += margin;
extent[1] -= margin;
}
/**
* @name module:echarts/coord/CartesianAxis
* @constructor
*/
var Axis = function (dim, scale, coordExtent) {
var Axis = function (dim, scale, extent) {
/**
* Axis dimension. Such as 'x', 'y', 'z'
* Axis dimension. Such as 'x', 'y', 'z', 'angle', 'radius'
* @type {string}
*/
this.dim = dim;
......@@ -25,12 +32,18 @@ define(function (require) {
* @type {Array.<number>}
* @private
*/
this._coordExtent = coordExtent;
this._extent = extent;
/**
* @type {boolean}
*/
this.inverse = false;
/**
* Usually true when axis has a ordinal scale
* @type {boolean}
*/
this.onBand = false;
};
Axis.prototype = {
......@@ -41,8 +54,8 @@ define(function (require) {
* Get coord extent
* @return {Array.<number>}
*/
getCoordExtent: function () {
var ret = this._coordExtent.slice();
getExtent: function () {
var ret = this._extent.slice();
if (this.inverse) {
ret.reverse();
}
......@@ -54,19 +67,19 @@ define(function (require) {
* @param {number} min
* @param {number} max
*/
setCoordExtent: function (min, max) {
var extent = this._coordExtent;
setExtent: function (min, max) {
var extent = this._extent;
extent[0] = min;
extent[1] = max;
},
/**
* Map a data to coord. Data is the rank if it has a ordinal scale
* Map a data to extent. Data is the rank if it has a ordinal scale
* @param {number} data
* @param {boolean} clamp
* @return {number}
*/
dataToCoord: function (data, clamp) {
mapData: function (data, clamp) {
// PENDING
if (data == null || data === '-') {
return NaN;
......@@ -74,75 +87,89 @@ define(function (require) {
data = this.scale.normalize(data);
return linearMap(data, [0, 1], this.getCoordExtent(), clamp);
var extent = this.getExtent();
if (this.onBand && this.scale.type === 'ordinal') {
fixExtentWithBands(extent, this.scale.getExtentSize());
}
return linearMap(data, [0, 1], extent, clamp);
},
/**
* Map a coord to data. Data is the rank if it has a ordinal scale
* @param {number} coord
* Unmap a data. Data is the rank if it has a ordinal scale
* @param {number} mapped
* @param {boolean} clamp
* @return {number}
*/
coordToData: function (coord, clamp) {
var t = linearMap(coord, this.getCoordExtent(), [0, 1], clamp);
unmapData: function (mapped, clamp) {
var extent = this.getExtent();
if (this.onBand && this.scale.type === 'ordinal') {
fixExtentWithBands(extent, this.scale.getExtentSize());
}
var t = linearMap(mapped, extent, [0, 1], clamp);
return this.scale.scale(t);
},
/**
* @return {ticks}
* @return {Array.<number>}
*/
getTicksCoords: function () {
getTicksPositions: function () {
var ticks = this.scale.getTicks();
return zrUtil.map(ticks, function (data) {
return this.dataToCoord(data);
}, this);
if (this.onBand && this.scale.type === 'ordinal') {
var bands = this.getBands();
var ret = [];
for (var i = 0; i < bands.length; i++) {
ret.push(bands[i][0]);
}
if (bands[i - 1]) {
ret.push(bands[i - 1][1]);
}
return ret;
}
else {
return zrUtil.map(ticks, this.mapData, this);
}
},
/**
* Get coords of bands.
* Get bands.
* If axis has ticks [1, 2, 3, 4]. Bands on the axis are
* |---1---|---2---|---3---|---4---|. And band coords is an array of coords
* where `|` is. It is useful when axis has an ordinal scale.
* |---1---|---2---|---3---|---4---|.
*
* @param {boolean} [margin=false]
* If margin is true. Coord extent is start at the position of first tick and end
* at the position of last tick.
* @return {Array.<number>}
* @return {Array}
*/
getBandsCoords: function (margin) {
var coordExtent = this._coordExtent;
var extent = this.scale.getExtent();
var coords = [];
var len = extent[1] - extent[0] + 1;
var startCoord = coordExtent[0];
var endCoord = coordExtent[1];
var size = endCoord - startCoord;
if (margin) {
var marginSize = size / (len * 2 - 2);
startCoord -= marginSize;
size += marginSize * 2;
}
for (var i = 0; i <= len; i++) {
coords.push(size * i / len + startCoord);
getBands: function () {
var extent = this._extent;
var bands = [];
var len = this.scale.getExtentSize() + 1;
var start = extent[0];
var end = extent[1];
var size = end - start;
for (var i = 1; i <= len; i++) {
bands.push([
size * (i - 1) / len + start,
size * i / len + start
]);
}
return coords;
return bands;
},
/**
* Get width of band
* @param {boolean} margin
* @return {number}
*/
getBandWidth: function (margin) {
var coordExtent = this._coordExtent;
getBandWidth: function () {
var axisExtent = this._extent;
var extent = this.scale.getExtent();
var len = extent[1] - extent[0] + 1;
var size = coordExtent[1] - coordExtent[0];
if (margin) {
size += size / (len - 1);
}
var size = axisExtent[1] - axisExtent[0];
return Math.abs(size) / len;
}
};
......
define(function (require) {
var zrUtil = require('zrender/core/util');
var Axis = require('./Axis');
var Axis = require('../Axis');
/**
* Extend axis 2d
......@@ -33,11 +33,6 @@ define(function (require) {
* - 'right'
*/
this.position = position || 'bottom';
/**
* @type {boolean}
*/
this.boundaryGap = false;
};
Axis2D.prototype = {
......@@ -47,7 +42,11 @@ define(function (require) {
isHorizontal: function () {
var position = this.position;
return position === 'top' || position === 'bottom';
}
},
dataToCoord: Axis.prototype.mapData,
coordToData: Axis.prototype.unmapData
};
zrUtil.inherits(Axis2D, Axis);
......
......@@ -8,7 +8,6 @@ define(function (require) {
'use strict';
var zrUtil = require('zrender/core/util');
var Axis = require('./Axis');
function dimAxisMapper(dim) {
return this._axes[dim];
......@@ -27,11 +26,6 @@ define(function (require) {
* @type {string}
*/
this.name = name || '';
/**
* Series using this cartesian coordinate system
* @type {Array.<Object>}
*/
this.series = [];
};
Cartesian.prototype = {
......@@ -70,19 +64,6 @@ define(function (require) {
);
},
/**
* Create a basic axis
* @param {number|string} dim
* @return {module:echarts/coord/Cartesian.Axis}
*/
createAxis: function (dim, scale, coordExtent) {
var axis = new Axis(dim, scale, coordExtent);
this.addAxis(axis);
return axis;
},
/**
* Add axis
* @param {module:echarts/coord/Cartesian.Axis}
......
......@@ -22,25 +22,17 @@ define(function(require) {
* `[[10, 10], [20, 20], [30, 30]]`
*/
dataToCoords: function (data) {
return data.map(this.dataToCoord, this);
},
dataToCoord: function (dataItem) {
var xAxis = this.getAxis('x');
var yAxis = this.getAxis('y');
var xIndex = xAxis.isHorizontal() ? 0 : 1;
// If y axis is category axis
var categoryAxis = this.getAxesByScale('ordinal')[0];
var swapAxis = categoryAxis && categoryAxis.dim === 'y';
var x = dataItem.getX();
var y = categoryAxis ? dataItem.getValue() : dataItem.getY();
var coord = [];
coord[xIndex] = xAxis.dataToCoord(swapAxis ? y : x);
coord[1 - xIndex] = yAxis.dataToCoord(swapAxis ? x : y);
return coord;
return data.map(function (dataItem) {
var coord = [];
coord[xIndex] = xAxis.dataToCoord(dataItem.getX());
coord[1 - xIndex] = yAxis.dataToCoord(dataItem.getY());
return coord;
}, this);
}
};
......
......@@ -134,18 +134,9 @@ define(function(require, factory) {
}
axis.otherCoord = otherCoord;
// Category axis with boundary gap. Which label and points are on the center of bands
// Insead of on the tick
if (axis.boundaryGap && axis.type === 'category') {
var size = extent[1] - extent[0];
var len = axis.scale.getTicks().length;
var margin = size / len / 2;
extent[0] += margin;
extent[1] -= margin;
}
var start = axis.isHorizontal() ? 0 : 1;
axis.setCoordExtent(extent[start], extent[1 - start]);
axis.setExtent(extent[start], extent[1 - start]);
});
},
......@@ -199,7 +190,7 @@ define(function(require, factory) {
xAxisModel.get('type'),
xAxisPosition
);
axisX.boundaryGap = xAxisModel.get('boundaryGap');
axisX.onBand = xAxisModel.get('boundaryGap');
axisX.inverse = xAxisModel.get('inverse');
// Inject axis into axisModel
......@@ -226,7 +217,7 @@ define(function(require, factory) {
yAxisModel.get('type'),
yAxisModel.get('position')
);
axisY.boundaryGap = yAxisModel.get('boundaryGap');
axisY.onBand = yAxisModel.get('boundaryGap');
axisY.inverse = yAxisModel.get('inverse');
yAxisModel.axis = axisY;
......
......@@ -29,14 +29,42 @@ define(function(require) {
/**
* @name echarts/data/List~Entry
* @extends {module:echarts/model/Model}
*
* @param {Object} option
* @param {module:echarts/model/Model} parentModel
* @param {number} dataIndex
* @param {Array.<string>} [independentVar=['x']]
* @param {Array.<string>} [dependentVar='y']
*/
var Entry = Model.extend({
layout: null,
dimension: 1,
/**
* @type {number}
* @protected
*/
xIndex: 0,
/**
* @type {number}
* @protected
*/
yIndex: 1,
/**
* @type {number}
* @protected
*/
zIndex: -1,
/**
* @type {number}
* @protected
*/
valueIndex: 1,
init: function (option, parentModel, dataIndex) {
init: function (option, parentModel, dataIndex, independentVar, dependentVar) {
/**
* @type {string}
......@@ -47,102 +75,42 @@ define(function(require) {
this.option = option;
var value = option.value == null ? option : option.value;
if (typeof value === 'number') {
value = [dataIndex, value];
}
if (dependentVar) {
for (var i = 0; i < dependentVar.length; i++) {
this[dependentVar[i] + 'Index'] = i;
}
this.valueIndex = value.length - 1;
this[independentVar + 'Index'] = this.valueIndex;
}
/**
* @type {number|Array}
* @type {Array.<number>}
* @memeberOf module:echarts/data/List~Entry
* @private
*/
this._value = option.value == null ? option : option.value
this._value = value;
/**
* @private
* @readOnly
*/
this.dataIndex = dataIndex || 0;
},
/**
* @return {number}
*/
getX: function () {
// Use idx as x if data is 1d
// Usually when xAxis is category axis
return this.dimension === 1 ? this.dataIndex : this._value[0];
},
/**
* @param {number} x
*/
setX: function (x) {
if (this.dimension > 1) {
this._value[0] = x;
}
this.dataIndex = dataIndex;
},
/**
* @return {number}
*/
getY: function () {
if (this.dimension > 1) {
return this._value[1];
}
},
/**
* @param {number} y
*/
setY: function (y) {
if (this.dimension > 1) {
this._value[1] = y;
}
},
/**
* @return {number}
*/
getZ: function () {
if (this.dimension > 2) {
return this._value[2];
}
},
/**
* @param {number} z
*/
setZ: function (z) {
if (this.dimension > 2) {
this._value[2] = z;
}
},
/**
* @return {number}
*/
getValue: function () {
var value;
// PENDING
// Value is a single number if data is 1d
if (this.dimension === 1) {
value = this._value;
}
else {
value = this._value[this.dimension];
}
if (value === '-') {
value = null;
}
return value;
},
/**
* @param {number} value
*/
setValue: function (value) {
if (this.dimension === 1) {
this._value = value;
}
else {
this._value[this.dimension] = value
getStackedValue: function () {
if (this.dimension !== 1) {
// Only 1d value support stack
return this.getValue();
}
},
......@@ -151,22 +119,33 @@ define(function(require) {
this.option, this.parentModel, this.dataIndex
);
entry.name = this.name;
entry.dimension = this.dimension;
entry.xIndex = this.xIndex;
entry.yIndex = this.yIndex;
entry.zIndex = this.zIndex;
entry.valueIndex = this.valueIndex;
return entry;
}
});
function List() {
zrUtil.each(['x', 'y', 'z', 'value'], function (dim) {
var capitalized = dim[0].toUpperCase() + dim.substr(1);
var indexKey = dim + 'Index';
Entry.prototype['get' + capitalized] = function () {
var index = this[indexKey];
if (index >= 0) {
return this._value[index];
}
}
Entry.prototype['set' + capitalized] = function (val) {
var index = this[indexKey];
if (index >= 0) {
this._value[indexKey] = val;
}
}
});
function List() {
this.elements = [];
// Depth and properties is useful in nested Array.
// For example in eventRiver, data structure is a nested 2d array as following
// [{evolution: []}, {evolution: []}]
// In this situation. depth should be 2 and properties should be ['evolution']
this.depth = 1;
this.properties = [];
}
List.prototype = {
......@@ -177,18 +156,12 @@ define(function(require) {
each: function (cb, context) {
context = context || this;
if (this.depth > 1) {
createArrayIterWithDepth(
this.depth, this.properties, cb, context, 'each'
)(this.elements, 0);
}
else {
zrUtil.each(this.elements, cb, context);
}
zrUtil.each(this.elements, cb, context);
},
/**
* Data mapping, returned array is flatten
* PENDING
*/
map: function (cb, context) {
var ret = [];
......@@ -198,36 +171,12 @@ define(function(require) {
return ret;
},
/**
* In-place filter
*/
filterInPlace: function (cb, context) {
filter: function (cb, context) {
var list = new List();
context = context || this;
if (this.depth > 1) {
createArrayIterWithDepth(
this.depth, this.properties, cb, context, 'filter'
)(this.elements, 0);
}
else {
this.elements = zrUtil.filter(this.elements, cb, context);
}
list.elements = zrUtil.filter(this.elements, cb, context);
},
/**
* In-place map
*/
// mapInPlace: function (cb, context) {
// context = context || this;
// if (this.depth > 1) {
// createArrayIterWithDepth(
// this.depth, this.properties, cb, context, 'map'
// )(this.elements, 0);
// }
// else {
// this.elements = zrUtil.map(this.elements, cb, context);
// }
// },
/**
* @return {module:echarts/data/List~Entry}
*/
......@@ -285,16 +234,41 @@ define(function(require) {
};
});
List.fromArray = function (data, dimension, parentModel) {
List.fromArray = function (data, seriesModel, ecModel) {
var xAxisModel = ecModel.getComponent('xAxis', seriesModel.get('xAxisIndex'));
var yAxisModel = ecModel.getComponent('yAxis', seriesModel.get('yAxisIndex'));
var independentVar;
var dependentVar;
// if (xAxisModel.get('type') === 'category') {
// independentVar = ['x'];
// dependentVar = 'y';
// }
// else if (yAxisModel.get('type') === 'category') {
// independentVar = ['y'];
// dependentVar = 'x';
// }
// else {
// var dim = data[0] && data[0].length;
// if (dim === 2) {
// independentVar = ['x'];
// dependentVar = 'y';
// }
// else if (dim === 3) {
// independentVar = ['x', 'y'];
// dependentVar = 'z';
// }
// }
var list = new List();
// Normalize data
list.elements = zrUtil.map(data, function (dataItem, index) {
var entry = new Entry(dataItem, parentModel, index);
var entry = new Entry(dataItem, seriesModel, index, independentVar, dependentVar);
// TODO
if (! dataItem.name) {
entry.name = index;
}
entry.dimension = dimension || 1;
return entry;
});
return list;
......
/**
* TODO processor的优先级
* restore
* setTheme
* stack
* axis position 统一处理
*/
define(function (require) {
......@@ -155,6 +155,8 @@ define(function (require) {
},
updateImmediately: function () {
// console.time('update');
var ecModel = this._model;
ecModel.restore();
......@@ -172,6 +174,8 @@ define(function (require) {
this._doRender(ecModel);
this._needsUpdate = false;
// console.timeEnd('update');
},
resize: function () {
......@@ -325,16 +329,24 @@ define(function (require) {
}, this);
// Remove groups of charts
zrUtil.each(this._chartsList, function (chart) {
this._zr.remove(chart.group);
chart.__keepAlive = false;
}, this);
// Render all charts
ecModel.eachSeries(function (seriesModel, idx) {
var id = getSeriesId(seriesModel.option, idx);
var chart = this._chartsMap[id];
chart.__keepAlive = true;
chart.render(seriesModel, ecModel, api);
this._zr.add(chart.group);
}, this);
// Remove groups of charts
zrUtil.each(this._chartsList, function (chart) {
if (!chart.__keepAlive) {
this._zr.remove(chart.group);
chart.remove();
}
}, this);
},
dispose: function () {
......
/**
* Linear continuous scale
* @module echarts/coord/scale/Ordinal
*
*
* http://en.wikipedia.org/wiki/Level_of_measurement
*/
......@@ -26,7 +26,7 @@ define(function (require) {
};
OrdinalScale.prototype = {
constructor: OrdinalScale,
type: 'ordinal',
......@@ -93,13 +93,20 @@ define(function (require) {
return ticks;
},
/**
* @return {number}
*/
getExtentSize: function () {
return this._extent[1] - this._extent[0];
},
/**
* Get item on rank n
*/
getItem: function (n) {
return this._list[n];
},
// Do nothing
niceTicks: function () {},
niceExtent: function () {},
......
......@@ -20,6 +20,8 @@ define(function (require) {
render: function () {},
remove: function () {},
dispose: function () {}
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册