PHP数组到ChartJs中的折线图 [英] Php Array to line chart in ChartJs

查看:46
本文介绍了PHP数组到ChartJs中的折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        $days = ["Monday","Tuesday","Wednesday"];
        $rates = [40,60,80];
        $profit = [];

        foreach($days as $day => $value){
            foreach($rates as $rate){
                $netprofit = $rate* 20;
                $profit[$value] = [$rate=> $netprofit];
            }

        }

        $usersChart = new UserChart;
        $usersChart->labels($days);
        foreach($profit as $key => $value){
            $data = array();
            foreach ($value as $values){
                $data[] = $values;
            }


            $usersChart->dataset($key, 'line', collect($data));
        }

我想将此数组显示为Chartjs线形图.我希望x轴为40,60,80.Y轴为800、1200、1600.数据集或线应为星期一,星期二和星期三.现在,我将星期一,星期二和星期三作为x轴和线.y轴上有600,800等.

I want to show this array into Chartjs Line Graph. I want the x axis to be the 40,60,80. Y axis to be 800, 1200, 1600. The Dataset or Lines should be Monday, Tuesday and Wednesday. Right now i get Monday, Tuesday and Wednesday as x axis and Line. 600,800 etc are on y axis.

Array
(
    [Monday] => Array
        (
            [40] => 800
            [60] => 1200
            [80] => 1600
        )

    [Tuesday] => Array
        (
            [40] => 800
            [60] => 1200
            [80] => 1600
        )

    [Wednesday] => Array
        (
            [40] => 800
            [60] => 1200
            [80] => 1600
        )

)

推荐答案

保持数组原样并将其转换为JSON对象,然后在Javascript中回显它,javascript必须在php文件中运行,而不是在外部javascript中运行文件.

keep your array as it is and convert it to JSON object and then echo it inside Javascript, javascript have to be inside php file to run, not in external javascript file.

请注意,此示例中有PHP代码,我已将<?php echo $ json;?> 放在JavaScript代码中.

notice that there is PHP code in this example and i have put <?php echo $json; ?> inside the javascript code.

<html>
    <head>
        <style>body{width: 800px;}</style>
        <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
    </head>
    <body>
        <div>
            <canvas  id="myChart" width="700px" height="300px"></canvas>
        </div>      
        <?php
            $array = array(600, 800, 1200, 1800);
            $json = json_encode($array);
        ?>
        <script>
            var ctx = document.getElementById('myChart').getContext('2d');
            var chart = new Chart(ctx, {
              // The type of chart we want to create
              type: 'line',

              // The data for our dataset
              data: {
                labels: ["20", "40", "60", "80"],
                datasets: [
                  {
                    label: 'Monday',
                    borderColor: "#FF9F40",
                    data: <?php echo $json; ?>,
                  },
                  {
                    label: 'Tuesday',
                    borderColor: "#FF6384",
                    data: [600, 800, 1200, 1800]
                  },
                  {
                    label: 'Wednesday',
                    borderColor: "#219151",
                    data: [600, 800, 1200, 1800]
                  }
                ]
              },

              // Configuration options go here
              options: {}
            });
        </script>
    </body>
</html>

这篇关于PHP数组到ChartJs中的折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆