犯罪嫌疑人X
您的代码有几个问题。首先,你的数据集应该有标签数量的数据,这样你就不能在不填充的情况下忽略空白的地方。要解决此问题,您需要更改 $parsedAry['datasets'][$i]['data'] = array();为$parsedAry['datasets'][$i]['data'] = array(0, 0, 0, 0, 0);这将在您的数据集中添加 5 个空位。其次,您必须为此将数字放在正确的位置,您必须为最后一个循环添加一个计数器以识别正确的位置。最后你的代码应该是这样的。function chart(){ $ar = RNCPHP\AnalyticsReport::fetch('Open tickets Ageing by priority'); $arr = $ar->run(0, $filters); $chartInit = array(); $chartArr['data'] = array(); // push the report data into an array for ($ii = $arr->count(); $ii--;) { $row = $arr->next(); if ($row['Severity'] == null || $row['Severity'] == "") { $row['Severity'] = "No Value"; } $row['Sort'] = $row['Sort']; array_push($chartInit, $row); } echo "<pre>"; print_r($chartInit); array_multisort(array_map(function ($element) { return $element['Sort']; }, $chartInit), SORT_DESC, $chartInit); $chartDataArr = array(); $sevArr = array(); foreach ($chartInit as $k => $v) { if (!isset($chartDataArr[$v['Weeks']])) { $chartDataArr[$v['Weeks']] = array(); } array_push($chartDataArr[$v['Weeks']], $v); if (!in_array($v['Severity'], $sevArr)) { array_push($sevArr, $v['Severity']); } } $mapLabels = array(); $parsedAry = array(); $parsedAry['labels'] = array(); $parsedAry['datasets'] = array(); for ($i = 0; $i < count($sevArr); $i++) { $parsedAry['datasets'][$i] = array(); $parsedAry['datasets'][$i]['label'] = $sevArr[$i]; $parsedAry['datasets'][$i]['backgroundColor'] = $this->getColor($i); $parsedAry['datasets'][$i]['borderColor'] = $this->getColor($i); $parsedAry['datasets'][$i]['borderWidth'] = 1; $parsedAry['datasets'][$i]['maxBarThickness'] = 50; $parsedAry['datasets'][$i]['data'] = array(0, 0, 0, 0, 0); } $labelsNo = 0; foreach ($chartDataArr as $k => $v) { array_push($parsedAry['labels'], $k); foreach ($v as $k1 => $v1) { $keySev = ""; foreach ($parsedAry['datasets'] as $k2 => $v2) { if ($v2['label'] == $v1['Severity']) { $keySev = $k2; } } if ($v1['Count'] == null || $v1['Count'] == "") { $v1['Count'] = 0; } $parsedAry['datasets'][$keySev]['data'][$labelsNo] = $v1['Count']; } $labelsNo++; } echo "<pre>"; print_r($parsedAry); echo "</pre>"; $chartArr['data'] = $parsedAry; return $chartArr;}