在 ChartJS 和 PHP 中分组数据

您好有一个数据表,我正在尝试使用 ChartJS 绘制图表。数据数组如下所示


Array

(

    [0] => Array

        (

            [Sort] => 2

            [Weeks] => 1-2 Weeks

            [Severity] => P1

            [Count] => 7

        )


    [1] => Array

        (

            [Sort] => 2

            [Weeks] => 1-2 Weeks

            [Severity] => P2

            [Count] => 3

        )


    [2] => Array

        (

            [Sort] => 2

            [Weeks] => 1-2 Weeks

            [Severity] => P3

            [Count] => 4

        )


    [3] => Array

        (

            [Sort] => 2

            [Weeks] => 1-2 Weeks

            [Severity] => P4

            [Count] => 3

        )


    [4] => Array

        (

            [Sort] => 3

            [Weeks] => 2-3 weeks

            [Severity] => P1

            [Count] => 1

        )


    [5] => Array

        (

            [Sort] => 4

            [Weeks] => >4 weeks

            [Severity] => No Value

            [Count] => 1

        )


    [6] => Array

        (

            [Sort] => 4

            [Weeks] => >4 weeks

            [Severity] => P1

            [Count] => 1

        )


    [7] => Array

        (

            [Sort] => 4

            [Weeks] => >4 weeks

            [Severity] => P3

            [Count] => 1

        )


我转换了与 ChartJS 兼容的数组,如下所示


Array

(

    [labels] => Array

        (

            [0] => >4 weeks

            [1] => 2-3 weeks

            [2] => 1-2 Weeks

        )


    [datasets] => Array

        (

            [0] => Array

                (

                    [label] => No Value

                    [backgroundColor] => #F00

                    [borderColor] => #F00

                    [borderWidth] => 1

                    [maxBarThickness] => 50

                    [data] => Array

                        (

                            [0] => 1

                        )


                )

但是当数据绘制到图表时,数据会被打乱。数据是这样排列的

http://img1.mukewang.com/64421f73000183a306530331.jpg

这是真实数据以及图表的外观

http://img3.mukewang.com/64421f820001890d06590314.jpg

潇潇雨雨
浏览 77回答 1
1回答

犯罪嫌疑人X

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