CanvasJS - динамические dataPoints .push - Loop - PullRequest
0 голосов
/ 30 августа 2018

У меня есть динамический массив PHP со строковыми значениями:

Array ( [Overload] => 42 [Reliability] => 1024 [Relocation] => 20 [NRUC] => 7 [Storm] => 589) 

Мне интересно, как зациклить их в пончиковом графе, используя CanvasJS в dataPoints, каждый раздел представляет каждый тип:

var chart2 = new CanvasJS.Chart("chartContainer2", {
    theme: "light1",
    exportFileName: "Doughnut Chart",
    animationEnabled: true,
    title:{
        text: "Work Type",
        wrap: false,
        fontFamily: "tahoma",
        fontSize: 26
    },
    legend:{
        cursor: "pointer",
        itemclick: explodePie,
        verticalAlign: "bottom",    horizontalAlign: "bottom"
    },
    data: [{
        type: "doughnut",
        innerRadius: 60,
        showInLegend: true,
        toolTipContent: "<b>{name}</b>: {y}",
        indexLabel: "{name} - {y}",
        indexLabelFontSize: 14,
        dataPoints: [/*???????*/]}]
          });

Я пытался использовать .push, но у меня все еще есть проблемы, и он не работает:

var dps = [];
<?for($i = 0; $i <$COUNT_TYPE; $i++) {?>dps.push({y:<?echo $val[$i]?>, name: <?echo $key[$i];?>});

    <?}?>

var chart2 = new CanvasJS.Chart("chartContainer2", {
    theme: "light1",
    exportFileName: "Doughnut Chart",
    animationEnabled: true,
    title:{
        text: "Work Type",
        wrap: false,
        fontFamily: "tahoma",
        fontSize: 26
    },
    legend:{
        cursor: "pointer",
        itemclick: explodePie,
        verticalAlign: "bottom",    horizontalAlign: "bottom"
    },
    data: [{
        type: "doughnut",
        innerRadius: 60,
        showInLegend: true,
        toolTipContent: "<b>{name}</b>: {y}",
        indexLabel: "{name} - {y}",
        indexLabelFontSize: 14,
        dataPoints: dps }]
          });

мои переменные php:

                 $NEW_TYPEOFWORK = array_count_values($TW);
                 /*$NEW_TYPEOFWORK = Array ( [Overload] => 42 [Reliability] => 1024 [Relocation] => 20 [NRUC] => 7 [Storm] => 589) */
                 foreach($NEW_TYPEOFWORK as $key[]=>$val[]){}
                 $COUNT_TYPE = count($NEW_TYPEOFWORK);
...