我正在尝试在甜甜圈图的dataPoints内部循环。我在PHP中有一个动态的values(string)数组,但是此数组经常更改。我在dataPoints中显示我的php变量,但在其他图中,但是对于这个甜甜圈图,我试图在dataPoints中循环,以便它也自动设置{y}和名称。
data: [{
type: "doughnut",
innerRadius: 60,
showInLegend: true,
toolTipContent: "<b>{name}</b>: {y}",
indexLabel: "{name} - {y}",
indexLabelFontSize: 14,
dataPoints: dps
/*[
{ y: <?echo $Count_Upcoming;?>, name: "Up coming", color: "#215C89" },
{ y: <?echo $Count_Planned;?>, name: "Scheduled", color: "#3984BD" },
{ y: <?echo $Count_Review;?>, name: "In Review", color: "#46A4EB" },
{ y: <?echo $Count_Trouble;?>, name: "Trouble", color: "#DC5151" },
{ y: <?echo $Count_Maint;?>, name: "UG Maintenance", color: "#659FC8" },
{ y: <?echo $Count_CUST;?>, name: "Services", color: "#A0B2BF" }
]*/
}]
在此之外,我正在尝试这样做:
<?for($i = 0; $i < $COUNT_TYPE; $i++){?>
dps.push({y: <?echo $val[$i];?> }, name: <?echo $key[$i];?>)
<?}?>
我的php数组:
$NEW_TYPEOFWORK = array_count_values($TW);
foreach($NEW_TYPEOFWORK as $key[]=>$val[]){}
$COUNT_TYPE = count($NEW_TYPEOFWORK);
这怎么办?或是否还有其他方法可以在JavaScript图表内循环为php变量的dataPoints循环。
答案 0 :(得分:0)
不知道您的php数组中的颜色是什么-但是我会像这样进行php输出循环:
None
答案 1 :(得分:0)
我通过以下方法解决了它: php:将数组设置为此:
foreach($NEW_TYPEOFWORK as $key=>$val){$datapoint[] = array("y"=> $val, "name"=> $key); }
在CanvasJS中:
data: [{
type: "doughnut",
innerRadius: 60,
showInLegend: true,
toolTipContent: "<b>{name}</b>: {y}",
indexLabel: "{name} - {y}",
indexLabelFontSize: 14,
dataPoints: <?echo json_encode($datapoint, JSON_NUMERIC_CHECK);?>
}]