简体   繁体   English

Php 数组到 ChartJs 中的折线图

[英]Php Array to line chart in 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));
        }

由上述代码生成的图表。 I want to show this array into Chartjs Line Graph.我想将此数组显示到 Chartjs 折线图中。 I want the x axis to be the 40,60,80.我希望 x 轴为 40、60、80。 Y axis to be 800, 1200, 1600. The Dataset or Lines should be Monday, Tuesday and Wednesday. Y 轴为 800、1200、1600。数据集或行应为星期一、星期二和星期三。 Right now i get Monday, Tuesday and Wednesday as x axis and Line.现在我将星期一、星期二和星期三作为 x 轴和线。 600,800 etc are on y axis. 600,800 等在 y 轴上。

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
        )

)

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. 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.

notice that there is PHP code in this example and i have put <?php echo $json; ?>请注意,此示例中有 PHP 代码,我已<?php echo $json; ?> <?php echo $json; ?> inside the javascript code. <?php echo $json; ?> javascript 代码里面。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM