Skip to content

Commit

Permalink
Fix #1370, 添加数据抽希策略配置
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Mar 21, 2015
1 parent fe43881 commit e05c812
Showing 1 changed file with 72 additions and 5 deletions.
77 changes: 72 additions & 5 deletions src/chart/line.js
Expand Up @@ -27,6 +27,8 @@ define(function (require) {
// stack: null
xAxisIndex: 0,
yAxisIndex: 0,
// 'nearest', 'min', 'max', 'average'
dataFilter: 'nearest',
itemStyle: {
normal: {
// color: 各异,
Expand Down Expand Up @@ -516,9 +518,11 @@ define(function (require) {
}
else {
// 大数据模式截取pointList
singlePL = this._getLargePointList(orient, singlePL);
singlePL = this._getLargePointList(
orient, singlePL, serie.dataFilter
);
}

// 折线图
var polylineShape = new PolylineShape({
zlevel: this.getZlevelBase(),
Expand Down Expand Up @@ -647,7 +651,7 @@ define(function (require) {
/**
* 大规模pointList优化
*/
_getLargePointList: function(orient, singlePL) {
_getLargePointList: function(orient, singlePL, filter) {
var total;
if (orient === 'horizontal') {
total = this.component.grid.getWidth();
Expand All @@ -658,12 +662,75 @@ define(function (require) {

var len = singlePL.length;
var newList = [];

if (typeof(filter) != 'function') {
switch (filter) {
case 'min':
filter = function (arr) {
return Math.max.apply(null, arr);
};
break;
case 'max':
filter = function (arr) {
return Math.min.apply(null, arr);
};
break;
case 'average':
filter = function (arr) {
var total = 0;
for (var i = 0; i < arr.length; i++) {
total += arr[i];
}
return total / arr.length;
};
break;
default:
filter = function (arr) {
return arr[0];
}
}
}

var windowData = [];
for (var i = 0; i < total; i++) {
newList[i] = singlePL[Math.floor(len / total * i)];
var idx0 = Math.floor(len / total * i);
var idx1 = Math.min(Math.floor(len / total * (i + 1)), len);
if (idx1 <= idx0) {
continue;
}

for (var j = idx0; j < idx1; j++) {
windowData[j - idx0] = orient === 'horizontal'
? singlePL[j][1] : singlePL[j][0];
}

windowData.length = idx1 - idx0;
var filteredVal = filter(windowData);
var nearestIdx = -1;
var minDist = Infinity;
// 寻找值最相似的点,使用其其它属性
for (var j = idx0; j < idx1; j++) {
var val = orient === 'horizontal'
? singlePL[j][1] : singlePL[j][0];
var dist = Math.abs(val - filteredVal);
if (dist < minDist) {
nearestIdx = j;
minDist = dist;
}
}

var newItem = singlePL[nearestIdx].slice();
if (orient === 'horizontal') {
newItem[1] = filteredVal;
}
else {
newItem[0] = filteredVal;
}
newList.push(newItem);
}
return newList;
},

_getSmooth: function (isSmooth/*, pointList, orient*/) {
if (isSmooth) {
/* 不科学啊,发现0.3通用了
Expand Down

0 comments on commit e05c812

Please sign in to comment.