Month Zhengzhou-power Xinxiang-power Anyang-power
201201 33 29 23
201202 35 26 25
201203 34 27 24
201204 36 28 26
201205 34.3 28.8 24.3
In this way, the Ext chart can display it into three sequences.
I wrote the following function to implement this function:
Copy codeThe Code is as follows:
Function CovertData (jsonData, idField, fromField, toField ){
Var result = [], curRecord = null, num;
Var fromFields = fromField. split (',');
// Loop the entire array: [{...}, {...},...]
For (var idx = 0; idx <jsonData. length; idx ++ ){
Num = findIdx (result, idField, jsonData [idx] [idField]);
If (num! =-1 ){
CurRecord = result [num];
}
Else {
CurRecord = {};
};
// Loop the fields in each json object
For (var key in jsonData [idx]) {
// Process the converted data content
For (var I = 0; I <fromFields. length; I ++ ){
If (key = fromFields [I]) {
CurRecord [jsonData [idx] [toField] + '-' + fromFields [I] = jsonData [idx] [key];
Break;
}
}
// Except the data content, only the ID field data is processed
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;
}
The JsTestDriver test code is as follows:
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 = 'earmonth ';
This. resultData = CovertData (this. jsonData, idField, fromField, toField );
},
"Test store has columns": function (){
Var month1 = this. resultData [findIdx (this. resultData, 'earmonth', 201201)];
Var mon2nd = this. resultData [findIdx (this. resultData, 'earmonth', 201202)];
Var month3 = this. resultData [findIdx (this. resultData, 'earmonth', 201203)];
Var month4 = this. resultData [findIdx (this. resultData, 'earmonth', 201204)];
AssertEquals (4, this. resultData. length );
AssertEquals ('23', month1 ['dfsoft-ppq']);
AssertEquals ('29', month1 ['vcom-ppq']);
AssertEquals ('33', mon2's ['dfsoft-ppq']);
AssertEquals ('34', mon2's ['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', mon2's ['dfsoft-spq']);
AssertEquals ('38', mon2's ['vcom-spq']);
AssertEquals ('49', month3 ['dfsoft-spq']);
AssertEquals ('43 ', month3 ['vcom-spq']);
AssertEquals ('51', month4 ['dfsoft-spq']);
AssertEquals ('59', month4 ['vcom-spq']);
}
})
If the test succeeds, the conversion is successful.