月份鄭州-電量新鄉-電量安陽-電量
201201 33 29 23
201202 35 26 25
201203 34 27 24這樣,Ext的圖表就能把它顯示成三個序列了。
我寫瞭如下的函數實現這個功能:
function findIdx(jsonData, columnName, value){
for(var idx = 0;idx
if(jsonData[idx][columnName]==value)
if(jsonData[idx][columnName]==value)
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 :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 month3 = this.resultData[findIdx(this.resultData,'yearmonth',201203)];
var month4 = this.resultDataf ,'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('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'] );