我需要在深入的图表中传递值,我的JSON格式无法正常工作,所以请帮我解决这个问题。
public function actionIndex ()
{
$array = []; // for array result
$field1 = ''; // if fix value $field1 = 'a'; can pass a to $result
$field2 = ''; // if fix value $field2 = 'b'; can pass b to $result
if (Yii::$app->request->isPost)
{
$FISTL = $_POST['field1']; // view ~field1
$FIPEX = $_POST['field2']; // view ~field2
}
if($field1 !== '' && $field1 !== ''){ *// add if condition for get variable*
$connection = Yii::$app->sconnection->connectionToAnotherSystem(); // connection to another system
$result = $connection->getValue([
'field1' => $field1, // if fix value $field1 = 'a'; can pass a to $result
'field2' => $field2, // if fix value $field2 = 'b'; can pass b to $result
]);
$array = array(['value' =>$result]);//return value from another system
} $dataProvider = new ArrayDataProvider([
'allModels' => $array,
]);
return $this->render('index',
[
'dataProvider' => $dataProvider, // true data
]);
}
我的JSON格式如下,
this.MemberProvider.totalMonthlyRevenueByBranch(getmonthmonthwisr)
.subscribe(data => {
this.graphdata =data;
console.log(this.graphdata);
})
如何获取这种JSON结构
[
{
"_id":{
"year":2018
},
"data":[
{
"year":2018,
"total":10260,
"month":"Mar"
}
],
"overall":10260
},
{
"_id":{
"year":2019
},
"data":[
{
"year":2019,
"total":14160,
"month":"Dec"
},
{
"year":2019,
"total":14160,
"month":"Nov"
},
{
"year":2019,
"total":14259,
"month":"Oct"
},
{
"year":2019,
"total":14160,
"month":"Sep"
},
{
"year":2019,
"total":14259,
"month":"Aug"
},
{
"year":2019,
"total":14160,
"month":"July"
},
{
"year":2019,
"total":28518,
"month":"Jun"
},
{
"year":2019,
"total":14259,
"month":"May"
},
{
"year":2019,
"total":185185,
"month":"Apr"
},
{
"year":2019,
"total":54739,
"month":"Mar"
},
{
"year":2019,
"total":28518,
"month":"Feb"
},
{
"year":2019,
"total":28518,
"month":"Jan"
}
],
"overall":424895
}
]
答案 0 :(得分:0)
这是我很快想到的。优化此代码由您决定。您可能使用for-loop
而不是map
。完全由您决定
var data = [
{
"_id":{
"year":2018
},
"data":[
{
"year":2018,
"total":10260,
"month":"Mar"
}
],
"overall":10260
},
{
"_id":{
"year":2019
},
"data":[
{
"year":2019,
"total":14160,
"month":"Dec"
},
{
"year":2019,
"total":14160,
"month":"Nov"
},
{
"year":2019,
"total":14259,
"month":"Oct"
},
{
"year":2019,
"total":14160,
"month":"Sep"
},
{
"year":2019,
"total":14259,
"month":"Aug"
},
{
"year":2019,
"total":14160,
"month":"July"
},
{
"year":2019,
"total":28518,
"month":"Jun"
},
{
"year":2019,
"total":14259,
"month":"May"
},
{
"year":2019,
"total":185185,
"month":"Apr"
},
{
"year":2019,
"total":54739,
"month":"Mar"
},
{
"year":2019,
"total":28518,
"month":"Feb"
},
{
"year":2019,
"total":28518,
"month":"Jan"
}
],
"overall":424895
}
];
var res = [];
data.map((item, index) => {
const overall = item.overall;
item.data.map(i => {
res[index] = Object.assign(res[index] || {}, {[i.month]: i.total});
});
res[index] = Object.assign(res[index], {
overall, year: item._id.year
});
return res;
});
document.body.innerHTML = '<pre>' + JSON.stringify(res, null, ' ') + '</pre>';