问题:长度或X值不同
serie1
X, Y
1, 11
3, 13
5, 15
6, 16
serie2
X, Y
1, 21
2, 22
4, 24
6, 26
7, 27
BothSeries
X, Y1, Y2
1, 11, 21
2, , 22
3, 13,
4, , 24
5, 15,
6, 16, 26
7, , 27
此代码修复它。
var serie1 = [];
var serie2 = [];
var BothSeries = [[null,null,null]];
function merge() {
BothSeries = [];
var i, j, aux;
//copy serie1 a BothSeries
if(serie1.length!=0){
for (var i = 0; i < serie1.length; i++) {
//null represent the serie2 value
BothSeries[i] = [serie1[i][0], serie1[i][1],null];
}
}
//set the value Y of serie2 in the BothSeries when their X are equals.
if(serie2.length!=0){
for (var i = 0; i < serie2.length; i++) {
for (var j = 0; j < BothSeries.length; j++) {
if(BothSeries[j][0] == serie2[i][0]){
BothSeries[j][2] = serie2[i][1];
}
}
}
}
//set the value X and Y of serie2 in the BothSeries when their X aren't equals or doesn't exist.
var cont=0;
if(serie2.length!=0){
for (var i = 0; i < serie2.length; i++) {
for (var j = 0; j < BothSeries.length; j++) {
if(BothSeries[j][0] != serie2[i][0]){
cont++;
}
}
if(cont==BothSeries.length)
{
BothSeries[BothSeries.length] = [serie2[i][0],null,serie2[i][1]];
}
cont=0;
}
}
//order with bubble method by X
if(BothSeries.length!=0){
for(i=0;i<BothSeries.length-1;i++){
for(j=0;j<BothSeries.length-i-1;j++){
if(BothSeries[j+1][0] < BothSeries[j][0])
{
aux=BothSeries[j+1];
BothSeries[j+1]=BothSeries[j];
BothSeries[j]=aux;
}
}
}
}
}