ChartJS Laravel -生产,错误:未定义的变量:图表中的部门图表数据条形图

5jvtdoz2  于 2022-11-07  发布在  Chart.js
关注(0)|答案(2)|浏览(112)

我在Laravel-5.8中使用chartjs条形图
控制器

public function employee_goals()
{  
   //Goal-Department bar chart
    $department_record = DB::table('hr_employees')
             ->join('hr_departments', 'hr_departments.id', '=', 'hr_employees.department_id')
             ->select('hr_departments.dept_name AS department', DB::raw('COUNT(hr_employees.department_id) AS count'))
             ->where('hr_employees.company_id', $userCompany)
             ->where('hr_employees.hr_status',0)
             ->whereIn('hr_employees.employee_code', $publishedgoals)
             ->groupBy('hr_employees.department_id', 'hr_departments.dept_name')
             ->orderBy('hr_departments.dept_name', 'asc')
             ->get();    

    $department_data = [];
    foreach($department_record as $row) {
       $department_data['department_label'][] = $row->department;
       $department_data['department_data'][] = (int) $row->count;
    }
   $department_data['department_chart_data'] = json_encode($department_data); 

    return view('report.employee_goal_statistics.employee_goals',$department_data);

我已经使用了相同的代码为chartjs甜甜圈,它的工作。

<div class="chart">
              <canvas id="empDepartment" style="min-height: 250px; height: 250px; max-height: 250px; max-width: 100%;"></canvas>
            </div>

<script src="{{ asset('theme/adminlte3/plugins/chart.js/Chart.min.js') }}"></script>

    var departmentData = JSON.parse(`<?php echo $department_chart_data; ?>`);
    var departmentChartCanvas = $('#empDepartment').get(0).getContext('2d')
    var departmentData        = {

    labels: departmentData.department_label,
        datasets: [
        {
        data: departmentData.department_data,
          backgroundColor : [
                '#f56954', 
                '#00a65a', 
                '#f39c12', 
                '#00c0ef',               
          ],
                borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
            ],
            borderWidth: 1          
         }  
      ]
    }
    var departmentOptions     = {
      maintainAspectRatio : true,
      responsive : true,
    cutoutPercentage: 80,

    tooltips: {
      callbacks: {
        label: (tooltipItem, data) => {
          var value = data.datasets[0].data[tooltipItem.index];
          var total = data.datasets[0].data.reduce((a, b) => a + b, 0);
          var pct = 100 / total * value;
          var pctRounded = Math.round(pct * 10) / 10;
          return value + ' (' + pctRounded + '%)';
        }
      }
    }

    }

     var departmentChart = new Chart(departmentChartCanvas, {
      type: 'bar',
      data: departmentData,
      options: departmentOptions      
     })

当我尝试加载页面时,出现此错误
production.ERROR:未定义的变量:部门图表数据
而李世民指着这行字:

var departmentData = JSON.parse(`<?php echo $department_chart_data; ?>`);

我试了好几种方法,问题都没有解决。
如何解决?
谢谢

h43kikqp

h43kikqp1#

你的问题就在这里:

return view('report.employee_goal_statistics.employee_goals',$department_data);

view()辅助方法的第二个参数必须是数组。
您应该可以通过更改为以下内容来解决此问题:

return view('report.employee_goal_statistics.employee_goals', [
   'department_data' => $department_data,
 ]);

正如丹尼尔Logvin提到的,这里通常使用compact()方法来删除设置数组键时的重复项,这应该可以完成同样的任务:

return view('report.employee_goal_statistics.employee_goals', compact($department_data));

www.example.com上对view()帮助器方法进行了更多的讨论https://laravel.com/docs/7.x/views#creating-views

bejyjqdl

bejyjqdl2#

我知道这是太晚了回答,但通过使用laravel 5.8和这个案例,我只是认为你的问题在路由看到web.php文件小心.

相关问题