为了实现这样的数据显示出来三个序列分别为郑州新乡安阳的电量就需要自己实现对这样数据的转换转换成如下的形式
月份 郑州电量 新乡电量 安阳电量
这样Ext的图表就能把它显示成三个序列了
我写了如下的函数实现这个功能
function CovertData(jsonDataidField fromField toField){
var result = [] curRecord =null num;
var fromFields = fromFieldsplit();
// 循环整个数组[{}{}{}]
for(var idx=;idx<jsonDatalength;idx++){
num = findIdx(result idField jsonData[idx][idField]);
if(num!=){
curRecord = result[num];
}
else{
curRecord = {};
};
// 循环每个json对象中的字段
for(var key in jsonData[idx]){
// 处理转换的数据内容
for(var i=;i<fromFieldslength;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==){
resultpush(curRecord);
}
}
return result;
}
function findIdx(jsonData columnName value){
for(var idx = ;idx<jsonDatalength;idx++){
if(jsonData[idx][columnName]==value)
return idx;
}
return ;
}
JsTestDriver的测试代码如下
TestCase("Test json data row to column"{
setUp:function(){
thisjsonData = [{yearmonth:ppq:spq:company:dfsoft}
{yearmonth:ppq:spq:company:dfsoft}
{yearmonth:ppq:spq:company:dfsoft}
{yearmonth:ppq:spq:company:dfsoft}
{yearmonth:ppq:spq:company:vcom}
{yearmonth:ppq:spq:company:vcom}
{yearmonth:ppq:spq:company:vcom}
{yearmonth:ppq:spq:company:vcom}];
var fromField = ppqspq toField = company idField = yearmonth;
thisresultData = CovertData(thisjsonDataidFieldfromField toField);
}
"test store has columns":function(){
var month = thisresultData[findIdx(thisresultDatayearmonth)];
var month = thisresultData[findIdx(thisresultDatayearmonth)];
var month = thisresultData[findIdx(thisresultDatayearmonth)];
var month = thisresultData[findIdx(thisresultDatayearmonth)];
assertEquals(thisresultDatalength);
assertEquals(month[dfsoftppq]);
assertEquals(month[vcomppq]);
assertEquals(month[dfsoftppq]);
assertEquals(month[vcomppq]);
assertEquals(month[dfsoftppq]);
assertEquals(month[vcomppq]);
assertEquals(month[dfsoftppq]);
assertEquals(month[vcomppq]);
assertEquals(month[dfsoftspq]);
assertEquals(month[vcomspq]);
assertEquals(month[dfsoftspq]);
assertEquals(month[vcomspq]);
assertEquals(month[dfsoftspq]);
assertEquals(month[vcomspq]);
assertEquals(month[dfsoftspq]);
assertEquals(month[vcomspq]);
}
})
测试通过表示转换成功