我想使用 Laravel 7 将我的查询输出传递到 jQuery 图表 js

Jer*_*rad 7 php jquery charts laravel

我想在折线图中显示每个月的注册学生人数。我从查询中得到了结果,但我无法理解如何在折线图 js 文件中传递值。请帮助解决这个问题。我附上了输出的代码和屏幕截图。

我想将查询输出数据传递给jQuery 数据:[0, 4, 9, 18, 21, 34, 20, 35, 45, 53, 49, 60],

应用程序.js

(function ($) {
  "use strict"; // Chart
  if ($('#report-line-chart').length) {
    var ctx = $('#report-line-chart')[0].getContext('2d');
    var myChart = new chart_js__WEBPACK_IMPORTED_MODULE_1___default.a(ctx, {
      type: 'line',
      data: {
        labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        datasets: [{
          label: '# of total',
          data: [0, 4, 9, 18, 21, 34, 20, 35, 45, 53, 49, 60],
          borderWidth: 2,
          borderColor: '#3160D8',
          backgroundColor: 'transparent',
          pointBorderColor: 'transparent'
        }]
      },
      options: {
        legend: {
          display: false
        },
        scales: {
          xAxes: [{
            ticks: {
              fontSize: '12',
              fontColor: '#777777'
            },
            gridLines: {
              display: false
            }
          }],
          yAxes: [{
            ticks: {
              fontSize: '12',
              fontColor: '#777777',
              callback: function callback(value, index, values) {
                return value;
              }
            },
            gridLines: {
              color: '#D8D8D8',
              zeroLineColor: '#D8D8D8',
              borderDash: [2, 2],
              zeroLineBorderDash: [2, 2],
              drawBorder: false
            }
          }]
        }
      }
    });
  })($s);
Run Code Online (Sandbox Code Playgroud)

我的控制器查询

<?php
$arr["eachMonthRegister"] = DB::table('students')->select(DB::raw('count(id) as `data`'),  DB::raw('YEAR(created_at) year, MONTH(created_at) month'))
        ->groupby('year','month')
        ->get();

        return view('home')->with($arr);
?>
Run Code Online (Sandbox Code Playgroud)

我的查询输出

在此处输入图片说明

Ara*_*esi 5

其实你做对了,但我在你的代码中添加了一些部分。

- 控制器

$result = DB::table('students')->select(DB::raw('count(id) as `data`'),  
DB::raw('YEAR(created_at) year, MONTH(created_at) month'))
    ->groupby('year','month')
    ->orderby('month) // I added this line
    ->get();

$output = [];
    
foreach ($result as $entry) {
    if (isset($output[$entry->month]))
        $output[$entry->month] += $entry->data;
    else
        $output[$entry->month] = $entry->data;
}
$lineChartData= array_values($output);
return view('home')->with($lineChartData);

 // $lineChartData will be like below
 // [0, 4, 9, 18, 21, ...]
Run Code Online (Sandbox Code Playgroud)

- home.blade

<script>
    var lineChartData = @json($lineChartData)
    // [0, 4, 9, 18, 21, ...]
</script>
Run Code Online (Sandbox Code Playgroud)

- 应用程序.js

 .
 ..
 ...
 datasets: [{
      label: '# of total',
      data: lineChartData,
Run Code Online (Sandbox Code Playgroud)

另一个注意事项:似乎您不需要year从 DB 返回。如果是这样,您可以更改如下代码并提高代码的性能和速度:

- 控制器

$result = DB::table('students')->select(DB::raw('count(id) as `data`'),  
DB::raw(MONTH(created_at) month'))
    ->groupby('month')
    ->orderby('month)
    ->get();


// same as top codes
...
Run Code Online (Sandbox Code Playgroud)