Combining two csv's (d3)

前端 未结 2 1303
傲寒
傲寒 2021-01-23 08:24

I am loading two csv files using the following code

    d3.csv(\"sqrt100train.csv\", function(error, data2) {
    d3.csv(\"sqrt100test.csv\", function(error, dat         


        
相关标签:
2条回答
  • 2021-01-23 08:27

    If you want to create new array:

    var newArray = [];
    for(var i = 0; i < data.length; i++) {
        newArray.push({Trees: data[i].Trees,
                       Train: data[i].Train,
                       Test: data2[i].Test});
    }
    
    0 讨论(0)
  • 2021-01-23 08:49

    Problem: the length or the X value are different

    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
    

    This code fix it.

    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;
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题