Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php数组转换为';标签';和';数据集';_Php_Yii2_Chart.js - Fatal编程技术网

将php数组转换为';标签';和';数据集';

将php数组转换为';标签';和';数据集';,php,yii2,chart.js,Php,Yii2,Chart.js,我正在使用Yii2,我会问是否有可能将数组推入标签和数据范围 这是我现在的代码: <?= ChartJs::widget([ 'type' => 'line', 'options' => [ 'height' => 400, 'width' => 800, ], 'data' => [ 'labels' => [$m[0], $m[1], $m[2], $m[3], $m[4

我正在使用Yii2,我会问是否有可能将数组推入标签和数据范围

这是我现在的代码:

<?= ChartJs::widget([
    'type' => 'line',
    'options' => [
        'height' => 400,
        'width' => 800,
    ],
    'data' => [
        'labels' => [$m[0], $m[1], $m[2], $m[3], $m[4], $m[5], $m[6], $m[7], $m[8], $m[9], $m[10], $m[11]],
        'datasets' => [
            [
                'label' => "2018",
                'backgroundColor' => "rgba(255,99,132,0.2)",
                'borderColor' => "rgba(255,99,132,1)",
                'pointBackgroundColor' => "rgba(255,99,132,1)",
                'pointBorderColor' => "#fff",
                'pointHoverBackgroundColor' => "#fff",
                'pointHoverBorderColor' => "rgba(255,99,132,1)",
                'data' => [$t[0], $t[1], $t[2], $t[3], $t[4], $t[5], $t[6], $t[7], $t[8], $t[9], $t[10], $t[11]]
            ],
        ] 
    ]
]); ?>
上面的代码生成此结果:

"0", "12", "5", "7", ...
但如果我试着做这样的事情,那是行不通的:

'data' => [$tot] or 'data' => $tot

可能吗?

我以前是这样做的:

var ctx = document.getElementById("Chart1"); // the element where the chart should be rendered
var myChart1 = new Chart(ctx, {
     type: 'line',
     data: {
        labels: [<?php 
            foreach( $m as $key => $row) {
                   echo "'".$row['label']."', "; // you can also use $row if you don't use keys                  
            }
         } ?>],

         datasets: [{
            label: '2018',
            data: [<?php
            foreach( $t as $key => $row) {
               echo "'".$row['data']."', "; // you can also use $row if you don't use keys         
             } ?>],
            backgroundColor => "rgba(255,99,132,0.2)",
            borderColor => "rgba(255,99,132,1)",
            pointBackgroundColor => "rgba(255,99,132,1)",
            pointBorderColor => "#fff",
            pointHoverBackgroundColor => "#fff",
            pointHoverBorderColor => "rgba(255,99,132,1)",
          }]
       },
     options => [
        'height' => 400,
        'width' => 800,
     ]
});  
var ctx=document.getElementById(“Chart1”);//应在其中呈现图表的元素
var myChart1=新图表(ctx{
键入:“行”,
数据:{
标签:[],
backgroundColor=>“rgba(255,99132,0.2)”,
borderColor=>“rgba(255,99132,1)”,
pointBackgroundColor=>“rgba(255,99132,1)”,
pointBorderColor=>“#fff”,
pointHoverBackgroundColor=>“#fff”,
pointHoverBorderColor=>“rgba(255,99132,1)”,
}]
},
选项=>[
“高度”=>400,
“宽度”=>800,
]
});  
我用javascript创建图表,并从php数组中添加数据

您可以通过添加具有上述ID的元素来显示图表

PHP用于处理数据,JS用于显示数据。
因此,使用JS显示图表,使用PHP处理数据

在您的情况下,您可以这样做:

<?php
$data = array(foreach( $t as $key => $row) {
     echo "'".$row['data']."', "; // you can also use $row if you don't use keys         
});
$labels = array(foreach( $m as $key => $row) {
     echo "'".$row['label']."', "; // you can also use $row if you don't use keys         
});
?>

<?= ChartJs::widget([
'type' => 'line',
'options' => [
    'height' => 400,
    'width' => 800,
],
'data' => [
    'labels' => $labels,
    'datasets' => [
        [
            'label' => "2018",
            'backgroundColor' => "rgba(255,99,132,0.2)",
            'borderColor' => "rgba(255,99,132,1)",
            'pointBackgroundColor' => "rgba(255,99,132,1)",
            'pointBorderColor' => "#fff",
            'pointHoverBackgroundColor' => "#fff",
            'pointHoverBorderColor' => "rgba(255,99,132,1)",
            'data' => $data
        ],
    ] 
]
]); ?>

我的错误在代码的另一部分,与数组相关。我将发布正确的解决方案:

<?= ChartJs::widget([
    'type' => 'line',
    'options' => [
        'height' => 400,
        'width' => 800,
    ],
    'data' => [
        'labels' => $labels,
        'datasets' => [
            [
                'label' => "2018",
                'backgroundColor' => "rgba(255,99,132,0.2)",
                'borderColor' => "rgba(255,99,132,1)",
                'pointBackgroundColor' => "rgba(255,99,132,1)",
                'pointBorderColor' => "#fff",
                'pointHoverBackgroundColor' => "#fff",
                'pointHoverBorderColor' => "rgba(255,99,132,1)",
                'data' => $data,
            ],
        ] 
    ]
]); ?>

您也可以使用中的内联PHP代码来实现这一点javascript@JelleBotman如果有办法,你能解释一下吗?我也会接受不同的方法。您在运行时试图更新图表,还是在解析图表时遇到问题array@L.Antonelli这对你有帮助?我正在尝试,但使用框架与使用普通代码不同,因此我在使用此代码时遇到了麻烦。@L.Antonelli检查我的更新答案,拿下面的代码块给我一个错误:语法错误,意外的'foreach'(T_foreach),期望''我发现了错误,我不需要你的代码,我的数组中有一个错误,显示了一个空间字符,但不起作用,谢谢你的帮助:)
<?= ChartJs::widget([
    'type' => 'line',
    'options' => [
        'height' => 400,
        'width' => 800,
    ],
    'data' => [
        'labels' => $labels,
        'datasets' => [
            [
                'label' => "2018",
                'backgroundColor' => "rgba(255,99,132,0.2)",
                'borderColor' => "rgba(255,99,132,1)",
                'pointBackgroundColor' => "rgba(255,99,132,1)",
                'pointBorderColor' => "#fff",
                'pointHoverBackgroundColor' => "#fff",
                'pointHoverBorderColor' => "rgba(255,99,132,1)",
                'data' => $data,
            ],
        ] 
    ]
]); ?>
$labels = array();
$data = array();

$curweek = date('Y-m-d', strtotime("previous monday"));
$today = date('Y-m-d');

$dates = new DatePeriod(
    DateTime::createFromFormat('Y-m-d|', $curweek),
    new DateInterval('P1D'),
    DateTime::createFromFormat('Y-m-d|', $today)->add(new DateInterval('P1D'))
);

foreach ($dates as $date) {
    $datestr = $date->format('Y-m-d');
    $index = array_search($datestr, array_column($chartvalue, 'dataNl'));
    if ($index === false) {
        $labels[] = date("d", strtotime($datestr));
        $data[] = 0;
    } else {
        $labels[] = date("d", strtotime($datestr));
        $data[] = $chartvalue[$index]['totale'];
    }
}