1. 程式人生 > >js實現json資料行到列的轉換

js實現json資料行到列的轉換

前面實現了Ext動態生成圖表的功能,滿足了基本的要求。後面使用者提出這樣的問題:有如下的資料:

月份                   電量                   單位

201201              33                       鄭州

201202              35                       鄭州

201203              34                       鄭州

201204              36                       鄭州

201205              34.3                    鄭州

201201              29                       新鄉

201202              26                       新鄉

201203              27                       新鄉

201204              28                       新鄉

201205              28.8                    新鄉

201201              23                       安陽

201202              25                       安陽

201203              24                       安陽

201204              26                       安陽

201205              24.3                    安陽

預期能夠顯示鄭州、安陽、新鄉三個序列,但Ext圖表本身不支援把這樣的資料顯示成三個圖表序列:如果X軸選月份,y軸選電量,就會顯示成三遍201201—201205的資料,而顯示不出來鄭州、新鄉和安陽的標識。

為了實現這樣的資料顯示出來三個序列,分別為鄭州、新鄉、安陽的電量,就需要自己實現對這樣資料的轉換,轉換成如下的形式:

月份                   鄭州-電量        新鄉-電量                 安陽-電量

201201              33                       29                                 23    

201202              35                       26                                 25    

201203              34                       27                                 24    

201204              36                       28                                 26    

201205              34.3                    28.8                             24.3

這樣,Ext的圖表就能把它顯示成三個序列了。

我寫了如下的函式實現這個功能:

function CovertData(jsonData,idField, fromField, toField){
    var result = [], curRecord =null, num;
    var fromFields = fromField.split(',');
    // 迴圈整個陣列:[{...},{...},{...},...]
    for(var idx=0;idx<jsonData.length;idx++){
        num = findIdx(result, idField, jsonData[idx][idField]);
        if(num!=-1){
            curRecord = result[num];
        }
        else{
            curRecord = {};
        };
        // 迴圈每個json物件中的欄位
        for(var key in jsonData[idx]){
            // 處理轉換的資料內容
            for(var i=0;i<fromFields.length;i++){
                if(key == fromFields[i]){
                    curRecord[jsonData[idx][toField]+'-' + fromFields[i]] = jsonData[idx][key];
                    break;
                }
            }
            // 除資料內容外,只處理標識欄位資料
            if(key == idField){
                curRecord[key] = jsonData[idx][key];
            }
        }
        if(num==-1){
            result.push(curRecord);
        }
    }
    return result;
}

function findIdx(jsonData, columnName, value){
    for(var idx = 0;idx<jsonData.length;idx++){
        if(jsonData[idx][columnName]==value)
            return idx;
    }
    return -1;
}

JsTestDriver的測試程式碼如下:

TestCase("Test json data row to column",{
    setUp:function(){
        this.jsonData = [{yearmonth:201201,ppq:23,spq:27,company:'dfsoft'},
            {yearmonth:201202,ppq:33,spq:38,company:'dfsoft'},
            {yearmonth:201203,ppq:43,spq:49,company:'dfsoft'},
            {yearmonth:201204,ppq:53,spq:51,company:'dfsoft'},
            {yearmonth:201201,ppq:29,spq:26,company:'vcom'},
            {yearmonth:201202,ppq:34,spq:38,company:'vcom'},
            {yearmonth:201203,ppq:48,spq:43,company:'vcom'},
            {yearmonth:201204,ppq:52,spq:59,company:'vcom'}];

        var fromField = 'ppq,spq', toField = 'company', idField = 'yearmonth';
        this.resultData = CovertData(this.jsonData,idField,fromField, toField);
    },
    "test store has columns":function(){
        var month1 = this.resultData[findIdx(this.resultData,'yearmonth',201201)];
        var month2 = this.resultData[findIdx(this.resultData,'yearmonth',201202)];
        var month3 = this.resultData[findIdx(this.resultData,'yearmonth',201203)];
        var month4 = this.resultData[findIdx(this.resultData,'yearmonth',201204)];

        assertEquals(4,this.resultData.length);
        assertEquals('23',month1['dfsoft-ppq']);
        assertEquals('29',month1['vcom-ppq']);
        assertEquals('33',month2['dfsoft-ppq']);
        assertEquals('34',month2['vcom-ppq']);
        assertEquals('43',month3['dfsoft-ppq']);
        assertEquals('48',month3['vcom-ppq']);
        assertEquals('53',month4['dfsoft-ppq']);
        assertEquals('52',month4['vcom-ppq']);

        assertEquals('27',month1['dfsoft-spq']);
        assertEquals('26',month1['vcom-spq']);
        assertEquals('38',month2['dfsoft-spq']);
        assertEquals('38',month2['vcom-spq']);
        assertEquals('49',month3['dfsoft-spq']);
        assertEquals('43',month3['vcom-spq']);
        assertEquals('51',month4['dfsoft-spq']);
        assertEquals('59',month4['vcom-spq']);
    }
})

測試通過,表示轉換成功。